Hello
Lets say that user define in macro variable mon12 as 2211 (In structure YYMM)
%let mon12=2211;
My question-
What is the way to calculate the other macro varaibles
mon11
mon10
mon9
mon8
mon7
mon6
mon5
mon4
mon3
mon2
mon1
%let mon12=2211;
%let mon11=2210;
%let mon10=2209;
%let mon9=2208;
%let mon8=2207;
%let mon7=2206;
%let mon6=2205;
%let mon5=2204;
%let mon4=2203;
%let mon3=2202;
%let mon2=2201;
%let mon1=2112;
Other way then this way?
%let month=2211;/***חודש עבורו מחושבת הכנסה פנויה***/
data temp(DROP= YYMM date_YYMM);
YYMM="&month.";
date_YYMM=mdy(mod(YYMM,100),1,floor(YYMM/100));
mon12="&step2_mon12.";
mon11=put(intnx('Month',date_YYMM,-1,'s'),yymmn4.);
mon10=put(intnx('Month',date_YYMM,-2,'s'),yymmn4.);
mon9=put(intnx('Month',date_YYMM,-3,'s'),yymmn4.);
mon8=put(intnx('Month',date_YYMM,-4,'s'),yymmn4.);
mon7=put(intnx('Month',date_YYMM,-5,'s'),yymmn4.);
mon6=put(intnx('Month',date_YYMM,-6,'s'),yymmn4.);
mon5=put(intnx('Month',date_YYMM,-7,'s'),yymmn4.);
mon4=put(intnx('Month',date_YYMM,-8,'s'),yymmn4.);
mon3=put(intnx('Month',date_YYMM,-9,'s'),yymmn4.);
mon2=put(intnx('Month',date_YYMM,-10,'s'),yymmn4.);
mon1=put(intnx('Month',date_YYMM,-11,'s'),yymmn4.);
call symputx('mon12',mon12);
call symputx('mon11',mon11);
call symputx('mon10',mon10);
call symputx('mon9',mon9);
call symputx('mon8',mon8);
call symputx('mon7',mon7);
call symputx('mon6',mon6);
call symputx('mon5',mon5);
call symputx('mon4',mon4);
call symputx('mon3',mon3);
call symputx('mon2',mon2);
call symputx('mon1',mon1);
Run;
%put mon12=&mon12;
%put mon11=&mon11;
%put mon10=&mon10;
%put mon9=&mon9;
%put mon8=&mon8;
%put mon7=&mon7;
%put mon6=&mon6;
%put mon5=&mon5;
%put mon4=&mon4;
%put mon3=&mon3;
%put mon2=&mon2;
%put mon2=&mon1;
You could use a DO loop in a DATA step, or a %DO loop inside a macro.
Why would you need so many macro variables? Couldn't you just calculate the string you want based on the starting date and the month offset?
Anyway inside a macro (so you can use %DO loop) you could run code like this.
First convert your MON12 macro variable with the YYMM string into a baseline date value.
%let date0=%sysfunc(intnx(month,%sysfunc(inputn(&mon12,yymmn.)),-12));
Now you can use a %DO loop to generate the other 11 macro variables from that base date. Make sure to make them GLOBAL if you need to use them after the macro ends.
%do offset=1 %to 11;
%if not %symexist(mon&offset) %then %global mon&offset;
%let mon&offset=%sysfunc(intnx(month,&date0,&offset),yymmn4.);
%end;
How about start using the macroArray package:
Documentation is here.
Article describing the package is here from PharmaSUG (and here) and WUSS conferences.
Example:
%loadPackage(macroArray)
/*
%helpPackage(macroArray,'%array()')
*/
resetline;
/* create */
%array(mon[12] $ 4, function=put(intnx('month','1dec2021'd,_I_-1,"S"),yymmdd4.), macarray=Y)
/* use */
%put %do_over(mon);
%put %mon(1), %mon(2) ... %mon(12);
/* delete */
%deleteMacArray(mon, macarray=Y)
Log:
1
2 /* create */
3 %array(mon[12] $ 4, function=put(intnx('month','1dec2021'd,_I_-1,"S"),yymmdd4.), macarray=Y)
NOTE:[ARRAY] 12 macrovariables created
4
5
6 /* use */
7 %put %do_over(mon);
2112 2201 2202 2203 2204 2205 2206 2207 2208 2209 2210 2211
8
9 %put %mon(1), %mon(2) ... %mon(12);
2112, 2201 ... 2211
10
11
12 /* delete */
13 %deleteMacArray(mon, macarray=Y)
Bart
Loops.
Loops.
Loops.
Did I say "Loops!" already? Still, I'll say it again: "LOOPS!!!"
%let month=2211;
data temp;
date = input("&month.",yymmn4.);
format date yymmn6.;
do i = 12 to 1 by -1;
date_yymm = put(date,yymmn4.);
call symputx(cats("mon",i),date_yymm);
output;
date = intnx("month",date,-1);
end;
drop i;
run;
%macro show;
%do i = 12 %to 1 %by -1;
%put mon&i=&&mon&i.;
%end;
%mend;
%show
Whenever you see something repetitive, think "loop!".
Registration is now open for SAS Innovate 2025 , our biggest and most exciting global event of the year! Join us in Orlando, FL, May 6-9.
Sign up by Dec. 31 to get the 2024 rate of just $495.
Register now!
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.