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.
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;
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;
Thank you very much Astounding , your solution worked perfect.
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.
SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!
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.
Ready to level-up your skills? Choose your own adventure.