BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
Batman
Quartz | Level 8

How can I convert the following text date stored in a macro variable into a sas date?

 

%let dt=September 16, 2005;

%put &dt;

1 ACCEPTED SOLUTION

Accepted Solutions
mkeintz
PROC Star

For macro variables, I suggest using text in the form of ddMMMyyyy, without any quotes.

 

I.e, something like

    %let d=05JUL2021 ;

 

This tactic makes it easy to use macrovar-based dates in regular SAS code, as in:

 

data want;
   set have;
   where date >="&d"d;
run;

I.e., in the SAS code, just provide the trailing d, and the quotes - but they have to be double quotes to avoid masking the &d value.

 

Similarly for time, use 

   %let t=15:05:20.05

 

And for date-times
   %let dt=03jul2021:12:20:30.10;

 

whose use in a sas program might look like:

 

%let d=05jul2021;
%let t=15:30:15.05;
%let dt=03jul2021:12:20:30.10;

data _null_;
  d="&d"d;      put d=date9.;
  t="&t"t;      put t=time11.2;
  dt="&dt"dt;   put dt=datetime30.3;
run;
--------------------------
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

--------------------------

View solution in original post

7 REPLIES 7
Batman
Quartz | Level 8

I initially read it in as a parameter with the %quote() around it.   Similar to 

 

%macro xyz(dt);

....

%mend;

%xyz(%quote(September 16, 2005))

ballardw
Super User

Where do you want to use this new "date" value? As another macro variable? Variable in a data step or proc sql?

 

General solution without details is going to involve an Input function with a proper informat as the possibly cleanest solution. But which and where can bring up some details.

 

@Kurt_Bremser is hinting, I think, that when you make it may be the place to create the new value.

 

 

Batman
Quartz | Level 8

I intend to use it as a date filter in a data step.   I know I can convert the date by hand to someone more user friendly, but was hoping SAS could do it if it doesn't require really elaborate code.

Batman
Quartz | Level 8
Sorry, "something more user friendly"
mkeintz
PROC Star

For macro variables, I suggest using text in the form of ddMMMyyyy, without any quotes.

 

I.e, something like

    %let d=05JUL2021 ;

 

This tactic makes it easy to use macrovar-based dates in regular SAS code, as in:

 

data want;
   set have;
   where date >="&d"d;
run;

I.e., in the SAS code, just provide the trailing d, and the quotes - but they have to be double quotes to avoid masking the &d value.

 

Similarly for time, use 

   %let t=15:05:20.05

 

And for date-times
   %let dt=03jul2021:12:20:30.10;

 

whose use in a sas program might look like:

 

%let d=05jul2021;
%let t=15:30:15.05;
%let dt=03jul2021:12:20:30.10;

data _null_;
  d="&d"d;      put d=date9.;
  t="&t"t;      put t=time11.2;
  dt="&dt"dt;   put dt=datetime30.3;
run;
--------------------------
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

--------------------------

hackathon24-white-horiz.png

The 2025 SAS Hackathon Kicks Off on June 11!

Watch the live Hackathon Kickoff to get all the essential information about the SAS Hackathon—including how to join, how to participate, and expert tips for success.

YouTube LinkedIn

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
  • 7 replies
  • 1243 views
  • 0 likes
  • 4 in conversation