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

in a macro, I would like to make it fexible, instead of use  name = "tbl_&x1._&x2._&x3.", is there a way to make

tbl_&&x1

%do k=2 %to &numbertype;

    .._&&x&k

%end;

but how to make quotetion (") work in where statement?  Thank you!

proc sql;

  select attname from _v_relation_column

  where name = "tbl_&x1._&x2._&x3.";

quit;

1 ACCEPTED SOLUTION

Accepted Solutions
Tom
Super User Tom
Super User

Sounds like you want to call a macro and pass in the number of values to expand and it will expand each of the those values.  So if N=3 then it will look like your example.

* Set up some values for the Xn macro variables. ;

%let x1=4 ;

%let x2=7;

%let x3= 1;

%* Define macro - This version the prefix used to generate the macro variable reference is an optional parameter ;

%macro x(n,var=x);

%local i ret ;

%do i=1 %to &n ;

%let ret=&ret._&&&var.&i;

%end;

&ret.

%mend ;


%put %x(3);

_4_7_1

So then your WHERE clause becomes:

where name="tbl%x(3)" ;

If you are doing this within an already existing macro you can use the same concept without the need to define another macro.  You just need to introduce the new macro variable to hold the generated string.

%let tbl=tbl;

%do k=1 %to &numbertype;

  %let tbl=&tbl._&&x&k ;

%end;

...

where name="&tbl"

...

View solution in original post

2 REPLIES 2
Tom
Super User Tom
Super User

Sounds like you want to call a macro and pass in the number of values to expand and it will expand each of the those values.  So if N=3 then it will look like your example.

* Set up some values for the Xn macro variables. ;

%let x1=4 ;

%let x2=7;

%let x3= 1;

%* Define macro - This version the prefix used to generate the macro variable reference is an optional parameter ;

%macro x(n,var=x);

%local i ret ;

%do i=1 %to &n ;

%let ret=&ret._&&&var.&i;

%end;

&ret.

%mend ;


%put %x(3);

_4_7_1

So then your WHERE clause becomes:

where name="tbl%x(3)" ;

If you are doing this within an already existing macro you can use the same concept without the need to define another macro.  You just need to introduce the new macro variable to hold the generated string.

%let tbl=tbl;

%do k=1 %to &numbertype;

  %let tbl=&tbl._&&x&k ;

%end;

...

where name="&tbl"

...

Ying
Fluorite | Level 6

Thank you!

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
  • 2 replies
  • 985 views
  • 0 likes
  • 2 in conversation