BookmarkSubscribeRSS Feed
☑ This topic is solved. Need further help from the community? Please sign in and ask a new question.
KatWinSASland
Calcite | Level 5

I want to create a table name from macro variables I iterate through. I am running into a problem when the first macro call &&thing&i. does not resolve and SAS is trying to resolve the combined call. The first call &&thing&j. is resolving to &thing1. How do I have thing1 get resolved to: auto? 

 

 

data things;
input thing $6.;
auto
rv
boat
;

proc SQL;
select distinct thing 
into :thing1-
from things;
quit;

macro thingy();
%do i %to %6;
%do j %to &sqlobs;
data temp;
set have.&&thing&j._&i.mo;
run;
%end;
%end;
%mend thingy;
%thingy()



1 ACCEPTED SOLUTION

Accepted Solutions
PaigeMiller
Diamond | Level 26
set have.&&thing&j._&i.mo;

 

After &j, try using two dots. (If that doesn't work, try three dots)

--
Paige Miller

View solution in original post

3 REPLIES 3
PaigeMiller
Diamond | Level 26
set have.&&thing&j._&i.mo;

 

After &j, try using two dots. (If that doesn't work, try three dots)

--
Paige Miller
Tom
Super User Tom
Super User

I want to create a table name from macro variables I iterate through.

Why?  What is the larger problem you are trying to solve and how does being able to generate dataset name in this way help?

 

There are a lot of mistakes in that SAS code.  First let's clean up the setup part.

data things;
  input thing $6.;
cards;
auto
rv
boat
;

proc sql noprint;
select distinct thing 
  into :thing1-
  from things
;
%let nthings=&sqlobs;
quit;

So now you have 3 macro variables named THING1, THING2 and THING3 and a fourth macro variable named NTHINGS which you could perhaps use to do something (what??).

 

Now let's cleanup your attempt to loop and build a token from the indexes and those macro variables.

Let's just write the generated string to the LOG so we can see what we get.

%macro thingy;
%do i=1 %to 2;
  %do j=1 %to &nthings;
    %put &=i &=J prefix_&&thing&j.._&i._suffix ;
  %end;
%end;
%mend thingy;
%thingy;

Results:

262  %macro thingy;
263  %do i=1 %to 2;
264    %do j=1 %to &nthings;
265      %put &=i &=j prefix_&&thing&j.._&i._suffix ;
266    %end;
267  %end;
268  %mend thingy;
269  %thingy;
I=1 J=1 prefix_auto_1_suffix
I=1 J=2 prefix_boat_1_suffix
I=1 J=3 prefix_rv_1_suffix
I=2 J=1 prefix_auto_2_suffix
I=2 J=2 prefix_boat_2_suffix
I=2 J=3 prefix_rv_2_suffix

But why couldn't you just leave the value of THING in the actual variable?  What did you gain by moving them into macro variables?  Perhaps just passing in one value at a time?

%macro thingy(thing);
%do i=1 %to 2;
    %put &=i prefix_&thing._&i._suffix ;
%end;
%mend thingy;

data _null_;
  set things;
  call execute(cats('%nrstr(%thingy)(',thing,')'));
run;

Result

1   + %thingy(auto)
I=1 prefix_auto_1_suffix
I=2 prefix_auto_2_suffix
2   + %thingy(rv)
I=1 prefix_rv_1_suffix
I=2 prefix_rv_2_suffix
3   + %thingy(boat)
I=1 prefix_boat_1_suffix
I=2 prefix_boat_2_suffix

 

PaigeMiller
Diamond | Level 26

@Tom said:

 

So now you have 3 macro variables named THING1, THING2 and THING3 and a fourth macro variable named NTHINGS which you could perhaps use to do something (what??).

 

I'm sure this was in a Dr. Seuss book I once read. I didn't know he was writing about SAS macros!

 

But good question!

--
Paige Miller

SAS Innovate 2025: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 3 replies
  • 558 views
  • 1 like
  • 3 in conversation