I'm creating macro variables to store dates.
I want these dates to be set to certain days if the code is run on Monday morning. If the code isn't run on Monday morning, I want the dates to be set to different days.
I thought I was doing this correctly with my code below. When I run my code, my date macro variables are indeed returned.
However, if I change my if statement (e.g. change if weekday = 2 to if weekday = 3) and then run my code, I'm returned dates that are intended to be set if weekday = 2 is returned (i.e. returned dates aren't changed, despite making change to if statement). If I run my code again (without making any changes to code after the initial change of weekday = 2 to weekday = 3), I'm then returned the dates that are set if weekday = 3.
Why do I have to run my code twice after making a change to the if statement for the logic to work and to return the intended dates?
data _null_;
/*if program runs on Monday before noon, then set reportdate to Saturday and priordate to Friday*/
if hour(time()) < 12 and weekday(today()) = 2 then do;
ReportDate = put(intnx('day',TODAY(),-2),date9.); call symputx('ReportDate',"'"||ReportDate||"'");
PriorDate = put(intnx('day',TODAY(),-3),date9.); call symputx('PriorDate',"'"||PriorDate||"'");
end;
/*if program doesn't run Monday morning, then set reportdate to current day and priordate to yesterday*/
else do;
ReportDate = put(intnx('day',TODAY(),0),date9.); call symputx('ReportDate',"'"||ReportDate||"'");
PriorDate = put(intnx('day',TODAY(),-1),date9.); call symputx('PriorDate',"'"||PriorDate||"'");
end;
%put &ReportDate; %put &PriorDate;
run;
Move your %PUT statements after the RUN.
If you place a %PUT statement inside a DATA step, it is execute before the data step code is executed. So there is a timing mistake.
Move your %PUT statements after the RUN.
If you place a %PUT statement inside a DATA step, it is execute before the data step code is executed. So there is a timing mistake.
What are you using those macro variables for?
If you are using them to select or compare to other dates then using the formatted values is inefficient.
If they are being used for things that humans need to see, such as title statements or filenames then the formatting makes sense.
You may want to investigate use of : quote(reportdate) instead of, the very ugly,
"'"||ReportDate||"'"
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!
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.