DATA Step, Macro, Functions and more

Dates and Macros

Reply
Frequent Contributor
Posts: 95

Dates and Macros

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
Frequent Contributor
Frequent Contributor
Posts: 76

Re: Dates and Macros

%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
Frequent Contributor
Posts: 95

Re: Dates and Macros

Thanks for the information and the solution. I was assuming the date format was being treated as a number by default.
Valued Guide
Posts: 2,175

Re: Dates and Macros

: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
Ask a Question
Discussion stats
  • 3 replies
  • 131 views
  • 0 likes
  • 3 in conversation