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

Hi folks,

I have some problem to call a series of macro variables from a table.

 

Say I have a table stored some values, namely:

Table1

x1   x2    x3

5      7      6

 

I want to use the call symputx function to call all the macro variables at the same time. Basically I am wrting a code like:

 

Data storedvalue;

Table1;

do i=1 to 3;

call symputx("macroi",xi);

end;

run;

 

However, it's not working since i is not evaluated. Any idea on this question?

 

 

1 ACCEPTED SOLUTION

Accepted Solutions
RW9
Diamond | Level 26 RW9
Diamond | Level 26
data storedvalue;
  set table1;
  array x{3};
  do i=1 to 3;
    call symputx(cats('macro',put(i,best.)),x{i});
  end;
run;

As you can see you missed a few points, a set statement, an array statement.  Plus you need to create  string for the macro name, and refer to the array for the value. 

I would also advise you to consider why you want to create lots of macro variables, there is likely a better method.

 

 

 

View solution in original post

4 REPLIES 4
RW9
Diamond | Level 26 RW9
Diamond | Level 26
data storedvalue;
  set table1;
  array x{3};
  do i=1 to 3;
    call symputx(cats('macro',put(i,best.)),x{i});
  end;
run;

As you can see you missed a few points, a set statement, an array statement.  Plus you need to create  string for the macro name, and refer to the array for the value. 

I would also advise you to consider why you want to create lots of macro variables, there is likely a better method.

 

 

 

liyongkai800
Obsidian | Level 7
Thanks a lot. I want to use those values in the late data step, but I don't know how to call them, that's why I want to store them as macro variables. Is it possible that I have some statement like:
Data want;
set whatever;
/** call the value from stored value**/;
/**calculation here**/;
run;
RW9
Diamond | Level 26 RW9
Diamond | Level 26

Depends on the exact problem, the simplest method is just to merge the X row onto any data where you want to use it.

Amir
PROC Star

Hi,

 

I came up with an almost identical solution as @RW9:

 

data have;
   input x1 x2 x3;
   datalines;
5      7      6
;

%let macro1=;
%let macro2=;
%let macro3=;

data _null_;
   set have;

   array x(3);

   do i = 1 to 3;
      call symputx(cats('macro',i),x[i]);
   end;
run;

%put macro1=&macro1;
%put macro2=&macro2;
%put macro3=&macro3;

 

 

From the documentation, an advantage of using the cats() function is:

 

The CATS function removes leading and trailing blanks from numeric arguments after it formats the numeric value with the BESTw. format.

 

For more details see:

 

http://support.sas.com/documentation/cdl/en/lefunctionsref/63354/HTML/default/viewer.htm#n1e21rr6al5...

 

 

Regards,

Amir.

SAS Innovate 2025: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

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
  • 4 replies
  • 1039 views
  • 2 likes
  • 3 in conversation