BookmarkSubscribeRSS Feed
Ronein
Onyx | Level 15

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?

 

6 REPLIES 6
novinosrin
Tourmaline | Level 20

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
novinosrin
Tourmaline | Level 20

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!

 

mkeintz
PROC Star

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

 

--------------------------
The hash OUTPUT method will overwrite a SAS data set, but not append. That can be costly. Consider voting for Add a HASH object method which would append a hash object to an existing SAS data set

Would enabling PROC SORT to simultaneously output multiple datasets be useful? Then vote for
Allow PROC SORT to output multiple datasets

--------------------------
novinosrin
Tourmaline | Level 20

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!

 

Patrick
Opal | Level 21

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

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 6 replies
  • 1568 views
  • 2 likes
  • 5 in conversation