BookmarkSubscribeRSS Feed
Ronein
Onyx | Level 15

Hello

User define a macro variable  YYYYMMDD  and I want that another macro variable called DDMMYYYY will be created automatically.

For example:

%let YYYYMMDD=20210719;

so DDMMYYYY should be 19072021

 

What is the way to do it please via %let  ?

 

6 REPLIES 6
Ronein
Onyx | Level 15

I know to do it via data step.

What is the reason that I must use compress function ?(Otherwise it is not working well)

Why the macro variable that is created via %LET is not compressed? 

 

%let YYYYMMDD=20210628 

Data tt;
dd=substr(compress(&YYYYMMDD.),7,2);/**want to get 28**/
mm=substr(compress(&YYYYMMDD.),5,2);/**want to get 06**/
yyyy=substr(compress(&YYYYMMDD),1,4);/**want to get 2021**/
DDMMYYYY=CATS(dd,mm,yyyy);
call symputx('DDMMYYYY',DDMMYYYY);
Run;
%put &DDMMYYYY.;

 

 

andreas_lds
Jade | Level 19

I would not do this in a %let statement, all those %sysfunc-calls will make the code hardly readable.

data _null_;
   call symputx('DDMMYYYY', put(input("&yyyymmdd.", yymmdd10.), ddmmyyn8.));
run;

%put &=DDMMYYYY.;
Ronein
Onyx | Level 15
Great, I have 3 questions please:
1-input("&yyyymmdd.", yymmdd10.)
Here you transform from char date into sas date.
The char date (&yyyymmdd.) has 8 digits .
Why did you use yymmdd10. and not yymmdd8. ?
Why did you put &yyyymmdd. into double quotation marks?

2-PUT( SAS date that you created, DDMMYYN8.)
Here you transform SAS date into char date.
Why did you use DDMMYYN8. and not DDMMYY8. ?

3- DATA _NULL_
Can you also write DATA NULL?
What is difference between DATA _NULL_ and DATA NULL ? (Or maybe both are same)?

thank you

andreas_lds
Jade | Level 19

1) yymmdd10 was a mistake, working but the "10" should be changed to "8".  I quoted the macro-variable to avoid the  following notes, which are in-fact error:

 NOTE: Numeric values have been converted to character values at the places given by: (Line):(Column).
       72:1   
 NOTE: Invalid argument to function INPUT at Zeile 72 Spalte 33.

2) Try it with ddmmyy8. - you will see the difference.

 

3) Try it 😉

andreas_lds
Jade | Level 19

Well, after actually thinking - should do this more often before i answer - there is only one %sysfunc necessary:

%let DDMMYYYY = %sysfunc(inputn(&yyyymmdd., yymmdd8.), ddmmyyn8.);
%put &=DDMMYYYY;
ChrisNZ
Tourmaline | Level 20

You could just use the %substr() function. One line of code, no data step required,

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
  • 1276 views
  • 2 likes
  • 3 in conversation