DATA Step, Macro, Functions and more

Assign date9. to macro var

Reply
New Contributor
Posts: 2

Assign date9. to macro var

%let dsin = test;

%let rc = %sysfunc(open(&dsin, I));

%if &rc % then %do;

   %let cdate = %sysfunc(attrn(&rc, crdte));

%end;

Question is: How to get cdate in date9. format? This can be done using data null and call symput but I would like to know if can be done in above method?

Super User
Posts: 11,343

Assign date9. to macro var

What result are you currently getting for CDATE? That is likely to be OS dependent so we need to see what you have.

The likely solution will involve some form of %sysfunc(inputn or inputc depending, with a date informat. That would be inside a %sysfunc(putn({long expression},date9.)0;

New Contributor
Posts: 2

Assign date9. to macro var

crdate value is 1636523977. Tried %sysfunc(putn(&crdate, date9.)) but not working. I get ********* [all asteriks]

Super User
Posts: 11,343

Assign date9. to macro var

When I tried a DATETIME16. format with that value of 1636523977 for cdate I get 10NOV11:05:59:37. Does that look like the correct date for your example? If so you want the DATEPART of CDATE.

Valued Guide
Posts: 2,177

Assign date9. to macro var

assuming internal value of &crdte is a sas date (#days since 1/1/1960)

%let date_constant = %sysfunc( putn( &crdte, date11 )) ;

* date11 looks even better than date9, and works just as well in:

  where date_var <= "&date_constant"d 

Super User
Posts: 19,877

Assign date9. to macro var

You can do it with nested functions. sysfunc has a put by default so you don't need a putn either.

However your variable is actually a datetime variable not a date variable so you need to take the datepart of the datetime variable first, then do the format.

%let cdate=%sysfunc(datepart(%sysfunc(attrn(&rc, crdte))),date9.);

Valued Guide
Posts: 2,177

Assign date9. to macro var

at some point SAS formats were extended with a few prefixed DT - these are designed for datetime values.

When extracting just the date part of "created date", we don't need the datepart() function, just DTDATE9

%let cdate= %sysfunc(attrn(&rc, crdte),DTdate9 );

Ask a Question
Discussion stats
  • 6 replies
  • 308 views
  • 0 likes
  • 4 in conversation