1
votes

I have a SAS macro where I would like to create a dataset name and then export that dataset to a csv file.

%macro export(outputDataset, prefix, outputFileName);
%let dName = cats(&prefix, test);
%let dName2 = cats(&prefix, test2);

data &outputDataset;
     set &dName &dName2;
run;


proc export data=&outputDataset outfile="outputDir/&outputFileName" replace; run;
%mend export;

However, when I call the macro using

%export(retain, hh_dpt, retained.csv)

I get the following error:

ERROR: Invalid option name hh_dept.

Does anyone know what the problem could be? Thank you!

2

2 Answers

4
votes

%let dName = cats(&prefix, test); stores the string cats(hh_dpt, test) into the macro variable dName. When the Data Step compiles, SAS sees:

data retain
    set cats(hh_dept, test) cats(hh_dept, test2);
run;

Which is an invalid use of the cats function. You cannot use SAS functions to concatenate dataset names in the Data Step set statement. You can correct this as such:

data retain;
     set &prefix.test &prefix.test2;
run;

OR

%let dName = &prefix.test;
%let dName2 = &prefix.test2;

data retain;
     set &dName &dName2;
run;

Where &prefix is ended by a . to denote that the string prefix is the name of the macro variable to be resolved. Without the ., the word scanner would assume that the full name of your macro variable is &prefixtest.

-1
votes

Try this based on your codes.

%macro export(outputDataset, prefix, outputFileName);
%let dName =%sysfunc(catx(.,&prefix, test));
%let dName2 =%sysfunc(catx(.,&prefix, test2));
data &outputDataset;
     set &dName &dName2;
run;
proc export data=&outputDataset outfile="outputDir/&outputFileName" replace; run;
%mend export;

%export(retain, hh_dpt, retained.csv)