DATA Step, Macro, Functions and more

get the the array elements in a macro

Reply
New Contributor
Posts: 2

get the the array elements in a macro

Hi all,

My intention is to load data set in an array and to use the array in a macro.

The first step is trivial. The second setup i found it in a document it creates an array.  In the third step i can read array elements with %put test=&disease_1 etc.

But i must use it in the following wise, so that i don't have to write it manually like &disease_2. i want to change it to  &disease_x and x is a counter variable. Like following try:

%let x=3;

%put test=cats("&disease_",x);

If you have any other solution to fill array with dataset

elements, you are also welcome.

cheers

Serdar

/* 1-fill the data set*/

data work.myspace;

  input varname $;

DATALINES;

0+

0-

A+

A-

AB+

AB-

B+

B-

;

run;

/* 2- create an array with the elements from data set*/

data myset; Set myspace;

   call symputx (cats("disease_",_N_), varname);

run;

/* 3- Using*/

%put test=&disease_1

PROC Star
Posts: 7,473

Re: get the the array elements in a macro

You mention an array, but you never create one.  How do you want to use the macros that you created?

New Contributor
Posts: 2

Re: get the the array elements in a macro

Hi Arthur,

thank you for very quich feedback.

i found the code in page 3-4 of the document http://www.nesug.org/proceedings/nesug07/cc/cc27.pdf

i think, what i've created  macro variables at the top. But i would be ok for my problem, if i get also them.

if i change the step 2 into (without retain):

data myset; Set myspace;

      length my_array $ 50 ;

     my_array  = catx (' ', my_array , varname);

run;

i hope, so i would create an array. Still I can't get the data as described. And second problem, if i change the data in myspace, i receive still the old data,

thanks

Contributor
Posts: 52

Re: get the the array elements in a macro

The macro facility doesn't really have arrays, but you can create a macro variable containing a list and then loop over that list. So, instead of creating a data set with the values you want, set up a list:

%let disease_list = 0+ 0- A+ A- AB+ AB- B+ B-;

and then in the context of a macro, you can loop through the list:

%MACRO loop_thru_list;

     %do i = 1 %to 8;

          %let disease&i = %scan(&disease_list, &i, %str( ));

          %put disease&i = &&&disease&i;

     %end;

%MEND loop_thru_list;

%loop_thru_list;

This way you will get 8 macro variables named disease1 thru disease8.

Contributor
Posts: 52

Re: get the the array elements in a macro

You could also just load the value onto one macro variable, say &disease, and then do what you need just for that one, then loop back to do the next one.

Respected Advisor
Posts: 3,799

Re: get the the array elements in a macro

It is usually a bad idea to take data from a powerful data base object like a SAS data set and put those values into an object which by comparison one might consider useless.

Super User
Posts: 11,343

Re: get the the array elements in a macro

It may be time to describe what you are attempting to accomplish overall. Many people coming from other programming backgrounds see SAS elements as behaving like another program might and often use an approach that can be accomplished much simpler than trying to translate another programming language approach.


Super Contributor
Posts: 387

Re: get the the array elements in a macro

I'm in agreement with data and ballard.  For me, major red flags are going off that you don't need macro.

Can you post exactly what you want to do, with the caveat being that you can't mention "macro" in that message :-) ?  Just post a representative sample of your source data and target data, preferably via code using datalines that we can easily paste into SAS and run as is.

If you're just curious how you can move data between a SAS dataset and the macro symbol table, then fine, we can post code to do that.  Knowing *when* to do that in a "real" program is another kettle of fish.

Finally, I recommend you read Chapter 2 of the Macro documentation (it's quite short), and that you have a solid understanding of how SAS compiles a data step, with an understanding of the difference between compile time and run time processing, and when the macro processor handles macro tokens.

Hope this helps,

Scott

Regular Contributor
Posts: 241

Re: get the the array elements in a macro

So the OP's question is how to access an element of a so-called macro array, using an index. Here is a way. HTH.

  %let btype_1 = O+;
  %let btype_2 = O-;
  %let btype_3 = A+;
  %let btype_4 = A-;
  %let btype_5 = AB+;
  %let btype_6 = AB-;
  %let btype_7 = B+;
  %let btype_8 = B-;
 
  %macro btype(idx);
    %if &idx < 1 or 8 < &idx %then %return;
    %*;&&btype_&idx
  %mend  btype;
 
  %*-- check --*;
  %put blood type 1 = %btype(1);
  %put blood type 3 = %btype(3);
  %*-- on log
  blood type 1 = O+
  blood type 3 = A+
  --*;

Ask a Question
Discussion stats
  • 8 replies
  • 405 views
  • 3 likes
  • 7 in conversation