%if %then %do 条件未解决
%if %then %do condition not resolved
我继承了宏,其中下面的块似乎无法解析。该宏应该有条件地 select 基于日期值的逻辑操作。如果满足 %if 条件,它们应该打印 %put 语句中提到的宏变量,这些变量又成为 if 条件的一部分。如果满足条件 %if %length(&datein.)=10 OR %length(ST_&date.)=10,它将打印第一个 %put 语句,否则第二个 %put 语句将被打印。相同的逻辑适用于第二个 %if 语句。
宏应该 运行 在数据步骤中。逻辑运算符 AND 被视为变量,我收到一条注释 注意:变量 AND 未初始化。
我试图通过添加下面代码中提到的左括号和右括号来平衡 if 条件。尝试更改 if 条件的结构以将第一个和第二个条件分开,但似乎不起作用。我假设逻辑运算符 AND 不能像在 %if %then %else 语句中那样使用。
%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');
你的程序没有任何意义。如果我们忽略可能向 LOG 生成消息的 %IF 语句,您的宏将生成此 SAS 代码:
if &datein. ne "" and ST_&date. ne "" then do;
if ( AND ) then EPOCH=&label.;
end;
由于您使用变量 AND
作为 IF 语句的条件,SAS 将假定它是一个数值变量并测试其值是否为非零和非缺失值。如果没有名为 AND
的变量,那么它的值将丢失,因此 EPOCH
的值永远不会改变。
如果您希望宏对宏变量长度进行那些测试,则将该宏逻辑从您尝试生成的 SAS 代码的中间拉出。
让我们看一下第一个 %IF/%THEN 语句。
%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_
总是长度为 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;
我继承了宏,其中下面的块似乎无法解析。该宏应该有条件地 select 基于日期值的逻辑操作。如果满足 %if 条件,它们应该打印 %put 语句中提到的宏变量,这些变量又成为 if 条件的一部分。如果满足条件 %if %length(&datein.)=10 OR %length(ST_&date.)=10,它将打印第一个 %put 语句,否则第二个 %put 语句将被打印。相同的逻辑适用于第二个 %if 语句。
宏应该 运行 在数据步骤中。逻辑运算符 AND 被视为变量,我收到一条注释 注意:变量 AND 未初始化。
我试图通过添加下面代码中提到的左括号和右括号来平衡 if 条件。尝试更改 if 条件的结构以将第一个和第二个条件分开,但似乎不起作用。我假设逻辑运算符 AND 不能像在 %if %then %else 语句中那样使用。
%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');
你的程序没有任何意义。如果我们忽略可能向 LOG 生成消息的 %IF 语句,您的宏将生成此 SAS 代码:
if &datein. ne "" and ST_&date. ne "" then do;
if ( AND ) then EPOCH=&label.;
end;
由于您使用变量 AND
作为 IF 语句的条件,SAS 将假定它是一个数值变量并测试其值是否为非零和非缺失值。如果没有名为 AND
的变量,那么它的值将丢失,因此 EPOCH
的值永远不会改变。
如果您希望宏对宏变量长度进行那些测试,则将该宏逻辑从您尝试生成的 SAS 代码的中间拉出。
让我们看一下第一个 %IF/%THEN 语句。
%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_
总是长度为 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;