cancel
Showing results for 
Search instead for 
Did you mean: 

Trouble comparing date macros in a macro

SOLVED
python_user
Fluorite | Level 6
Solved!

Trouble comparing date macros in a macro

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
Solution

Re: Trouble comparing date macros in a macro

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
Solution

Re: Trouble comparing date macros in a macro

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

Re: Trouble comparing date macros in a macro

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

Re: Trouble comparing date macros in a macro

@python_user 

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

--
Paige Miller
python_user
Fluorite | Level 6

Re: Trouble comparing date macros in a macro

Ha! So it is. Apologies and thanks again.

ballardw
Super User

Re: Trouble comparing date macros in a macro


@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

Re: Trouble comparing date macros in a macro

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