BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
wzca74
Calcite | Level 5

I was trying to find the difference between the two conditions below, the top one is running fine with TRUE result, but the bottom one always gave me a FALSE  result

 

%let open_begin = '01APR2017'd;
%let open_end = '30JUN2017'd;

 

.../*top one*/

%else %if (&open_begin) ~=    and &open_begin <= &open_end  %then  %do;

&open_begin <= OPENDATE <= &open_end

%end;

...

 

vs.

 

.../*bottom one*/

 

%else %if &open_begin > '01JAN1960'd and &open_end > '01JAN1960'd and &open_begin <= &open_end  %then %do;

&open_begin <= OPENDATE <= &open_end

%end;

 

Any thoughts? Thanks.

 

1 ACCEPTED SOLUTION

Accepted Solutions
FreelanceReinh
Jade | Level 19

Hello @wzca74 and welcome to the SAS Support Communities!

 

Basically, macro variables contain text. (Nevertheless, integer arithmetic can be done: see "How the Macro Processor Evaluates Arithmetic Expressions".) In particular, your date literals ('01APR2017'd etc.) are in fact treated as character strings.

 

Hence, the first comparison ("top one") yields TRUE accidentally because '0... is alphabetically smaller than '3... .

The "bottom one" yields FALSE because '01A... is alphabetically not greater than '01J... (first inequality).

 

To make those comparisons work, use the %SYSEVALF function, e.g.

... and %sysevalf(&open_begin <= &open_end) %then ...

And don't worry, you're not the first who had this problem: %If statement using dates.

 

View solution in original post

4 REPLIES 4
FreelanceReinh
Jade | Level 19

Hello @wzca74 and welcome to the SAS Support Communities!

 

Basically, macro variables contain text. (Nevertheless, integer arithmetic can be done: see "How the Macro Processor Evaluates Arithmetic Expressions".) In particular, your date literals ('01APR2017'd etc.) are in fact treated as character strings.

 

Hence, the first comparison ("top one") yields TRUE accidentally because '0... is alphabetically smaller than '3... .

The "bottom one" yields FALSE because '01A... is alphabetically not greater than '01J... (first inequality).

 

To make those comparisons work, use the %SYSEVALF function, e.g.

... and %sysevalf(&open_begin <= &open_end) %then ...

And don't worry, you're not the first who had this problem: %If statement using dates.

 

wzca74
Calcite | Level 5

Thanks 

Kurt_Bremser
Super User

The macro language is a code generator, which only knows the datatype text, and cannot do calculations on its own, with the exception of integer arithmetic in %if and %do. Therefore the date literals are treated as strings in comparisons.

'01A is always "smaller" than '01J (in the collating sequence).

If you follow Maxim 28 and use raw values in the macro variables

%let open_begin = %sysfunc(inputn(20170401,yymmdd8.));

the comparisons will work as expected.

wzca74
Calcite | Level 5

Thanks

Ready to join fellow brilliant minds for the SAS Hackathon?

Build your skills. Make connections. Enjoy creative freedom. Maybe change the world. Registration is now open through August 30th. Visit the SAS Hackathon homepage.

Register today!
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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 4 replies
  • 1686 views
  • 1 like
  • 3 in conversation