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

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)
1 ACCEPTED SOLUTION

Accepted Solutions
novinosrin
Tourmaline | Level 20

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 

View solution in original post

4 REPLIES 4
novinosrin
Tourmaline | Level 20

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 

esvenson
Fluorite | Level 6

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

bjarkeahm
Fluorite | Level 6

Would you mind sharing that line please? 🙂

Tom
Super User Tom
Super User

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

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

Register now!

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 4 replies
  • 15586 views
  • 0 likes
  • 4 in conversation