Hello
User define current month (1808) and number of months back (12).
SAS should create parameters:
m24=1808
m23=1807
......
.....
m1=1709
m0=1708
I get an error in my code.
I want to repair my code please
%let end=1808;
%let n=12;
%let end_date=%sysfunc(inputn(&end.,yymmn4.));
%put &end_date.;
%macro months;
%do i=&n. %to i=0 %by -1 ;
k=%eval(&i.-&n.);
t&i.=intnx('month',&end_date.,-&k.,'end');
call symput("t&i",t&i.);
%end;
%mend;
data month_score1(drop=k);
%months;
run;
%macro mon;
%do i=&n. %to i=0 %by -1;
k=%eval(&i.-&n.);
m&i.=put(&&t&i.,yymmn5.);
call symput("m&i",trim(left(m&i.)));
%end;
%mend;
data month_score2 (drop=k);
%mon;
run;
%put &m0.;
%put &&m&n..;
Why? Putting date information into macro variables is really not the best way forward regardless of your process. Macro is nothing more than a text generation system. Do not use it to replace base SAS.
%let end=1808;
%let n=12;
data _null_;
end=input(cats("20","&end.","01"),yymmdd10.);
j=0;
do i=0 to -&n. by -1;
call symputx(cats("m",put(j,best.)),put(intnx('month',end,i),yymmn4.));
j=j+1;
end;
run;
%put &m0.;
%put &m1.;
%put &m2.;
%put &m3.;
%put &m4.;
%put &m5.;
See the above example, and compare it to the mess of macro you have created, Base SAS is the programming language, use it. Although I wouldn't even be doing this in the first place as you have a formula and a base point so there is no value creating macro variables for each item.
Thank you.
I run your code but: m12 should be 1808 and in your code m0 is 1808
Change:
j=12;
do i=0 to -&n. by -1;
call symputx(cats("m",put(j,best.)),put(intnx('month',end,i),yymmn4.));
j=j-1;
end;
Perfect and thank you!
May you please look again in my code and tell me specific what is wrong here?
%let end=1808;
%let n=12;
%let end_date=%sysfunc(inputn(&end.,yymmn4.));
%put &end_date.;
%macro months;
%do i=&n. %to i=0 %by -1 ;
k=%eval(&i.-&n.);
t&i.=intnx('month',&end_date.,-&k.,'end');
call symput("t&i",t&i.);
%end;
%mend;
data month_score1(drop=k);
%months;
run;
Its because you are mixing up what macro is and what Base SAS programming is. I really cannot be bothered to go over it again.
k=%eval(&i.-&n.);
t&i.=intnx('month',&end_date.,-&k.,'end');
Can you not see the issue from these two lines? Start by do it all in Base SAS - this is the programming language. There is no benefit, or need to keep pushing all your coding into Macro.
This is also working very well.
The problem was that I need to define k as parameter with %let
%let end=1808;
%let n=12;
%let end_date=%sysfunc(inputn(&end.,yymmn4.));
%put &end_date.;
%macro months;
%do i=&n. %to i=0 %by -1 ;
%let k=%eval(&i.-&n.);
t&i.=intnx('month',&end_date.,&k.,'end');
call symput("t&i",t&i.);
%end;
%mend;
data month_score1;
%months;
run;
%macro mon;
%do i=&n. %to i=0 %by -1;
%let k=%eval(&i.-&n.);
m&i.=put(&&t&i.,yymmn5.);
call symput("m&i",trim(left(m&i.)));
%end;
%mend;
data month_score2 ;
%mon;
run;
%put &m0.;
%put &&m&n..;
It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.
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.