DATA Step, Macro, Functions and more

Call a series of macro variables

Accepted Solution Solved
Reply
Occasional Contributor
Posts: 12
Accepted Solution

Call a series of macro variables

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?

 

 


Accepted Solutions
Solution
4 weeks ago
Super User
Super User
Posts: 9,599

Re: Call a series of macro variables

Posted in reply to liyongkai800
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


All Replies
Solution
4 weeks ago
Super User
Super User
Posts: 9,599

Re: Call a series of macro variables

Posted in reply to liyongkai800
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.

 

 

 

Occasional Contributor
Posts: 12

Re: Call a series of macro variables

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;
Super User
Super User
Posts: 9,599

Re: Call a series of macro variables

Posted in reply to liyongkai800

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

Super Contributor
Posts: 339

Re: Call a series of macro variables

Posted in reply to liyongkai800

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.

☑ This topic is solved.

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

Discussion stats
  • 4 replies
  • 95 views
  • 2 likes
  • 3 in conversation