Hello,
I would like to do a do loop using two marcro variables
%let startdate=202501;
%let enddate=202602;
loop:
202501
202502
202503
202504
202505
202506
202507
202508
202509
202510
202511
202512
202601
202602
I find it easier (and generally a good practice) to work with actual SAS dates and let the INTNX function determine the next month. SAS has done the hard work to determine what the next month is, so you don't have to. INTNX has to work with actual SAS dates, not dates that look like dates to humans but are not SAS dates such as 202501. So first I convert the human readable dates to actual SAS dates via the INPUTN function resulting in macro variables &startdate1 and &enddate1.
%let startdate=202501;
%let enddate=202602;
%let startdate1=%sysfunc(inputn(&startdate,yymmn6.));
%let enddate1=%sysfunc(inputn(&enddate,yymmn6.));
%macro dothis;
%let thismonth=&startdate1;
%do %while(&thismonth<=&enddate1);
%put %sysfunc(putn(&thismonth,yymmn6.));
%let thismonth=%sysfunc(intnx(month,&thismonth,1,b));
%end;
%mend;
%dothis
I find it easier (and generally a good practice) to work with actual SAS dates and let the INTNX function determine the next month. SAS has done the hard work to determine what the next month is, so you don't have to. INTNX has to work with actual SAS dates, not dates that look like dates to humans but are not SAS dates such as 202501. So first I convert the human readable dates to actual SAS dates via the INPUTN function resulting in macro variables &startdate1 and &enddate1.
%let startdate=202501;
%let enddate=202602;
%let startdate1=%sysfunc(inputn(&startdate,yymmn6.));
%let enddate1=%sysfunc(inputn(&enddate,yymmn6.));
%macro dothis;
%let thismonth=&startdate1;
%do %while(&thismonth<=&enddate1);
%put %sysfunc(putn(&thismonth,yymmn6.));
%let thismonth=%sysfunc(intnx(month,&thismonth,1,b));
%end;
%mend;
%dothis
Nearly 200 sessions are now available on demand with the SAS Innovate Digital Pass.
Explore Now →Check out this tutorial series to learn how to build your own steps in SAS Studio.
Find more tutorials on the SAS Users YouTube channel.
Ready to level-up your skills? Choose your own adventure.