使用DATA NULL将let变量分配给WHERE语句

时间:2019-05-02 20:06:07

标签: sas sas-macro

嗨,我正在尝试使用DATA NULL步骤根据不同条件将值分配给变量。来自NULL语句的此变量将在以下DATA步骤中分配给WHERE语句。

理想情况下,如果我今天(星期四为5)运行该代码,则两个变量均应返回30APR2019。但是我的代码仅在LAST-IF-语句中抛出变量值。

data _null_;

if weekday(today()) = 5 then do;
    %let exc_st_day = '30APR2019'd;
%let exc_en_day = '30APR2019'd;
end;

else if weekday(today()) = 6 then do;
%let exc_st_day = '01MAY2019'd;
%let exc_en_day = '01MAY2019'd;
end;

else if weekday(today()) = 2 then do;
%let exc_st_day = '02MAY2019'd;
%let exc_en_day = '02MAY2019'd;
end;

else if weekday(today()) = 3 then do;
%let exc_st_day = '03MAY2019'd;
%let exc_en_day = '03MAY2019'd;
end;

else if weekday(today()) = 4 then do;
%let exc_st_day = '04MAY2019'd;
%let exc_en_day = '06MAY2019'd;
end;

%put &exc_st_day &exc_en_day;

run;

2 个答案:

答案 0 :(得分:0)

您需要使用CALL SYMPUTX()创建宏变量,而不是在数据步骤中创建%LET。

if weekday(today()) = 5 then do;
    call symputx('exc_st_day', '30APR2019'd);
    call symputx('exc_en_day', '30APR2019'd);
end;

答案 1 :(得分:0)

宏代码将在其生成的SAS代码运行之前进行评估。因此,您告诉SAS运行以下代码:

%let exc_st_day = '30APR2019'd;
%let exc_en_day = '30APR2019'd;
%let exc_st_day = '01MAY2019'd;
%let exc_en_day = '01MAY2019'd;
%let exc_st_day = '02MAY2019'd;
%let exc_en_day = '02MAY2019'd;
%let exc_st_day = '03MAY2019'd;
%let exc_en_day = '03MAY2019'd;
%let exc_st_day = '04MAY2019'd;
%let exc_en_day = '06MAY2019'd;
%put &exc_st_day &exc_en_day;

data _null_;
if weekday(today()) = 5 then do;
end;
else if weekday(today()) = 6 then do;
end;
else if weekday(today()) = 2 then do;
end;
else if weekday(today()) = 3 then do;
end;
else if weekday(today()) = 4 then do;
end;
run;

如果要从数据步骤创建宏变量值,请使用CALL SYMPUTX()函数。或者,如果您确实需要在宏变量值中插入前导和/或尾随空格,请使用旧的CALL SYMPUT()函数。