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

Hello,

 

I have a large dataset, from which I want to extract a subset based on a datetime variable.

 

So far, I've managed to obtain the SAS datetime that I want as a macro variable. For instance, I want the SAS datetime for 2019-04-01, 00:00:00:000, and I've tried the following to save the datetime as a macro variable called "dtime":

 

 

data save;
a='01APR2019, 00:00:00.000'dt;
run;

data _null_;
set save;
call symput("dtime", a);
run;

 

 

Question: Because I will have to repeat the codes above many times to subset based on multiple different dates, I am wondering if I can create a macro for the date part of the datetime. I tried the following to add a macro variable called "date" that has the date of interest:

 

%let date=01APR2019;

data save;
a='&date, 00:00:00.000'dt;
run;

data _null_;
set save;
call symput("dtime", a);
run;

Nonetheless, the &date part is not evaluated in the data step. So I wonder if there's way to let a = '01APR2019, 00:00:00.000'dt in the data step with the help of this macro variable "date".

 

First time posting, so I apologize if my question was not clearly stated.

1 ACCEPTED SOLUTION

Accepted Solutions
PaigeMiller
Diamond | Level 26

You need to enclose macro variables in double-quotes if you are going to use a macro variable.

 

%let date=01APR2019;

data _null_;
     a="&date 00:00:00.000"dt;
     call symput('dtime',a);
run;

Please note proper terminology for clarity: this is not a macro as your question implies, this is a macro variable, which is different.

 

Even easier:

 

%let dtime=%sysfunc(dhms("&date"d,0,0,0));

 

--
Paige Miller

View solution in original post

2 REPLIES 2
PaigeMiller
Diamond | Level 26

You need to enclose macro variables in double-quotes if you are going to use a macro variable.

 

%let date=01APR2019;

data _null_;
     a="&date 00:00:00.000"dt;
     call symput('dtime',a);
run;

Please note proper terminology for clarity: this is not a macro as your question implies, this is a macro variable, which is different.

 

Even easier:

 

%let dtime=%sysfunc(dhms("&date"d,0,0,0));

 

--
Paige Miller
aaronh
Quartz | Level 8

Thank you so much! I didn't realize there is a short-cut (albeit a somewhat more complex one) that gets me the same result. Thanks for helping me out!

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

Register now!

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
  • 2 replies
  • 3337 views
  • 1 like
  • 2 in conversation