DATA Step, Macro, Functions and more

Open Code Macro

Reply
Contributor
Posts: 27

Open Code Macro

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);

 

Super User
Posts: 19,822

Re: Open Code Macro

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.));
Contributor
Posts: 27

Re: Open Code Macro

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!

Super User
Posts: 19,822

Re: Open Code Macro

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?

Trusted Advisor
Posts: 1,301

Re: Open Code Macro

[ Edited ]

%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.

Ask a Question
Discussion stats
  • 4 replies
  • 328 views
  • 1 like
  • 3 in conversation