BookmarkSubscribeRSS Feed
HeatherNewton
Quartz | Level 8

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;
6 REPLIES 6
PaigeMiller
Diamond | Level 26

"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.

--
Paige Miller
Patrick
Opal | Level 21

@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.

ballardw
Super User

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";
Tom
Super User Tom
Super User

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:

 

  • Use a macro variable name that reflects the its usage better.
  • Use double quotes around the filepath string so the macro variable will resolve
  • Include a period before the csv extension.  The period after the macro variable names is used by the macro processor to indicate where the name of the macro variable ends.

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
Quartz | Level 8
Thanks i was using single quotes around filepath so it didnt work

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?
PaigeMiller
Diamond | Level 26

@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;
--
Paige Miller

SAS Innovate 2025: Call for Content

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!

Submit your idea!

How to Concatenate Values

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 6 replies
  • 754 views
  • 0 likes
  • 5 in conversation