Hello
%let YYMMDD=200323;
I want to create a new macro variable that will get value 23_03_2020
What is the way to do it please?
%let newvar=%substr(&yymmdd,5,2)_%substr(&yymmdd,3,2)_20%substr(&yymmdd,1,2);
For what it's worth, here is another approach
%let YYMMDD=200323;
%let want=%sysfunc(translate(%sysfunc(inputn(&yymmdd,yymmdd8.),ddmmyyd10.),'_','-'));
%put &=want;
LOG:
1803 %let YYMMDD=200323;
1804
1805 %let want=%sysfunc(translate(%sysfunc(inputn(&yymmdd,yymmdd8.),ddmmyyd10.),'_','-'));
1806 %put &=want;
WANT=23_03_2020
Some fun with PICTURE formats instead of translation.
proc format;
picture dt
low-high='99_99_9999' ;
run;
%let YYMMDD=200323;
%let want=%sysfunc(putn(%sysfunc(inputn(&yymmdd,yymmdd8.),ddmmyyn8.),dt.));
%put &=want;
1841 %let YYMMDD=200323;
1842
1843 %let want=%sysfunc(putn(%sysfunc(inputn(&yymmdd,yymmdd8.),ddmmyyn8.),dt.));
1844 %put &=want;
WANT=23_03_2020
I would like bring this need to the attention of elders like @Tom @ChrisHemedinger @mkeintz the idea of having a '_' also as a separator DDMMYYx Format as it seems this option is currently unavailable. Thank you @Ronein for the question!
@novinosrin Your picture idea will work if you use the DATATYPE option in the picture statement:
proc format; picture dt low-high='%0d_%0m_%G' (datatype=date) ;run;
%let yymmdd=200323;
%let want=%sysfunc(putn(%sysfunc(inputn(&yymmdd,yymmdd6.)),dt.));
%put &=want;
The datatype option allows you to use picture directives. In this case %0d (day of month with leading zero), %0m (month), and %G (4-digit year), with _ as separators.
Of course it would be nice if the ddmmyyxw. format allowed underscore characters. But if the OP were happy with, say a colon separator, then you can use a pre-defined variant of ddmmyyxw.:
381 %let yymmdd=200323;
382 %let want=%sysfunc(putn(%sysfunc(inputn(&yymmdd,yymmdd6.)),ddmmyyC10.));
383 %put &=want;
WANT=23:03:2020
If SAS allowed a U (for underscore) where I put a C, no picture statement would be needed.
BTW, whenever I am using a date expression as a macro var value, I try to avoid the YYYYMMDD (unless I'm sorting by macro values) or DDMMYY layouts. Instead I often do:
%let mydate=23mar2020;
which allows me to trivially use "&mydate"d as date expressions in SAS code,
or %sysevalf("&mydate"d) when it has to be evaluated at the macro level.
Thanks to Richard de Venezia for the %sysevalf tip.
Thank you @mkeintz for lending your time and furnishing finer details. So, OP, I et al got more for our money 🙂
"Of course it would be nice if the ddmmyyxw. format allowed underscore characters. If SAS allowed a U (for underscore) where I put a C, no picture statement would be needed."
I'm glad to note that concurrence and that really made my day on the community. Cheers!
And just for fun here an option using regex
%let YYMMDD=200323;
%let want=%sysfunc(prxchange(s/(\d\d)(\d\d)(\d\d)/20\3_\2_\1/oi,1,&yymmdd));
%put &=want;
It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.
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.
Ready to level-up your skills? Choose your own adventure.