hi how to use a macro name in a filename statement I tried and it doent work
filename exprt '~/EMC1V2/data/&filename.csv' encoding="utf-8"; proc export data=&filename. outfile=exprt dbms=csv; run;
"Doesn't work" tells us absolutely nothing useful. Please show us the LOG for this section of code. From now on, if there are problems in the code, SHOW US the log, do not wait for us to ask.
In this case, the answer is obvious, you need to use double quotes in the FILENAME statement.
@HeatherNewton wrote:
hi how to use a macro name in a filename statement I tried and it doent work
filename exprt '~/EMC1V2/data/&filename.csv' encoding="utf-8"; proc export data=&filename. outfile=exprt dbms=csv; run;
You need to create and populate the macro variable somewhere.
In your example what you call &filename would actually need to resolve to a table.
What also doesn't add up: You're using in your code the macro variable in the Export procedure and not in the filename statement.
May be you need to explain us in detail what you have and what you're actually trying to achieve.
Assuming you have the macro Filename assigned the first thing, and I think has been mentioned before macro variables do not resolve inside single quotes.
To actually use the macro variable here.
filename exprt "~/EMC1V2/data/&filename.csv" encoding="utf-8";
But that is only the first problem. The macro processor uses the dot at the end of the variable to indicate the name of the macro variable so following text appends.
In the above code if &filename has a value like MYDATA then the text in the quotes would resolve to:
filename exprt "~/EMC1V2/data/MYDATAcsv" encoding="utf-8";
So if you expect the resulting data set to have an extension of CSV then you need two dots.
filename exprt "~/EMC1V2/data/&filename..csv" encoding="utf-8";
Here is an example of how to do what it looks like you are trying to do:
%let memname=example;
filename exprt "~/EMC1V2/data/&memname..csv" encoding="utf-8";
proc export data=&memname. outfile=exprt dbms=csv;
run;
Key changes:
You might also want to consider using some macro logic to control the case of the the letters used in the generated filename. Unix filesystem is case sensitive. So MyData.csv and mydata.csv are two different files. But SAS member names are not case sensitive so MyData and mydata are the some dataset name.
@HeatherNewton wrote:
By the way i didnt %let a macro variable as it was already one because it was created in a macro program so it is already a macro variable right?
Macro variables created in a macro are not usable outside the macro, unless you declare them to be %global like this
%global filename;
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!
Learn how use the CAT functions in SAS to join values from multiple variables into a single value.
Find more tutorials on the SAS Users YouTube channel.