The SAS Output Delivery System and reporting techniques

Looping through macro variable

Reply
N/A
Posts: 0

Looping through macro variable

Hi.
I am trying to loop through a macro variable and a field in a datastep, but am struggling with the coding.

This code shows what I would like to do:

%Let adjust0904 = 1.1;
%Let adjust0905 = 1.2;
%Let adjust0906 = 1.3;

DATA survey;
INPUT id mass0904 mass0905 mass0906 height0904 height0905 height0906;
DATALINES;
1 55 56 57 1.7 1.72 1.75
2 90 88 90 1.8 1.8 1.8
3 55 57 60 1.6 1.6 1.6
;
PROC PRINT; RUN;

Data B;
Set A;

Do i = '0904', '0905', '0906';
If mass&i > 85 Then BMI&i = mass&i * height&i * &adjust&i;
* code should read: If mass0904 > 85 Then BMI0904 = mass0904 * height0904 * &adjust0904;
End;

Run;

I realise that there are fundamental flaws with this code (e.g. using "i" as a macro variable), but I hope it shows what I would like to do. Any suggestions / solutions would be appreciated.
Thanks very much
Robin
Super Contributor
Super Contributor
Posts: 3,174

Re: Looping through macro variable

You are mixing SAS MACRO language processing with DATA step code. Your DO I=... statement must change to be a %DO;; %END; which will compile SAS DATA step code to be executed. Also, your DO (s/b %DO) construct is not valid and so you must find a suitable alternative to parse your macro variable using %LET assignment stmt and %SCAN macro function into a sub-field macro variable which you would reference in your code instead of the &I macro variable.

Scott Barry
SBBWorks, Inc.


SAS Macro Language Reference: Intro to Macro Facility
http://support.sas.com/documentation/cdl/en/mcrolref/61885/HTML/default/a002293969.htm

SAS Macro Programming for Beginners
Susan J. Slaughter, Avocet Solutions, Davis, CA
Lora D. Delwiche, Delwiche Consulting, Winters, CA
http://www2.sas.com/proceedings/sugi29/243-29.pdf
SAS Super FREQ
Posts: 8,740

Re: Looping through macro variable

Hi:
In addition to Scott's comments, it occurs to me that you may only need a simple array in order to perform repetitive calculations and not a SAS macro %DO loop at all. Something like this:
[pre]
array ms mass0904-mass0906;
array ht height0904-height0906;
array bmi bmi0904-bmi0906;
array aj adjust0904-adjust0906;
do i = 1 to 3 by 1;
if ms(i) gt 85 then bmi(i) = ms(i) * ht(i) *aj(i);
end;
[/pre]

Refer to this paper for an example of using a simple array -- Example 1.
http://support.sas.com/rnd/papers/sgf07/arrays1780.pdf

cynthia
N/A
Posts: 0

Re: Looping through macro variable

That is great, thanks! The only thing is - can you pass the macro variable adjust090x to an array in the datastep? Have tried a simple "array aj &adjust0904 - &adjust0906;" but SAS does not like this.
Cheers
Robin
SAS Super FREQ
Posts: 8,740

Re: Looping through macro variable

Hi:
I'm not sure I understand what you mean by:
pass the macro variable adjust090x to an array in a datastep

are you saying that you have %let statements, such as:
%let adj0904 = 4.5;
%let adj0905 = 5.5;
%let adj0906 = 6.5;
(or some other process that creates these macro variables...)

and then you want to reference those macro values as -though- they were in a data step array. Yes, you can. Look at the set up for a _TEMPORARY_ array in that paper (in Example 2). A temporary array contains a list of "constants", in your case, numeric constants and one way of creating the temporary array would be to hard-code the values. Another way would be to use macro variables.
[pre]
array aj{3} _temporary_ (4.5, 5.5, 6.5);

could also be defined as:

array aj{3} _temporary_ (&adj0904, &adj0905, &adj0906);
(assuming that the 3 macro variables used above all have values before the data step program starts processing.)
[/pre]

You could get more complex in the SAS macro program, however, it is better to start simple and get a working program and correct logic, before you go to town with fancy macro processing.

cynthia
N/A
Posts: 0

Re: Looping through macro variable

Cynthia, I think you have hit the jackpot!! It works perfectly on my small example, but will try it on the main program. Thanks to you guys for your help!
Cheers
Robin
Ask a Question
Discussion stats
  • 5 replies
  • 153 views
  • 0 likes
  • 3 in conversation