DATA Step, Macro, Functions and more

macro variable in a let statement not resolving all the way

Accepted Solution Solved
Reply
New Contributor
Posts: 2
Accepted Solution

macro variable in a let statement not resolving all the way

I the following code I am trying to create a dynamic variable name and use it later in the code, but it isn't fully resolving.  please see the last few lines.  I would like the put statement macro variable &&mem&i.._rcols to output the same info that is in &xx.  I have also tried a data _null_  with a call symput to create the varialbe.

 

%macro a;

Data _null_;

call symput("mem1","ae");

call symput("mem1vartotal","4");

call symput("mem2","cm");

call symput("mem2vartotal","3");

run;

%do i = 1 %to 2;

%let x = 0;

%do v = 1 %to &&mem&i.vartotal;

%let x = %eval(&x + 1);

%end;

%let &&mem&i.._rcols = %str();

%let xx=%str();

%do k=1 %to &x;

%let xx = %str(&xx, _&k);

%end;

%let &&mem&i.._rcols = %str(&xx);

%put &&mem&i &&mem&i.._rcols &xx ;

%end;

%mend a;

%a;

 


Accepted Solutions
Solution
‎06-15-2017 01:06 PM
PROC Star
Posts: 1,322

Re: macro variable in a let statement not resolving all the way

Posted in reply to BillBaker_cd3
It's a little terrifying, but looks like:
%put &&mem&i &&&&&&mem&i.._rcols &xx ;
works

View solution in original post


All Replies
Solution
‎06-15-2017 01:06 PM
PROC Star
Posts: 1,322

Re: macro variable in a let statement not resolving all the way

Posted in reply to BillBaker_cd3
It's a little terrifying, but looks like:
%put &&mem&i &&&&&&mem&i.._rcols &xx ;
works
New Contributor
Posts: 2

Re: macro variable in a let statement not resolving all the way

Posted in reply to BillBaker_cd3

thank you very much, I Stopped at 4 &.

Super User
Super User
Posts: 7,046

Re: macro variable in a let statement not resolving all the way

Posted in reply to BillBaker_cd3

It is MUCH easier to store the generated macro variable name into another macro variable and then use three & to evaluate the macro variable that it names.

%macro a;
%local i k name;
%do i = 1 %to 2;
    %let name=&&mem&i.._rcols;
    %local &name ;
    %let &name=;
    %do k=1 %to &&mem&i.vartotal;
        %let &name = &&&name, _&k;
    %end;
    %put &=i|&&mem&i|&=name|&&&name| ;
%end;
%put _local_;
%mend a;

data _null_;
call symput("mem1","ae");
call symput("mem1vartotal","4");
call symput("mem2","cm");
call symput("mem2vartotal","3");
run;

%a;

Results

I=1|ae|NAME=ae_rcols|, _1, _2, _3, _4|
I=2|cm|NAME=cm_rcols|, _1, _2, _3|
A AE_RCOLS , _1, _2, _3, _4
A CM_RCOLS , _1, _2, _3
A I 3
A K 4
A NAME cm_rcols
☑ This topic is solved.

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

Discussion stats
  • 3 replies
  • 147 views
  • 2 likes
  • 3 in conversation