Desktop productivity for business analysts and programmers

Macro variable data format

Posts: 73

Macro variable data format

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!?



Grand Advisor
Posts: 10,196

Re: Macro variable data format

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);

Posts: 73

Re: Macro variable data format

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?

Super User
Super User
Posts: 6,316

Re: Macro variable data format

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);


Posts: 1

Re: Macro variable data format

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;


Ask a Question
Discussion stats
  • 4 replies
  • 1 like
  • 4 in conversation