BookmarkSubscribeRSS Feed
noobs
Calcite | Level 5


Hello EG Users,

I have following had-coded %Let statement:

%LET birthdate = '01JAN2014'd;

I would like to change it to be flexible and equal to another variable from dataset

%LET birthdate = bdate;

Now bdate is of format mmddyy8.

How do I make sure that macro variable birthdate gets assigned value of variable bdate however is similar to '01JAN2014'd?

In summary, I am trying to understand what that 'd does to value inside text?

I tried using

format birthdate mmddyy8.;

as well as

format birthdate DATE9.;

but it did not work!?

Thanks,

Dhanashree

4 REPLIES 4
ballardw
Super User

The d in a literal like '01JAN2014'd tells SAS to treat the string as an internal date value which is numeric. There is a similar suffix, dt, for use with datetime literals.

If bdate is a variable in a dataset using %let will not assign its value to the macro variable birthdate. The main interface to assign values to macro variables from the data step is CALL SYMPUT. The syntax would generally be Call Symput ('birthdate', bdate); There are some cautions here though. If you dataset has multiple records the above syntax will only contain the value for the last record in the data set. Also, since macro variables are strings you probably want to control exactly what is the content and the default for dates probably won't be what you want.

If you want the value of bdate to become something like '01jand2014'd then you would probably be best off to create an additional variable and use funtions to create a variable of interest.

This code in a data step will create a macro variable birthdate with the date of bdate appearing as 'DDMONYYYY'd

length datestring $ 12;

datestring = cats("'",put(bdate,date9.),"'d");

call symput('birthdate',datestring);

noobs
Calcite | Level 5

Thats very helpful.

But I am seeing erratic behavior. Intermittently, when I try to print &birthdate, am getting warning

WARNING: Apparent symbolic reference birthdate not resolved.

How do I make sure that this does not happen?

Tom
Super User Tom
Super User

This means the variable did not get created.  If you are creating it from a data step then the data step might not have run or ran with zero observations so it never executed the CALL SYMPUT().  One way to avoid this is to create the macro variable first with a default value.

%let birthdate=unknown;

data _null_;

   set patients ;

   where pid = 5 ;

   call symputx('birthdate',bdate);

run;

madanparna
Calcite | Level 5

I think the below code may help you 

%let st_dt = '11FEB2014'd;

%put &st_dt;

data _NULL_;

/*to use the same date with diffrent date format*/

call symput("wst_end","'"||put(&st_dt,date9.)||"'d");

%put &wst_end;

/* goes 90 days back to the date present in the macro which you are passing */

call symput("wst1_end","'"||put(intnx('DAY',&st_dt,-90,'SAMEDAY'),date9.)||"'d");

%put &wst1_end;

/*to use the same date in oracle quries (ie, connect to oracle ....) */

call symput("st_end","'"||put(&st_dt,date9.)||"'");

%put &st_end;

run;

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

Register now!

SAS Enterprise Guide vs. SAS Studio

What’s the difference between SAS Enterprise Guide and SAS Studio? How are they similar? Just ask SAS’ Danny Modlin.

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
  • 4 replies
  • 6195 views
  • 1 like
  • 4 in conversation