When i run the program, period_1 appear in the date1 dataset, may i know how to output period_2 to period_24 into the date1 dataset? Thanks.
data date1;
date1 = intnx('month',&today,-1,'end');
period_1 = substr(put(date1, yymmddn8.),3,4);
do i=2 to 24;
call symput(cats("Period_",i),substr(put(intnx('month',date1,-(i-1),'end'),yymmddn8.),3,4));
end;
run;
Use arrays:
%let today=today();
data date1;
array period_[24] $ 10;
do i=1 to 24;
date1 = intnx('month',&today,-i,'end');
period_[i] = substr(put(date1, yymmddn8.),3,4);
end;
run;
Bart
@scb wrote:
When i run the program, period_1 appear in the date1 dataset, may i know how to output period_2 to period_24 into the date1 dataset? Thanks.
data date1;
date1 = intnx('month',&today,-1,'end');
period_1 = substr(put(date1, yymmddn8.),3,4);
do i=2 to 24;
call symput(cats("Period_",i),substr(put(intnx('month',date1,-(i-1),'end'),yymmddn8.),3,4));
end;
run;
May I recommend that you avoid macro variables and CALL SYMPUT when data step functions will do the job?
May I also recommend that you not store calendar information as characters strings like '1903'? Instead, they should be stored as actual numeric SAS date values and formatted as needed.
data date1;
array period_(24);
today=today();
do i=1 to 24;
period(i) = intnx('month',today,-i,'e');
end;
format period_: yymmn4.;
drop i today;
run;
It's always a bit difficult to really provide an answer just based on code posted but without any sample data (like Have and Want).
Based on your code: Macro level code is great for dynamic code but it's really not the appropriate design to just hold data. Even if things work as desired it will lead to a lot of complicated and unnecessary macro coding.
Available on demand!
Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.
Learn how use the CAT functions in SAS to join values from multiple variables into a single value.
Find more tutorials on the SAS Users YouTube channel.