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
BASUG is hosting free webinars Next up: Mike Sale presenting Data Warehousing with SAS April 10 at noon ET. Register now at the Boston Area SAS Users Group event page: https://www.basug.org/events.

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
BASUG is hosting free webinars Next up: Mike Sale presenting Data Warehousing with SAS April 10 at noon ET. Register now at the Boston Area SAS Users Group event page: https://www.basug.org/events.
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-2024.png

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.

 

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

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