Deej88
Deej88

Reputation: 13

SAS - Macro Variable in Proc Export file name

I'm trying to transfer code that pulls a survey sample every month into a cronjob, but the last step I'm having an issue with in automating the code is with the file name in the proc export step.

I have the following macro variables defined at the beginning of the code:

%let today = date();
%let month = month(today);
%let year = year(today);

After I pull the data from our database and filter appropriately, I have a code that outputs the files as a pipe delimited .txt file. This file format is important to preserve:

proc export data=mkt.project_&timestamp._group
     outfile="/filepath/project_&year.&month._group" dbms=dlm Replace; 
delimiter='|';
run;

The file output name doesn't recognize the macro variables, however, so instead of getting the year and month, it just names them as "project_&year.&month._group".

Can anybody help with this?

Thanks!

Upvotes: 1

Views: 1755

Answers (1)

Tom
Tom

Reputation: 51566

Macro variables contain text.

You have set yours to text strings that look like SAS function calls. But then you did not use the strings to generate SAS code where such a function call would make sense. Instead you put the function call into the name of a file.

440   %let today = date();
441   %let month = month(today);
442   %let year = year(today);
443   %put "/filepath/project_&year.&month._group";
"/filepath/project_year(today)month(today)_group"

One way to execute SAS functions in macro code is to use the macro function %sysfunc(). If you want to generate the 6 digit string in they style YYYYMM you can use the YYMMN6. format. So you could generate your filename like this:

 "/filepath/project_%sysfunc(date(),yymmn6.)_group"

Or your other macro variables like this:

%let today = %sysfunc(date());
%let month = %sysfunc(month(&today),z2.);
%let year = %sysfunc(year(&today));

Upvotes: 2

Related Questions