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);
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.));
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!
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?
%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.
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.