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-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

Register now!

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