BookmarkSubscribeRSS Feed
scb
Obsidian | Level 7 scb
Obsidian | Level 7

The below codes work perfectly to generate the desired output; may I know how to use loop to generate the same output; as I might have 30 months instead of 8 months.

 

Anyone can help? Thanks.

 


%LET RUNDATE = "01APR2018"D;

 

DATA _NULL_;
IF MONTH(&RUNDATE) GE 4 AND MONTH(&RUNDATE) LE 12 THEN DO;
CALL SYMPUT("MONTH1",PUT(INTNX('MONTH',&RUNDATE,-(MONTH(&RUNDATE))+4),YYMMN6.));
CALL SYMPUT("MONTH2",PUT(INTNX('MONTH',&RUNDATE,-(MONTH(&RUNDATE))+5),YYMMN6.));
CALL SYMPUT("MONTH3",PUT(INTNX('MONTH',&RUNDATE,-(MONTH(&RUNDATE))+6),YYMMN6.));
CALL SYMPUT("MONTH4",PUT(INTNX('MONTH',&RUNDATE,-(MONTH(&RUNDATE))+7),YYMMN6.));
CALL SYMPUT("MONTH5",PUT(INTNX('MONTH',&RUNDATE,-(MONTH(&RUNDATE))+8),YYMMN6.));
CALL SYMPUT("MONTH6",PUT(INTNX('MONTH',&RUNDATE,-(MONTH(&RUNDATE))+9),YYMMN6.));
CALL SYMPUT("MONTH7",PUT(INTNX('MONTH',&RUNDATE,-(MONTH(&RUNDATE))+10),YYMMN6.));
CALL SYMPUT("MONTH8",PUT(INTNX('MONTH',&RUNDATE,-(MONTH(&RUNDATE))+11),YYMMN6.));
END;

 

IF MONTH(&RUNDATE) GE 1 AND MONTH(&RUNDATE) LE 3 THEN DO;
CALL SYMPUT("MONTH1",PUT(INTNX('MONTH',&RUNDATE,-(MONTH(&RUNDATE))-4),YYMMN6.));
CALL SYMPUT("MONTH2",PUT(INTNX('MONTH',&RUNDATE,-(MONTH(&RUNDATE))-3),YYMMN6.));
CALL SYMPUT("MONTH3",PUT(INTNX('MONTH',&RUNDATE,-(MONTH(&RUNDATE))-2),YYMMN6.));
CALL SYMPUT("MONTH4",PUT(INTNX('MONTH',&RUNDATE,-(MONTH(&RUNDATE))-1),YYMMN6.));
CALL SYMPUT("MONTH5",PUT(INTNX('MONTH',&RUNDATE,-(MONTH(&RUNDATE))-0),YYMMN6.));
CALL SYMPUT("MONTH6",PUT(INTNX('MONTH',&RUNDATE,-(MONTH(&RUNDATE))+1),YYMMN6.));
CALL SYMPUT("MONTH7",PUT(INTNX('MONTH',&RUNDATE,-(MONTH(&RUNDATE))+2),YYMMN6.));
CALL SYMPUT("MONTH8",PUT(INTNX('MONTH',&RUNDATE,-(MONTH(&RUNDATE))+3),YYMMN6.));
END;

RUN;

 

%PUT MONTH1 = &MONTH1;
%PUT MONTH2 = &MONTH2;
%PUT MONTH3 = &MONTH3;
%PUT MONTH4 = &MONTH4;
%PUT MONTH5 = &MONTH5;
%PUT MONTH6 = &MONTH6;
%PUT MONTH7 = &MONTH7;
%PUT MONTH8 = &MONTH8;

3 REPLIES 3
s_lassen
Meteorite | Level 14

Something like this?

%LET RUNDATE = "01APR2018"D;
 
DATA _NULL_;
if month(&rundate) ge 4 then
  add=3-month(&rundate);
else 
  add=-5-month(&rundate);
do i=1 to 8;
  call symput(cats("MONTH",i),put(intnx('MONTH',&RUNDATE,i+add),YYMMN6.));
end;
run;
 
%PUT MONTH1 = &MONTH1;
%PUT MONTH2 = &MONTH2;
%PUT MONTH3 = &MONTH3;
%PUT MONTH4 = &MONTH4;
%PUT MONTH5 = &MONTH5;
%PUT MONTH6 = &MONTH6;
%PUT MONTH7 = &MONTH7;
%PUT MONTH8 = &MONTH8;
RW9
Diamond | Level 26 RW9
Diamond | Level 26

I CANT READ SHOUTING!!

 

Why do you want to put date data into macro variables (which are text) thus making twice the work for yourself?  There are numerous options, you could use the fundamental concept of by group processing to do this, or call execute from a do loop etc.  Code like this is never going to be the best way to approach a problem.

But I have no doubt you will continue, so:

data _null_;
  if 4 <= month(&rundate.) <= 12 then do;
    do i=month(&rundate.) to 11;
      call symputx(cats('month',put(i,best.)),put(intnx('month',&rundate.,-(month(&rundate.))+i),yymmn6.));
    end;
  end;
if month <= 3 then do;
...;
end; run;
ballardw
Super User

It would likely help more in the long run to show how you are using those macro variables.

Quite often a large block of sequential macro variables indicates that either the process is suboptimal or that the data is poorly structured (a common result of importing spreadsheet data and not restructuring it for proper processing)

hackathon24-white-horiz.png

The 2025 SAS Hackathon has begun!

It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.

Latest Updates

How to Concatenate Values

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.

SAS Training: Just a Click Away

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

Browse our catalog!

Discussion stats
  • 3 replies
  • 2205 views
  • 0 likes
  • 4 in conversation