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!
Don't miss out on SAS Innovate - Register now for the FREE Livestream!
Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.
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.