- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
I can't figure out what I'm doing wrong:
%macro test();
%if "06DEC2021"d<"05JAN2022"d
%then %do;
%put "Lesser";
%end;
%else %do;
%put "Why doesn't it work?";
%end;
%mend;
%test();
How are you supposed to compare dates?
Thanks
Accepted Solutions
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
The macro processor does not recognize dates as dates, it recognizes them as text strings. And text strings sort alphabetically, so 05JAN2022 is less than (alphabetically) 06DEC2021.
To compare dates, use either a SAS data step, or convert the dates to SAS date values which are integers which can then be compared by the SAS macro processor effectively.
data example;
length value $ 7;
if '06DEC2021'd < '05JAN2022'd then value='Lesser';
else value='Greater';
run;
or
%let abc=%sysevalf("05JAN2022"d);
%let def=%sysevalf("06DEC2021"d);
%if &abc<&def %then %do; %put LESSER; %end;
%else %do; %put GREATER; %end;
Paige Miller
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
The macro processor does not recognize dates as dates, it recognizes them as text strings. And text strings sort alphabetically, so 05JAN2022 is less than (alphabetically) 06DEC2021.
To compare dates, use either a SAS data step, or convert the dates to SAS date values which are integers which can then be compared by the SAS macro processor effectively.
data example;
length value $ 7;
if '06DEC2021'd < '05JAN2022'd then value='Lesser';
else value='Greater';
run;
or
%let abc=%sysevalf("05JAN2022"d);
%let def=%sysevalf("06DEC2021"d);
%if &abc<&def %then %do; %put LESSER; %end;
%else %do; %put GREATER; %end;
Paige Miller
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
Hi @PaigeMiller ,
Thanks, I guess it explains why it's not working. Unfortunately for me the sysevalf also does not work (output is GREATER):
%macro test();
%let abc=%sysevalf("05JAN2022"d);
%let def=%sysevalf("06DEC2021"d);
%if &abc<&def %then %do; %put LESSER; %end;
%else %do; %put GREATER; %end;
%mend;
%test();
any other ideas on how to compare dates in a macro?
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
Ha! So it is. Apologies and thanks again.
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
@python_user wrote:
Hi @PaigeMiller ,
Thanks, I guess it explains why it's not working. Unfortunately for me the sysevalf also does not work (output is GREATER):
%macro test();
%let abc=%sysevalf("05JAN2022"d);
%let def=%sysevalf("06DEC2021"d);
%if &abc<&def %then %do; %put LESSER; %end;
%else %do; %put GREATER; %end;
%mend;%test();
any other ideas on how to compare dates in a macro?
Look back at the direction of the comparison and your values. "05JAN2022"d is greater than (after) "06DEC2021"d .
Run the macro with options Symbolgen and MLOGIC
options symbolgen mlogic; %test(); options nosymbolgen nomlogic;
123 options symbolgen mlogic; 124 %test(); MLOGIC(TEST): Beginning execution. MLOGIC(TEST): %LET (variable name is ABC) MLOGIC(TEST): %LET (variable name is DEF) SYMBOLGEN: Macro variable ABC resolves to 22650 SYMBOLGEN: Macro variable DEF resolves to 22620 MLOGIC(TEST): %IF condition &abc<&def is FALSE MLOGIC(TEST): %PUT GREATER GREATER MLOGIC(TEST): Ending execution. 125 options nosymbolgen nomlogic;
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
Yes, good point @ballardw
Actually looking at the values of your macro variables is always an excellent debugging strategy, and I'm surprised when people don't do that.
Paige Miller