BookmarkSubscribeRSS Feed
SAS09
Calcite | Level 5

Could someone please help me with this problem.  I am trying to create a macro variable in the format yymm using the following code- however the code seems to ignore the z format for the month portion of the code. I keep getting 162 instead of 1602  What am I missing here?  Please advise.

 

%macro getdates (currdate);

%if %eval("&currdate"d > %sysfunc(intnx(week.6, %sysfunc(intnx(month, "&currdate"d,0,E)),0))) %then %do;

%let C_ME=%eval(%sysfunc(intnx(week.6, %sysfunc(intnx(month, "&currdate"d,1,E)),0)));

%end;

%else %do;

%let C_ME=%eval(%sysfunc(intnx(week.6, %sysfunc(intnx(month, "&currdate"d,0,E)),0)));

%end;

%put %sysfunc(putn(&C_ME,date9.));

%let mth=%sysfunc(cats(%substr(%sysfunc(putn(%sysfunc(year(&C_ME)),4.)),3,2),%sysfunc(putn(%sysfunc(month(&C_ME)),Z2.))));

%put &mth;

%mend;

%getdates(26Feb2016);

 

4 REPLIES 4
Reeza
Super User

I don't know why you're getting 2, instead of 02 but I didn't look very hard. 

 

You can use the yymmn4. format instead though:

 

%put %sysfunc(putn(&C_ME,yymmn4.));
SAS09
Calcite | Level 5

Thank you Reeza!  This helps - although I am still curious to understand what I should do to make the Z format to work.  Thanks again for your quick response!

Reeza
Super User

CATS is converting the value to a number and stripping the leading zero. You need to create a character 02 and pass that to the function. The PUTN only formats the display value, but then CATS is stripping it. You'll need to nest some functions or use a different CAT operator - plain CAT perhaps?

FriedEgg
SAS Employee

%let mth=%substr(%sysfunc(year(&C_ME),4.),3,2)%sysfunc(month(&C_ME),Z2.);

 

or, more simply, as

 

 

%let mth=%sysfunc(putn(&C_ME.,yymmn4.));

 

as Reeza said

 

 

 

 

 

@Reeza, this behavior of CATS with sysfunc is another one of those bug/feature scenarios, if you ask me.  The CATS function 'conviently' interprets the datatype as being numeric and as a result trims the leading zero.

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

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
  • 1638 views
  • 1 like
  • 3 in conversation