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

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.

 

 

1 ACCEPTED SOLUTION

Accepted Solutions
Astounding
PROC Star

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

4 REPLIES 4
Astounding
PROC Star

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;

sheru
Fluorite | Level 6

Thank you very much , your solution worked perfect.

paulkaefer
Lapis Lazuli | Level 10

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.

sheru
Fluorite | Level 6
Thank you paulkaefer, As you and Astounding pointed out, its considering if conditions as if there were parentheses.

SAS Innovate 2025: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

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
  • 4 replies
  • 1243 views
  • 5 likes
  • 3 in conversation