如果找不到记录,如何在变量中输入SAS中的空值

如果找不到记录,如何在变量中输入SAS中的空值,sas,sas-macro,Sas,Sas Macro,我在SAS上工作,从数据集中获取值,并将它们保存在SAS中的变量中 样本数据: table RK | ID | column_1 | column_2 1 | one| value_1 | 2 | two| value_1 | value_2 proc sql noprint; select column_1 into: variable_1 from table where RK = 1; select column_2 into: variable_2 from tab

我在SAS上工作,从数据集中获取值,并将它们保存在SAS中的变量中

样本数据:

table
RK | ID | column_1 | column_2
1  | one| value_1  |  
2  | two| value_1  |  value_2

proc sql noprint;

select column_1 
into: variable_1
from table
where RK = 1;

select column_2
into: variable_2
from table
where RK = 1;

quit;
现在我想在我的报告中使用这些变量,如果我的报告中没有数据,我想打印一个空格。作为

%put &variable_1;
%put &variable_2;
结果

value_1
&variable_2
如果我的into变量中没有值,我希望它只在我的日志或报告中打印一个空格

我该怎么做

预期结果

value_1
(A blank space)
试试这个:

proc sql noprint;

select case when column_1 is null then " " else column_1 end 
into: variable_1
from table
where RK = 1;

select case when column_2 is null then " " else column_2 end 
into: variable_2
from table
where RK = 1;

quit;


%put &variable_1;
%put &variable_2;
试试这个:

proc sql noprint;

select case when column_1 is null then " " else column_1 end 
into: variable_1
from table
where RK = 1;

select case when column_2 is null then " " else column_2 end 
into: variable_2
from table
where RK = 1;

quit;


%put &variable_1;
%put &variable_2;

使用聚结:

proc sql noprint;

select coalesecec(column_1," ") 
into: variable_1
from table
where RK = 1;

select coalesecec(column_2," ") 
into: variable_2
from table
where RK = 1;

quit;

使用聚结:

proc sql noprint;

select coalesecec(column_1," ") 
into: variable_1
from table
where RK = 1;

select coalesecec(column_2," ") 
into: variable_2
from table
where RK = 1;

quit;

如果
select
语句未返回任何行(空源表或没有与where条件匹配的行),则不会创建
into
子句中命名的宏变量。在运行select语句之前,只需使用
%let
语句设置默认值

proc sql noprint ;
%let infant_list=;
select name
  into :infant_list separated by ' '
  from sashelp.class 
  where age < 5 
;
quit;
%put &=infant_list;

如果
select
语句未返回任何行(空源表或没有与where条件匹配的行),则不会创建
into
子句中命名的宏变量。在运行select语句之前,只需使用
%let
语句设置默认值

proc sql noprint ;
%let infant_list=;
select name
  into :infant_list separated by ' '
  from sashelp.class 
  where age < 5 
;
quit;
%put &=infant_list;
日志 命名宏值记录是一种快捷语法

%put &=variable_1; /* is almost the same as */
%put variable_1=&variable_1;
如果变量_1包含不带引号的分号或其他混淆的编程段,最好使用
superq
记录。宏变量也可以通过将值分辨率括起来在日志中显示得更清楚。这将让您看到前导空格和尾随空格

%put NOTE: variable_1=[%superq(variable_1)];
进入 您可以在
INTO
子句中选择指定多个变量

select a, b
into :a, :b
基本
转换为
表单不会修剪值,目标(宏变量)值长度基于源变量、计算长度或选择项
length=
选项指定的长度

select a length=50, substr(b,1,2)
into :a_50, :b_2

/* length of macro variable 'variable_1' will be the same length as column_1,
   regardless of the " ".  If the string literal was longer than column_1,
   the computation length is the string literals length.
*/
select case when column_1 is null then " " else column_1 end 
into: variable_1
目标中有额外的语法和关键字

  • 将值修剪为宏变量
    选择一个
    插入:已修剪的
  • 将多行中的值修剪为多个(范围)宏变量
    选择一个
    INTO:A1-A99
    /*在日志中填充&SQLOBS宏变量范围 命名宏值记录是一种快捷语法

    %put &=variable_1; /* is almost the same as */
    %put variable_1=&variable_1;
    
    如果变量_1包含不带引号的分号或其他混淆的编程段,最好使用
    superq
    记录。宏变量也可以通过将值分辨率括起来在日志中显示得更清楚。这将让您看到前导空格和尾随空格

    %put NOTE: variable_1=[%superq(variable_1)];
    
    进入 您可以在
    INTO
    子句中选择指定多个变量

    select a, b
    into :a, :b
    
    基本
    转换为
    表单不会修剪值,目标(宏变量)值长度基于源变量、计算长度或选择项
    length=
    选项指定的长度

    select a length=50, substr(b,1,2)
    into :a_50, :b_2
    
    /* length of macro variable 'variable_1' will be the same length as column_1,
       regardless of the " ".  If the string literal was longer than column_1,
       the computation length is the string literals length.
    */
    select case when column_1 is null then " " else column_1 end 
    into: variable_1
    
    目标中有额外的语法和关键字

    • 将值修剪为宏变量
      选择一个
      插入:已修剪的
    • 将多行中的值修剪为多个(范围)宏变量
      选择一个
      
      INTO:A1-A99
      /*填充&SQLOBS宏变量范围(如果它正在工作)。因为它正在打印一个空间,所以您可能无法在logIt中看到它正在工作。因为它正在打印一个空格,所以您可能无法在日志中看到它