BookmarkSubscribeRSS Feed
☑ This topic is solved. Need further help from the community? Please sign in and ask a new question.
alepage
Barite | Level 11

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

 

 

 

1 ACCEPTED SOLUTION

Accepted Solutions
PaigeMiller
Diamond | Level 26

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

 

--
Paige Miller

View solution in original post

1 REPLY 1
PaigeMiller
Diamond | Level 26

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

 

--
Paige Miller

Catch up on SAS Innovate 2026

Nearly 200 sessions are now available on demand with the SAS Innovate Digital Pass.

Explore Now →
Creating Custom Steps in SAS Studio

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 1 reply
  • 647 views
  • 3 likes
  • 2 in conversation