Hello
User define 2 macro varaibles: Current month (YYMM format) and number of months.
For example :
%let end=1808;
%let n=24;
The task is to create a data set that will have one column called :YYMM and will have the values (char values):
1808
1807
1806
.......
........
........
1608 (because it 24 months back)
Good solution:
%let end=1808;
%let n=24;
data dates_tbl;
do i=0 to &n. ;
YYMM=MDY(substr(compress(&end.),3,2)*1,1,substr(compress(&end.),1,2)*1);
YYMM2=intnx('month',YYMM,-i) ;
YYMM2b=put(YYMM2,yymmn4.);
YYMM3=intnx('month',YYMM2,-12,'end') ;
var_name='t'||strip(&n.-i);
call symputx (var_name,YYMM3);
var_name2='m'||strip(&n.-i);
call symputx (var_name2,YYMM2b);
format YYMM YYMM2 YYMMN4. YYMM3 date9.;
output;
end;
run;
%put &t0;
%put &t1;
%put &t2;
%put &t24;
%put &m0;
%put &m1;
%put &m2;
%put &m24;
Seems to be related to
The solution seems to be in Create date parameters already. Use the datastep suggested by @RW9, create a dataset instead of macro-variables.
Good solution:
%let end=1808;
%let n=24;
data dates_tbl;
do i=0 to &n. ;
YYMM=MDY(substr(compress(&end.),3,2)*1,1,substr(compress(&end.),1,2)*1);
YYMM2=intnx('month',YYMM,-i) ;
YYMM2b=put(YYMM2,yymmn4.);
YYMM3=intnx('month',YYMM2,-12,'end') ;
var_name='t'||strip(&n.-i);
call symputx (var_name,YYMM3);
var_name2='m'||strip(&n.-i);
call symputx (var_name2,YYMM2b);
format YYMM YYMM2 YYMMN4. YYMM3 date9.;
output;
end;
run;
%put &t0;
%put &t1;
%put &t2;
%put &t24;
%put &m0;
%put &m1;
%put &m2;
%put &m24;
%let end=1808;
%let n=24;
data have;
do i=0 to &n by 1;
do yymm=intnx('month',input(cats(&end,'01'),yymmdd6.),-i);
format yymm yymmn4.;
output;
end;
end;
run;
I see that your code is working well but why do you have to Do loops?
Is it not better to have one Do loop?
%let end=1808;
%let n=24;
data have;
do i=0 to &n by 1;
yymm=intnx('month',input(cats(&end,'01'),yymmdd6.),-i);
format yymm yymmn4.;
output;
end;
run;
Good news: We've extended SAS Hackathon registration until Sept. 12, so you still have time to be part of our biggest event yet – our five-year anniversary!
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.
Ready to level-up your skills? Choose your own adventure.