If statement %如果为%,则%不解决条件
我继承了一个宏,其中下面的块似乎没有被解析。宏应该根据日期值有条件地选择逻辑操作。如果满足%If条件,则应打印%put语句中提到的宏变量,该宏变量将成为If条件的一部分。如果满足条件%If%length&datein.=10或%lengthST&date.=10,它将打印第一条%put语句,否则将打印第二条%put语句。同样的逻辑适用于第二条%if语句 宏应该在数据步骤中运行。逻辑运算符和被视为一个变量,我得到一个提示:变量和未初始化 我试图通过添加下面代码中提到的开括号和闭括号来平衡if条件。试图更改if条件的结构以分离第一个条件和第二个条件,但似乎不起作用。我假设逻辑运算符AND不能像在%if%then%else语句中那样使用If statement %如果为%,则%不解决条件,if-statement,sas,sas-macro,If Statement,Sas,Sas Macro,我继承了一个宏,其中下面的块似乎没有被解析。宏应该根据日期值有条件地选择逻辑操作。如果满足%If条件,则应打印%put语句中提到的宏变量,该宏变量将成为If条件的一部分。如果满足条件%If%length&datein.=10或%lengthST&date.=10,它将打印第一条%put语句,否则将打印第二条%put语句。同样的逻辑适用于第二条%if语句 宏应该在数据步骤中运行。逻辑运算符和被视为一个变量,我得到一个提示:变量和未初始化 我试图通过添加下面代码中提到的开括号和闭括号来平衡if条件。
%macro test_date(date=, comp1=, comp2=, label=);
if &datein. ne "" and ST_&date. ne "" then do;
if (%if %length(&datein.)=10 OR %length(ST_&date.)=10 %then %put ST_&date._10 &comp1. datein_10; %else %put ST_&date._19 &comp1. datein_19;
AND %if %length(&datein.)=10 OR %length(ED_&date.)=10 %then %put datein_10 &comp2. ED_&date._10; %else %put datein_19 &comp2. ED_&date._19;)
then EPOCH=&label.;
end;
%mend test_date;
%test_date(date=SCREEN, comp1= le, comp2= le, label='SCREEN');
%if %length(&datein.)=10 OR %length(ST_&date.)=10
%then %put ST_&date._10 &comp1. datein_10;
%else %put ST_&date._19 &comp1. datein_19;
你的程序毫无意义。如果忽略可能正在向日志生成消息的%If语句,则宏正在生成此SAS代码:
if &datein. ne "" and ST_&date. ne "" then do;
if ( AND ) then EPOCH=&label.;
end;
由于使用变量和作为IF语句的条件,SAS将假定它是一个数值变量,并测试其值是否为非零和非缺失。如果没有命名的变量,那么它的值将丢失,因此EPOCH的值将永远不会更改
如果您希望宏执行这些宏变量长度的测试,那么请将该宏逻辑从您试图生成的SAS代码中间拉出
让我们看一下第一条%IF/%THEN语句
%macro test_date(date=, comp1=, comp2=, label=);
if &datein. ne "" and ST_&date. ne "" then do;
if (%if %length(&datein.)=10 OR %length(ST_&date.)=10 %then %put ST_&date._10 &comp1. datein_10; %else %put ST_&date._19 &comp1. datein_19;
AND %if %length(&datein.)=10 OR %length(ED_&date.)=10 %then %put datein_10 &comp2. ED_&date._10; %else %put datein_19 &comp2. ED_&date._19;)
then EPOCH=&label.;
end;
%mend test_date;
%test_date(date=SCREEN, comp1= le, comp2= le, label='SCREEN');
%if %length(&datein.)=10 OR %length(ST_&date.)=10
%then %put ST_&date._10 &comp1. datein_10;
%else %put ST_&date._19 &comp1. datein_19;
因此,您正在测试宏变量DATEIN的长度是10字节还是宏变量DATE的长度是7字节。常量文本ST_u将始终为3字节长。您可能需要对此进行一些调整,但这有助于理清其逻辑。下面的代码检查宏变量的长度,并根据该长度插入数据步骤代码。它还会将一些值打印到日志中,但这不会影响结果。我已经包括了一些简单的数据步骤示例来说明它是如何工作的
%macro test_date(date=, comp1=, comp2=, label=);
%if &datein. ne and &&ST_&date. ne %then %do;
%if %length(&datein.)=10 OR %length(&&ST_&date.)=10 %then %do;
/* Insert data step code */
if variable1=1 then epoch="First condition";
else epoch="firstelse";
/* Print to log*/
%put &&ST_&date. &comp1. &datein.;
%end;
%else %if %length(&datein.)=10 OR %length(&&ED_&date.)=10 %then %do;
if variable1=0 then epoch="Second condition";
else epoch="secondelse";
%end;
%end;
%mend test_date;
/* Macro variables given values of length 10. Outer if and first condition in do block holds.*/
%let datein=0123456789;
%let st_screen=0123456789;
%let ed_screen=0123456789;
data test1;
variable1=1;
%test_date(date=SCREEN, comp1= le, comp2= le, label='SCREEN');
run;
data test2;
variable1=0;
%test_date(date=SCREEN, comp1= le, comp2= le, label='SCREEN');
run;
/* Macro variables given values of length 9 and 10. Outer if and else condition in do block holds.*/
%let datein=012345678;
%let st_screen=012345678;
%let ed_screen=0123456789;
data test3;
variable1=1;
%test_date(date=SCREEN, comp1= le, comp2= le, label='SCREEN');
run;
data test4;
variable1=0;
%test_date(date=SCREEN, comp1= le, comp2= le, label='SCREEN');
run;
/* Macro variables are empty. Outer if is false. The macro does nothing. */
%let datein=;
%let st_screen=;
%let ed_screen=;
data test5;
variable1=1;
%test_date(date=SCREEN, comp1= le, comp2= le, label='SCREEN');
run;
data test6;
variable1=0;
%test_date(date=SCREEN, comp1= le, comp2= le, label='SCREEN');
run;
这对我来说没什么意义。它是if,then,else和epoch,而不是%if,%then,%else和%let epoch,所以我假设它应该在一个数据步骤中运行?内部if中的内容将写入日志,并且在数据步骤运行时不会解析为true或false。而且不可能是第一个发表声明的人。ST_&date在外部if中似乎是数据步变量,在内部if中似乎是字符串。这是怎么回事?抱歉没有说清楚。我已经更新了我的问题。你希望宏做什么?您希望宏生成什么SAS代码?显示您试图使用此宏生成的部分数据步骤的其余部分。如果满足%If条件,则应打印%put语句中提到的宏变量,这些变量将成为If条件的一部分。这段代码是一段庞大代码的一部分,这段代码会很枯燥。很抱歉不清楚。我更新了我的问题,让你更清楚。