BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
BillBaker_cd3
Calcite | Level 5

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;

 

1 ACCEPTED SOLUTION

Accepted Solutions
Quentin
Super User
It's a little terrifying, but looks like:
%put &&mem&i &&&&&&mem&i.._rcols &xx ;
works
The Boston Area SAS Users Group is hosting free webinars!
Next webinar will be in March 2025. Until then, check out our archives: https://www.basug.org/videos. And be sure to subscribe to our our email list.

View solution in original post

3 REPLIES 3
Quentin
Super User
It's a little terrifying, but looks like:
%put &&mem&i &&&&&&mem&i.._rcols &xx ;
works
The Boston Area SAS Users Group is hosting free webinars!
Next webinar will be in March 2025. Until then, check out our archives: https://www.basug.org/videos. And be sure to subscribe to our our email list.
BillBaker_cd3
Calcite | Level 5

thank you very much, I Stopped at 4 &.

Tom
Super User Tom
Super User

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

sas-innovate-wordmark-2025-midnight.png

Register Today!

Join us for SAS Innovate 2025, our biggest and most exciting global event of the year, in Orlando, FL, from May 6-9. Sign up by March 14 for just $795.


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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

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