DATA Step, Macro, Functions and more

%IF Statement and Macro resolution

Accepted Solution Solved
Reply
Occasional Contributor
Posts: 7
Accepted Solution

%IF Statement and Macro resolution

[ Edited ]

Hello

i have a code which needs to conditionaly import an text file. dates are dynamic and cant be hardcoded, we import them from an excel file and add them to a macro variable, which we use to conditionaly import a text file. but i am having issues with open if condition. if the same condition is passes in a data step, i am able to resolve it with out any issues.

 

PROC IMPORT OUT= dates_file DATAFILE= "D:\test\dates.xls" DBMS=xls REPLACE;

RUN;

proc sql ;

select strip(start_dt) into :stdt from operational where start_dt ne '' ;

select strip(end_dt) into :endt from operational where end_dt ne '' ;

quit;

 

%let d_today=%sysfunc(putn("&sysdate9"d,5.));

%let start_dt1=%sysfunc(inputn(&stdt,mmddyy10.));

%let end_dt1=%sysfunc(inputn(&endt,mmddyy10.));

%put &d_today start_dt1 end_dt1;

 

%macro test;

%if &start_dt1 <= &d_today <= &end_dt1 %then %do;

data comp;

infile "D:\Extract.txt" delimiter= '~' MISSOVER DSD lrecl=32767 firstobs=2 ;

informat name $20. ;

informat race $20. ;

informat sex $10. ;

informat age best32. ;

format name $20. ;

format race $20. ;

format sex $10. ;

format age best12. ;

input name $ race $ sex $ age;

run;

%end;

%mend test;

%test;

 

i have been working on this code and if condition does not work as intended, i am really confused what the underlying issue, is it the date macro variables being not resolved properly or is it the open if condition.

 

 

if condition is always true even thogh date is out of range.

 

SYMBOLGEN: Macro variable START_DT1 resolves to 20645

SYMBOLGEN: Macro variable D_TODAY resolves to 20612

SYMBOLGEN: Macro variable END_DT1 resolves to 21375

 

MLOGIC(TEST): %IF condition &start_dt1 <= &d_today <= &end_dt1 is TRUE

 

same step when called from a data step it works fine.

 

 


Accepted Solutions
Solution
‎06-07-2016 03:16 PM
Super User
Posts: 5,518

Re: %IF Statement and Macro resolution

This comparison is not working the way you expect:

 

%if &start_dt1 <= &d_today <= &end_dt1 %then %do;

 

Macro language evaluates from left to right, as if you had parentheses in place:

 

%if (&start_dt1 <= &d_today) <= &end_dt1 %then %do;

 

The first comparison returns a 0 (if false) or a 1 (if true) which then gets compared to &end_dt1.  Just split the comparison into two comparisons and that should take care of the problem:

 

%if &start_dt1 <= &d_today and &d_today <= &end_dt1 %then %do;

View solution in original post


All Replies
Solution
‎06-07-2016 03:16 PM
Super User
Posts: 5,518

Re: %IF Statement and Macro resolution

This comparison is not working the way you expect:

 

%if &start_dt1 <= &d_today <= &end_dt1 %then %do;

 

Macro language evaluates from left to right, as if you had parentheses in place:

 

%if (&start_dt1 <= &d_today) <= &end_dt1 %then %do;

 

The first comparison returns a 0 (if false) or a 1 (if true) which then gets compared to &end_dt1.  Just split the comparison into two comparisons and that should take care of the problem:

 

%if &start_dt1 <= &d_today and &d_today <= &end_dt1 %then %do;

Occasional Contributor
Posts: 7

Re: %IF Statement and Macro resolution

Posted in reply to Astounding

Thank you very much , your solution worked perfect.

Regular Contributor
Posts: 175

Re: %IF Statement and Macro resolution

I'd change it to

%IF condition &start_dt1 <= &d_today and &d_today <= &end_dt1 %then

 

I think it's doing (&start_dt1 <= &d_today) first, which is 0 (false), and 0 is of course <= &end_dt1.

 

Trying on a simple example (%IF condition 2 <= 3 <= 1 %then ) versus (%IF condition 2 <= 3 and 3 <= 1 %then ) verifies that this is what is happening; the first is "true" due to the order of operations.

Occasional Contributor
Posts: 7

Re: %IF Statement and Macro resolution

Posted in reply to paulkaefer
Thank you paulkaefer, As you and Astounding pointed out, its considering if conditions as if there were parentheses.
☑ This topic is solved.

Need further help from the community? Please ask a new question.

Discussion stats
  • 4 replies
  • 286 views
  • 5 likes
  • 3 in conversation