08-02-2012 11:51 AM
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:
If you have any other solution to fill array with dataset
elements, you are also welcome.
/* 1-fill the data set*/
input varname $;
/* 2- create an array with the elements from data set*/
data myset; Set myspace;
call symputx (cats("disease_",_N_), varname);
/* 3- Using*/
08-02-2012 12:46 PM
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);
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,
08-02-2012 12:49 PM
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:
%do i = 1 %to 8;
%let disease&i = %scan(&disease_list, &i, %str( ));
%put disease&i = &&&disease&i;
This way you will get 8 macro variables named disease1 thru disease8.
08-02-2012 12:55 PM
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.
08-02-2012 01:02 PM
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.
08-02-2012 01:33 PM
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.
08-02-2012 06:14 PM
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,
08-03-2012 10:24 AM
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-;
%if &idx < 1 or 8 < &idx %then %return;
%*-- check --*;
%put blood type 1 = %btype(1);
%put blood type 3 = %btype(3);
%*-- on log
blood type 1 = O+
blood type 3 = A+