Jannick Mikkelsen
Jannick Mikkelsen

Reputation: 141

Include macro in a file name

In SAS I like to make a dynamic filename since I am browsing my data on a daily tabel.

I have tried to include a macro in the filename like this:

%let date=input(put(today()-3,ddmmyy6.),6.); *This is equal to todays date-3 (format = 190317)
filename nlp "DailyB.DG%date"; 

It does not work can you help me?

To get a intuition of what I like to do I have posted a example below I want to have a date minus 3 days from today in this format:DDMMYY (190317) So if i run the code the day after it would be 200317.

The variable should then be put into the code so I get the following:

 filename nlp 'DailyB.DG190317';

Upvotes: 0

Views: 561

Answers (3)

DomPazz
DomPazz

Reputation: 12465

You need to use the %sysfunc() and %eval() macro functions to evaluate the Data Step functions outside the Data Step. As Joe says:

%let date=%sysfunc(putn(%eval(%sysfunc(today())-3),ddmmyyn6.));

Then you need to change your % to a &

filename nlp "DailyB.DG&date"; 

Upvotes: 0

Tom
Tom

Reputation: 51566

So what you currently tried is going to end up with a filename statement like

filename nlp "DailyB.DGinput(put(today()-3,ddmmyy6.),6.)"; 

To run functions in macro code you need to use %SYSFUNC(). You will also need to use the PUTN() function since the PUT() function doesn't work with %SYSFUNC().

%let date=%sysfunc(putn(%sysfunc(today())-3,ddmmyyn6));
filename nlp "DailyB.DG&date"; 

If you can get the process changed I would recommend using 8 characters for the date part of the name so that you can include the century. Also if you use Year,Month,Day order for your date string your generated filenames will sort in proper date order and also prevent users confusing Columbus Day for Dewey Decimal System Day.

Upvotes: 0

Joe
Joe

Reputation: 63424

If you want a macro variable to resolve a function, you need %sysfunc. Here's one way to do that.

%let date=%sysfunc(putn(%eval(%sysfunc(today())-3),ddmmyyn6.)); *This is equal to todays date-3 (format = 190317);
%put &=date;

The first %sysfunc asks for the result of today(), the second asks for the result to be formatted. %eval is needed to subtract 3 from the value, as @Quentin points out in comments.

Alternately, call symputx would work here if you're more comfortable in the data step.

data _null_;
  call symputx('date',put(today()-3,ddmmyyn6.));
run;
%put &=date;

Upvotes: 1

Related Questions