BookmarkSubscribeRSS Feed
deleted_user
Not applicable
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
5 REPLIES 5
sbb
Lapis Lazuli | Level 10 sbb
Lapis Lazuli | Level 10
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
Cynthia_sas
SAS Super FREQ
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
deleted_user
Not applicable
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
Cynthia_sas
SAS Super FREQ
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
deleted_user
Not applicable
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

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
  • 5 replies
  • 859 views
  • 0 likes
  • 3 in conversation