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

Can someone help write a macro do loop for the following scenario, please? Rather than doing it manually line-by-line, it would be great if this can be done using a macro. Thanks

 

%Let M0=Start_Date+28;
%Let M1=Start_Date+56;
%Let M2=Start_Date+84;
%Let M3=Start_Date+112;
%Let M4=Start_Date+140;
%Let M5=Start_Date+168;
%Let M6=Start_Date+196;
%Let M7=Start_Date+224;
%Let M8=Start_Date+252;
%Let M9=Start_Date+280;
%Let M10=Start_Date+308;
%Let M11=Start_Date+336;
%Let M12=Start_Date+364;
%Let M13=Start_Date+392;
%Let M14=Start_Date+420;
%Let M15=Start_Date+448;
%Let M16=Start_Date+476;
%Let M17=Start_Date+504;
%Let M18=Start_Date+532;
%Let M19=Start_Date+560;
%Let M20=Start_Date+588;

1 ACCEPTED SOLUTION

Accepted Solutions
Astounding
PROC Star

While I'm not certain that the result you describe is the result that you want, it is relatively straightforward to produce:

 

%macro loop28;
   %local i;
   %do i=0 %to 20;
      %global m&i;
      %let m&i = Start_Date+%eval(28 + 28*&i);
   %end;
%mend loop28;
%loop28

View solution in original post

3 REPLIES 3
Astounding
PROC Star

While I'm not certain that the result you describe is the result that you want, it is relatively straightforward to produce:

 

%macro loop28;
   %local i;
   %do i=0 %to 20;
      %global m&i;
      %let m&i = Start_Date+%eval(28 + 28*&i);
   %end;
%mend loop28;
%loop28
saweheh
Fluorite | Level 6

Thank you so much. This is exactly what I need.

jimbarbour
Meteorite | Level 14

@saweheh,

 

Is your 28 day increment intended to be one month?  I assume it is.  You might want to use the "sameday" option of function INTNX which will be a lot more precise.  I've written a macro to just that which I will include below.  I've written another macro to write the results to the log.  In the log, we can see that I'm incrementing a full month each time, regardless of whether a month is 28, 29, 30, or 31 days long.  Such is the power of INTNX.

 

Jim

 

%LET	Start_Date	=	'01JAN2021'd;

**------------------------------------------------------------------------------**;

%MACRO	Months_Loop(Start_Date, Nbr_of_Months=20);
	%LOCAL	i  j  Temp_Date;
	%GLOBAL	M0;

	%LET	M0					=	%QSYSFUNC(STRIP(%QSYSFUNC(PUTN(&Start_Date, 9.))));

	%DO		i					=	1	%TO	&Nbr_Of_Months;
		%GLOBAL	M&i;
		%LET	j				=	%EVAL(&i - 1);
		%LET	M&i	=	%QSYSFUNC(INTNX(month,&&M&j,1,sameday));
	%END;
%MEND	Months_Loop;

%Months_Loop(&Start_Date);

**------------------------------------------------------------------------------**;

%MACRO	Display_Months(Nbr_of_Months=20);
	%LOCAL	i;
	%LOCAL	Formatted_Date;

	%DO		i					=	0	%TO	&Nbr_of_Months;
		%LET	Formatted_Date	=	%QSYSFUNC(PUTN(&&M&i, DATE9.));
		%PUT	NOTE:  Month &i =	&&M&i (&Formatted_Date);
	%END;
%MEND	Display_Months;

%Display_Months;

Results:

NOTE:  Month 0 = 22281 (01JAN2021)
NOTE:  Month 1 = 22312 (01FEB2021)
NOTE:  Month 2 = 22340 (01MAR2021)
NOTE:  Month 3 = 22371 (01APR2021)
NOTE:  Month 4 = 22401 (01MAY2021)
NOTE:  Month 5 = 22432 (01JUN2021)
NOTE:  Month 6 = 22462 (01JUL2021)
NOTE:  Month 7 = 22493 (01AUG2021)
NOTE:  Month 8 = 22524 (01SEP2021)
NOTE:  Month 9 = 22554 (01OCT2021)
NOTE:  Month 10 = 22585 (01NOV2021)
NOTE:  Month 11 = 22615 (01DEC2021)
NOTE:  Month 12 = 22646 (01JAN2022)
NOTE:  Month 13 = 22677 (01FEB2022)
NOTE:  Month 14 = 22705 (01MAR2022)
NOTE:  Month 15 = 22736 (01APR2022)
NOTE:  Month 16 = 22766 (01MAY2022)
NOTE:  Month 17 = 22797 (01JUN2022)
NOTE:  Month 18 = 22827 (01JUL2022)
NOTE:  Month 19 = 22858 (01AUG2022)
NOTE:  Month 20 = 22889 (01SEP2022)

 

 

hackathon24-white-horiz.png

2025 SAS Hackathon: There is still time!

Good news: We've extended SAS Hackathon registration until Sept. 12, so you still have time to be part of our biggest event yet – our five-year anniversary!

Register Now

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
  • 935 views
  • 2 likes
  • 3 in conversation