簡體   English   中英

使用doop循環保存變量sas

[英]keep variables with a doop loop sas

在數據步驟中是否有任何形式可以使用doop循環來保存變量? 將是:

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;

我需要迭代幾個日期。 非常感謝你。

您需要使用宏%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;

您可以使用冒號快捷方式引用具有相同前綴的變量,冒號前面的任何內容都將保留。

keep ID aper_f_2015: ;

當你有順序列表時,還有一個連字符

keep ID aper_f_201501-aper_f_201512;

您可以使用宏但不確定它在這里增加了很多價值。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM