保持变量与doop循环sas

时间:2018-08-23 14:21:38

标签: loops sas

在数据步骤中,是否有任何形式可以通过变量循环保留变量? 将为:

data test;
input id aper_f_201501 aper_f_201502 aper_f_201503 aper_f_201504 
aper_f_201505 aper_f_201506;
datalines;
1 0 1 2 3 5 7
2 -1 5 4 8 7 9
;
run;

%macro test;
%let date = '01Jul2015'd;

data test2;
set test(keep=do i = 1 to 3;
                 aper_f_%sysfunc(intnx(month,&date,-i,begin),yymmn6.);
              end;)
run;
%mend;
%test;

我需要迭代几个日期。 非常感谢。

2 个答案:

答案 0 :(得分:3)

您需要使用宏%do循环而不是数据步骤do循环,该循环在数据集选项中间将无效。也不要在数据集选项的中间生成那些多余的分号。并且请使用分号结束SET语句。

%macro test;
%local i date;
%let date = '01Jul2015'd;

data test2;
  set test(keep=
%do i = 1 %to 3;
  aper_f_%sysfunc(intnx(month,&date,-i,begin),yymmn6.)
%end;
  );
run;
%mend;
%test;

答案 1 :(得分:1)

您可以使用冒号快捷方式引用具有相同前缀的变量,冒号前面的所有内容都将保留。

keep ID aper_f_2015: ;

当您有顺序列表时,还会有一个连字符

keep ID aper_f_201501-aper_f_201512;

您可以使用宏,但不确定在这里是否可以增加很多价值。