Hello,
I have a macro where I need to return a two digit value for the prior month. The following macro looks back one month but when I use the z2. format, it drops the 0 in what I need to return as '07'. Is there a format that will return a leading zero for months (Jan - Sep) which would always have a leading zero? I would like to do this in a single line without an extra data step. I am using Base SAS 9.4. Thank you in advance for your help!
%let mm = %sysfunc(intnx(month.1,"&sysdate"d,-1), z2.);
%put &mm; (returns 7 instead of the desired 07)
Your macro didn't result in 7 as your code requires correction in the first place I am afraid. And then the use of month function is also missing like
%let mm = %sysfunc(intnx(month.1,%sysevalf("&sysdate"d),-1));
%let _mm=%sysfunc(month(&mm),z2.);
%put &_mm;
Of course you can combine the mm and _mm in one expression but my eyes hurt looking at very long expressions
Your macro didn't result in 7 as your code requires correction in the first place I am afraid. And then the use of month function is also missing like
%let mm = %sysfunc(intnx(month.1,%sysevalf("&sysdate"d),-1));
%let _mm=%sysfunc(month(&mm),z2.);
%put &_mm;
Of course you can combine the mm and _mm in one expression but my eyes hurt looking at very long expressions
Thank you so much novinosrin! I was able to combine the two lines you shared into one to make it a little more streamlined.
Have a great day!
- E
Would you mind sharing that line please? 🙂
Z2 will not drop the leading zero. How are you using the macro variable?
But if you try to apply the Z2 format to a date you should get ** unless the date is within the first 100 days of 1960.
If you want the month value you need ask for that explicitly. You could try using MMDDYY format and just take the first two characters.
Also don't use SYSDATE as it only includes 2 digit years. You should either Use SYSDATE9 or call the DATE() , also known as the TODAY() function, to get the current date instead of the date when your SAS session started.
1135 %put %sysfunc(putn(7,z2.)); 07 1136 %put %sysfunc(intnx(month.1,"&sysdate9"d,-6),mmddyy10.); 07/01/2019 1137 %put %substr(%sysfunc(intnx(month.1,"&sysdate9"d,-6),mmddyy10.),1,2); 07 1138 %put %sysfunc(month(%sysfunc(intnx(month.1,"&sysdate9"d,-6))),z2.); 07
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.