SAS Programming

DATA Step, Macro, Functions and more
BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
python_user
Fluorite | Level 6

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

1 ACCEPTED SOLUTION

Accepted Solutions
PaigeMiller
Diamond | Level 26

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

View solution in original post

6 REPLIES 6
PaigeMiller
Diamond | Level 26

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
python_user
Fluorite | Level 6

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?

PaigeMiller
Diamond | Level 26

@python_user 

GREATER is the correct answer, at least on my calendar.

--
Paige Miller
python_user
Fluorite | Level 6

Ha! So it is. Apologies and thanks again.

ballardw
Super User

@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;
PaigeMiller
Diamond | Level 26

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

sas-innovate-white.png

Register Today!

Join us for SAS Innovate 2025, our biggest and most exciting global event of the year, in Orlando, FL, from May 6-9.

 

Early bird rate extended! Save $200 when you sign up by March 31.

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
  • 6 replies
  • 2302 views
  • 0 likes
  • 3 in conversation