BookmarkSubscribeRSS Feed
paul_1
Calcite | Level 5

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

8 REPLIES 8
art297
Opal | Level 21

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

paul_1
Calcite | Level 5

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

tish
Calcite | Level 5

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.

tish
Calcite | Level 5

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.

data_null__
Jade | Level 19

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.

ballardw
Super User

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.


ScottBass
Rhodochrosite | Level 12

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


Please post your question as a self-contained data step in the form of "have" (source) and "want" (desired results).
I won't contribute to your post if I can't cut-and-paste your syntactically correct code into SAS.
chang_y_chung_hotmail_com
Obsidian | Level 7

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+
  --*;

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

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
  • 8 replies
  • 1366 views
  • 3 likes
  • 7 in conversation