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;
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"
...
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"
...
Thank you!
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!
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.