BookmarkSubscribeRSS Feed
Sarah2
Calcite | Level 5

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.

 

 

13 REPLIES 13
novinosrin
Tourmaline | Level 20

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 

 

Sarah2
Calcite | Level 5

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.

Sarah2
Calcite | Level 5

%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.

Astounding
PROC Star

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.

Sarah2
Calcite | Level 5

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.

Astounding
PROC Star

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.

Sarah2
Calcite | Level 5

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);

Astounding
PROC Star

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.

Reeza
Super User

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;

 

 

Sarah2
Calcite | Level 5

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?

 

Reeza
Super User

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_:;

 

 

thomp7050
Pyrite | Level 9

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.

Tom
Super User Tom
Super User

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;

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

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
  • 13 replies
  • 2026 views
  • 1 like
  • 6 in conversation