BookmarkSubscribeRSS Feed
Ronein
Meteorite | Level 14

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
Meteorite | Level 14

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
Meteorite | Level 14
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,

SAS Innovate 2025: Call for Content

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!

Submit your idea!

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 6 replies
  • 632 views
  • 2 likes
  • 3 in conversation