Hi SAS Forum,
A coder has written the following 3 sas statements with the purpose of creating yester weekday (no just yesterday) in MMMDD format as a macro variable.
For example, when I ran below 3 statements just today (i.e. Jan 21), Jan20 should be auto typed into the log.
Yes it did the job.
/*statement 1*/
%let today = %sysfunc(date());
/*statement 2*/
%let yesterwkday= %sysfunc(intnx(WEEKDAY,&today,-1), monname3.)%qsubstr(%sysfunc(intnx(WEEKDAY,&today,-1),mmddyy4.),3,2) ;
/*statement 3*/
%PUT &yesterwkday;
To understand what “statement 2” does above, I dismantled it into 2 pieces and ran them separately (green and blue colors).
/*Green part*/
%let part1= %sysfunc(intnx(WEEKDAY,&today,-1), monname3.);
%put &part1;
Jan is typed in the log
Thanks to Reeza and Amir’s help, I think I understand green part (Interpretation: Macro function %sysfunc calls the embedded SAS function called intnx)
/*Blue part*/
%let part2= %qsubstr(%sysfunc(intnx(WEEKDAY,&today,-1),mmddyy4.),3,2) ;
%put &part2;
20 is typed in the log
I have googled and also understood the role of macro function %qsubstr (as opposed to just %substr), so can understand somewhat the part highlighted by blue color.
Question:
When I put green and blue color pieces together and try to understand how it finally generates Jan20, I get lost. In other words, there are two %sysfunc embedded. So, which %sysfunc works firsts and which %sysfunc works next and so on…
Could someone help me in understanding the long statement 2 as a whole.
Thanks
Miris
You are over thinking the problem. Since in this problem they are independent the order of execution does not really matter. Any more than asking do you add 3 and 4 first or add 5 and 6 in the following equation: x = (3+4)*(5+6)
%SYSFUNC() is mainly just a way to call most SAS functions (there are a handful that do not work).
So think of nested call %sysfunc(X(...)) the same as if there was a macro function %x().
The main difference that %SYSFUNC() has over that is the optional second parameter that lets you specify a format to use to convert the results of the function being called to a character string (everything in macro is a character sting). So your two examples you are using MONNAME3. and MMDDYY. as the formats.
Your BLUE section looks like it wants the two digit day of the month. That could more easily be coded this way.
%sysfunc(intnx(weekday,&today,-1),ddmmyy2.)
You are over thinking the problem. Since in this problem they are independent the order of execution does not really matter. Any more than asking do you add 3 and 4 first or add 5 and 6 in the following equation: x = (3+4)*(5+6)
%SYSFUNC() is mainly just a way to call most SAS functions (there are a handful that do not work).
So think of nested call %sysfunc(X(...)) the same as if there was a macro function %x().
The main difference that %SYSFUNC() has over that is the optional second parameter that lets you specify a format to use to convert the results of the function being called to a character string (everything in macro is a character sting). So your two examples you are using MONNAME3. and MMDDYY. as the formats.
Your BLUE section looks like it wants the two digit day of the month. That could more easily be coded this way.
%sysfunc(intnx(weekday,&today,-1),ddmmyy2.)
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 the difference between classical and Bayesian statistical approaches and see a few PROC examples to perform Bayesian analysis in this video.
Find more tutorials on the SAS Users YouTube channel.
Ready to level-up your skills? Choose your own adventure.