DATA Step, Macro, Functions and more

cannot make it work

Accepted Solution Solved
Reply
Contributor
Posts: 31
Accepted Solution

cannot make it work

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;


Accepted Solutions
Solution
‎11-15-2014 10:38 AM
Super User
Super User
Posts: 7,076

Re: cannot make it work

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


All Replies
Solution
‎11-15-2014 10:38 AM
Super User
Super User
Posts: 7,076

Re: cannot make it work

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"

...

Contributor
Posts: 31

Re: cannot make it work

Thank you!

🔒 This topic is solved and locked.

Need further help from the community? Please ask a new question.

Discussion stats
  • 2 replies
  • 209 views
  • 0 likes
  • 2 in conversation