BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
Shradha1
Obsidian | Level 7

I am doing an analysis on data for 3 specific months: 202006, 202007, 202008.

And for each of these 3 months, I want a macro loop, which will run only for M-1 and M+6 month where M takes the above three months as values. The starting part of my macro is this:

 

%LET Mon_ST = 202006;			
%LET Mon_EN = 202008;			
Option obs = Max;			
			
%macro copy_data();			
			
%DO Mnth = &Mon_ST. %TO &Mon_EN. ;			
%IF %SUBSTR(&Mnth.,5,2) = 13 %THEN %LET Mnth =	%EVAL(%SUBSTR(&Mnth.,1,4)+1)01;	
%Put Mnth = &Mnth.;	

Now, I want to create another loop within this macro which will run for only two specific time periods for each of these months. For eaxample, when Mnth =202006, the loop will create another macro variable, say MON, which will first take value 202005 (Mnth-1) and on the next iteration will take value 202012 (Mnth+6). Similary, when Mnth =202007, MON will first be equal to 202006, and then 202101.

 

How do I create this sub loop? Any help will be much appreciated. Thanks!

1 ACCEPTED SOLUTION

Accepted Solutions
ChrisNZ
Tourmaline | Level 20

One way:

%let mon_st = 202006;			
%let mon_en = 202008;			
option obs = max;			
			
%macro copy_data();			
  %local mnth from to by m;
  %do mnth = &mon_st. %to &mon_en. ;			
    %if %substr(&mnth.,5,2) = 13 %then %let mnth = %eval(&mnth+100-12);	
    %put &=mnth;
    %let from= %sysfunc(intnx(month,%sysfunc(inputn(&mnth,yymmn6.)),-1),yymmn6.); 	
    %let to  = %sysfunc(intnx(month,%sysfunc(inputn(&mnth,yymmn6.)),+6),yymmn6.);
    %let by  = %eval(&to - &from);
    %do m=&from %to &to %by &by;
      %put &=m;
    %end;
  %end;
%mend;
%copy_data;

MNTH=202006
M=202005
M=202012
MNTH=202007
M=202006
M=202101
MNTH=202008
M=202007
M=202102

 

View solution in original post

1 REPLY 1
ChrisNZ
Tourmaline | Level 20

One way:

%let mon_st = 202006;			
%let mon_en = 202008;			
option obs = max;			
			
%macro copy_data();			
  %local mnth from to by m;
  %do mnth = &mon_st. %to &mon_en. ;			
    %if %substr(&mnth.,5,2) = 13 %then %let mnth = %eval(&mnth+100-12);	
    %put &=mnth;
    %let from= %sysfunc(intnx(month,%sysfunc(inputn(&mnth,yymmn6.)),-1),yymmn6.); 	
    %let to  = %sysfunc(intnx(month,%sysfunc(inputn(&mnth,yymmn6.)),+6),yymmn6.);
    %let by  = %eval(&to - &from);
    %do m=&from %to &to %by &by;
      %put &=m;
    %end;
  %end;
%mend;
%copy_data;

MNTH=202006
M=202005
M=202012
MNTH=202007
M=202006
M=202101
MNTH=202008
M=202007
M=202102

 

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
  • 1 reply
  • 634 views
  • 0 likes
  • 2 in conversation