DATA Step, Macro, Functions and more

using a macro variable in a name literal

Accepted Solution Solved
Reply
Contributor
Posts: 39
Accepted Solution

using a macro variable in a name literal

hi everyone,

im trying to use a macro variable in a name literal to reference an excel sheet but it is not working for me. i can get the libname statement to work but not the data step. is there anyway to evaluate a macro variable inside the name literal? here is my code and error:

%let prot=132;

libname stabdata "C:\Desktop\p&prot..xlsx";

data p&prot.data; set stabdata.'p&prot.data$'n; run;

libname stabdata clear;

i would like the data step to evaluate as data p132data; set stabdata.'p132data$'n; run; but i get the following error

data p&prot.data; set stabdata.'p&prot.data$'n; run;

ERROR: File STABDATA.'p&prot.data$'n.DATA does not exist.


Accepted Solutions
Solution
‎08-05-2013 05:00 PM
Super User
Super User
Posts: 7,076

Re: using a macro variable in a name literal

Macro variables do not expand within single quotes. Convert to using double quotes.

stabdata."p&prot.data$"n;

View solution in original post


All Replies
Respected Advisor
Posts: 3,156

Re: using a macro variable in a name literal

In your SAS explorer or open the excel sheet, get the sheet name you need. I suspect that you don't have a sheet name called p123data.

Haikuo

Contributor
Posts: 39

Re: using a macro variable in a name literal

Haikuo-

i checked SAS explorer and the excel sheet is listed. I believe the problem is that the macro variable is not being resolved.

if i manually change the stabdata.'p&prot.data$'n to stabdata.'p132data$'n then everything works ok

Solution
‎08-05-2013 05:00 PM
Super User
Super User
Posts: 7,076

Re: using a macro variable in a name literal

Macro variables do not expand within single quotes. Convert to using double quotes.

stabdata."p&prot.data$"n;

Contributor
Posts: 39

Re: using a macro variable in a name literal

Excellent!! I didn't know that SAS literals worked with double quotes...I was always under the impression that name, time, date, etc literals needed single quotes.

🔒 This topic is solved and locked.

Need further help from the community? Please ask a new question.

Discussion stats
  • 4 replies
  • 422 views
  • 0 likes
  • 3 in conversation