BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
VCucu
Obsidian | Level 7

i have a dataset "distincttenants" with following values: xxx yyy zzz I would like to be able to refer correctly in the macro "do" to the datasets prodxxx, prodyyy, prodzzz. How do I need to write the code to reference to the correct dataset ? obviously the text "prod" together (&) with the macro variable which i tried in the example below does not work. thank you

 

%macro do(tenant=); 
proc sql;
create table a1 as select * from prod&tenant.;
quit;
%mend do;

data distincttenants;
set distincttenants;
str=catt('%do(tenant=',tenantid,');');
call execute(str);
run;
1 ACCEPTED SOLUTION

Accepted Solutions
Tom
Super User Tom
Super User

What part of it does not work?  The posted code looks fine. 

To test the code try it without any macro logic.

%let tenant=xxx;
proc contents data=prod&tenant ;
run;

Sometimes in macros the parser can get confused about what you want to treat as a single token.  You can fix this with %unquote()

proc contents data=%unquote(prod&tenant) ;
run;

or simply assigning the generated name to a new macro variable and use that so there is no confusion about whether it is one token or two.

%let dsname=prod&tenant;
proc contents data=&dsname ;
run;

View solution in original post

8 REPLIES 8
Astounding
PROC Star

There are two things you need to do to get started.

 

First, change the name of the macro.  %DO is a macro key word, and so would be illegal as the name of a macro.

 

Second, write a call to the macro that works for a single data set instead of trying to do all three data sets at once. 

 

Once you have those pieces in place, you can re-post and get some help in how to call the macro three times.

VCucu
Obsidian | Level 7

1) thats fine, it was for illustration purpose; i have another one which is not "do" in the real application

2) i basically want to refer in a macro to a dataset name  which is composed of two parts

- fixed text "prod"

- variable text coming from macro variable "xxx", "yyy", "zzz"

 

in the example provided using "prod&tenant." does not work

VCucu
Obsidian | Level 7

so i have prodxxx, prodyyy and prodzzz tables however SAS compiler within the macro does not understand i want to select from these datasets if i use "prod&tenant."; 

Astounding
PROC Star

There are many places where the logic could break down.  That's why I asked for a simple macro call that processes one value.  For example, what happens when you try to run:

 

%macroname_instead_of_do (tenant=xxx)

PaigeMiller
Diamond | Level 26

I am going to second the request from @Astounding 

 

That's why I asked for a simple macro call that processes one value.

We have to see your code, and as requested, we need to see code that works on one value. Words will not get us there.

--
Paige Miller
Kurt_Bremser
Super User

Since you always create the same dataset in the macro, only the result of the last macro call will "survive".

 

PS "does not work" on its own without further details is a very stupid and unhelpful statement.

Tom
Super User Tom
Super User

What part of it does not work?  The posted code looks fine. 

To test the code try it without any macro logic.

%let tenant=xxx;
proc contents data=prod&tenant ;
run;

Sometimes in macros the parser can get confused about what you want to treat as a single token.  You can fix this with %unquote()

proc contents data=%unquote(prod&tenant) ;
run;

or simply assigning the generated name to a new macro variable and use that so there is no confusion about whether it is one token or two.

%let dsname=prod&tenant;
proc contents data=&dsname ;
run;
VCucu
Obsidian | Level 7

Hi Tom,

 

Thank you very much for the feedback. Your suggestion solved my issue.

%unquote(prod&tenant) ;

 Best regards,

 

Ready to join fellow brilliant minds for the SAS Hackathon?

Build your skills. Make connections. Enjoy creative freedom. Maybe change the world. Registration is now open through August 30th. Visit the SAS Hackathon homepage.

Register today!
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
  • 8 replies
  • 1236 views
  • 1 like
  • 5 in conversation