BookmarkSubscribeRSS Feed
1162
Calcite | Level 5
I'm trying to convert this data step into a macro. I can't figure out what I need to do to increment a date value in a macro.

This works:
[pre]
data _null_;
i = '01Oct2007'd;
do until (i >= '31Dec2007'd);
i = i + 7;
put i date9.;
end;
run;
[/pre]

This doesn't:
[pre]
%macro loop;
%let i = '01Oct2007'd;
%do %until (&i >= '31Dec2007'd);
%let i = %eval(&i + 7);
%put &i;
%end;
%mend loop;
%loop;
[/pre]

I'm pretty sure the problem is with the %eval part, but I'm not sure what the correct syntax should be.

Thanks in advance for your help
3 REPLIES 3
LAP
Quartz | Level 8 LAP
Quartz | Level 8
%macro loop;
%let i = %sysfunc(putn('01Oct2007'd,8.));
%do %until (&i >= %sysfunc(putn('31Dec2007'd,8.)));
%let i = %eval(&i + 7);
%put %sysfunc(Putn(&i,date9.));
%end;

%mend loop;
%loop;

The trick here is knowing that macro variables are strings. So in the 1st %let
statement i was litterally '01Oct2007'd instead of the numeric value 17440
Therefore adding 7 to i would not work. By converting all your dates to the numeric equivalent with %sysfunc, you are able to increment i. To write the date back out in the date format, you must convert in back to the text string .

Hope this helps
Linda
1162
Calcite | Level 5
Thanks for the information and the solution. I was assuming the date format was being treated as a number by default.
Peter_C
Rhodochrosite | Level 12
:If you want a macro to treat a date constant as the corresponding number-of-days-since 1960, use the %sysevalf() like [pre]
%if %sysevalf( %&sysdate9"d ) gt %sysevalf( "&my_date"d ) %then %do; [/pre]


Good Luck

PeterC
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
  • 1430 views
  • 0 likes
  • 3 in conversation