DATA Step, Macro, Functions and more

how to get macro variables from a dynamic value

Reply
Occasional Contributor
Posts: 6

how to get macro variables from a dynamic value

[ Edited ]

i have a dataset:eco which has some variables like a_1 a_2 a_3, etc.. this is what I want to get:

1,

%macro aa;

data final_d;

  do m = 1 to 100 

  set eco(keep=a_&m); /*&m doesn't work here, any good way to have macro variable to keep the dynamic m value?*/

    end;

run;

%mend;

%aa;

i know if i change to:

2,

%macro aa;

data final_d;

  %do m = 1 %to 100 

  set eco(keep=a_&m);

    %end;

run;

%mend;

%aa;

i can get the variables a_1 to a_100 very easily. but since the old code used lots of m in the program, i don't want to change. so any good way that I can get a_1 to a_100 in code 1, which I don't change do to %do, but to get the macro variables? thank you.

 

 

PROC Star
Posts: 283

Re: how to get macro variables from a dynamic value

In your

2. Are you trying to generate 100 set statements? or you want 100 vairables in a series in your keep= dataset option

what is that you actually want to accomplish? Please give more details with samples. Thanks 

 

Occasional Contributor
Posts: 6

Re: how to get macro variables from a dynamic value

Posted in reply to novinosrin

my purpose is to easily use macro variable a_&m, because I will use this variable for calculating. i don't want to have my datasets too big, so when m is 1, i will only get a_1 to do calculate. and when m is 2, i use a_2 to do calculate. so m is changing, it's dynamic in do loop. hope this helps. thank you.

Occasional Contributor
Posts: 6

Re: how to get macro variables from a dynamic value

%macro aa;

data final_d;

  %do m = 1 %to 100 

  set eco(keep=a_&m);

  ****************will do calculation with a_&m variable*********

    %end;

run;

%mend;

%aa;

 

but if i use do, i couldn't get a_&m variable. so how to get this variable in do loop. thank you.

Super User
Posts: 5,505

Re: how to get macro variables from a dynamic value

There are a few ways to go about this.  You could create 100 macro variables.  However, if you are really using a %DO loop for processing your calculations, it would be simpler to create a single macro variable and change its value.  For example:

 

%macro aa;

 

  %do m=1 %to 100;

      data _null_;

      set eco;

      call symputx('m_value', a_&m);

     run;

 

     ** Further processing that calculates using &M_VALUE;

 

   %end;

 

%mend aa;

 

This assumes you only have one observation in ECO.  If that's not the case, it would be very unclear what should go into the macro variable and how you would calculate.

Occasional Contributor
Posts: 6

Re: how to get macro variables from a dynamic value

Posted in reply to Astounding

yes, my code 2 can simple solve this problem. but, since i have lot of m has been used in my whole program, i can't change m to &m by using %do. so i have to keep do m=1 to 100; and also try to have a way to use macro variables to store this dynamic m value.

Super User
Posts: 5,505

Re: how to get macro variables from a dynamic value

OK, again this assumes there is just one observation in ECO:

 

data _null_;

set eco;

array a {100} a_1-a_100;

do i=1 to 100;

   call symputx('m_' || left(put(i, 3.)), a{i});

end;

run;

 

It creates 100 macro variables, where &M_1 holds the value of a_1, &M_2 holds the value of a_2, etc.

Occasional Contributor
Posts: 6

Re: how to get macro variables from a dynamic value

Posted in reply to Astounding

your array code can help to solve the problem. but my example here is not only for one array, so still, i want to have a macro variable to hold m value when m is changing in do m=1 to 100. i tried to use call symput too, but it has to been in data step, doesn't work in do i=1 to 100; call symput(k, m);

Super User
Posts: 5,505

Re: how to get macro variables from a dynamic value

There are lots of ways to do all the things you are talking about, but I can't spend my time guessing which ones you will need.  You will have to show some code that illustrates what you are trying to do.

Super User
Posts: 19,807

Re: how to get macro variables from a dynamic value

Posted in reply to Astounding

If this matters and is in a data step why can't you control the variables my making the loop go up to M rather than control the variables in the list.

 

 

do i = 1 to M;

 

 

Occasional Contributor
Posts: 6

Re: how to get macro variables from a dynamic value

Posted in reply to Astounding

i understand.. i have a long code under do loop, so my purpose is not changing do m=1 to 100; when m is 1, i want to keep only a_1 in eco. this a_*, * changes depending on m. hope this explain is better. i tried to use your array, so you will have &m_1 &m_2 for macro variables. now when we try to use these variable in: do a=1 to 100; set eco(keep=&m_*) ---how do you apply a=1 to the macro variable &m_1?

 

Super User
Posts: 19,807

Re: how to get macro variables from a dynamic value

Without knowing more information, I'm not sure we can offer a suggestion that would help here. 

 

Depending on how variables are being calculated can use you use the colon short cut at all?

 

keep a_:;

 

 

Frequent Contributor
Posts: 93

Re: how to get macro variables from a dynamic value

This creates 100 variables from A1 to A100.

 

PROC SQL OUTOBS = 100;
CREATE TABLE IDS AS
SELECT MONOTONIC() AS ID
FROM SASHELP.STOCKS;
QUIT;

PROC SQL;
SELECT 0 INTO: A1-:A100 FROM IDS;
QUIT;

Hacky?  Yes, but it works.

Super User
Super User
Posts: 7,050

Re: how to get macro variables from a dynamic value

If sounds like you want to pass in an upper bound on the number of similarly named variables.

Looks like a simple use of a varaible list.  

So you could use the variable list in a KEEP= dataset option.

Or anywhere else that you need to provide a list of variables. Like in a ARRAY statement.

%let m=10;
data want ;
   set have(keep=a_1 - a_&m);
   array avars a_1 - a_&m;
   do i=1 to &m ;
    ...
   end;
run;
Ask a Question
Discussion stats
  • 13 replies
  • 162 views
  • 1 like
  • 6 in conversation