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
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;
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;
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?
Ha! So it is. Apologies and thanks again.
@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;
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.
Available on demand!
Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.
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.
Select SAS Training centers are offering in-person courses. View upcoming courses for: