Solved
Valued Guide
Posts: 863

# new to array

Hello, I am trying to get the hang of arrays and having trouble.  The below code is intended to take current weight and get a goal weight.  Instead of doing that I am adding two variables and not calculating goal weight.  If anyone can see the errors and advise I would appreciate it.  Thanks:

data start;

infile datalines;

input name \$ sex \$ weight1 weight2;

cards;

sara f 120 119

bill m 220 219

;

run;

data end (drop=i);

set start;

array start_array{*} _numeric_;

array end_array{*} goal_weight1-goal_weight2;

if sex=f then do

i=1 to 2;

end_array{i}=start_array{i}*.95;

end;

else if sex=m then do

i=1 to 2;

end_array{i}=start_array{i}*.9;

end;

run;

Accepted Solutions
Solution
‎02-08-2013 06:14 PM
SAS Super FREQ
Posts: 9,367

## Re: new to array

Hi:

In answer to your more general question about arrays, I have always found this to be a good paper.

http://support.sas.com/rnd/papers/sgf07/arrays1780.pdf

But, as you learned, the problem was not so much with array processing as IF statement syntax and the need for quoting character strings in comparisons.

cynthia

All Replies
Posts: 3,167

## Re: new to array

Not sure if you have other issues with the code, but is it supposed to be like:

if sex='f' then do

else if sex='m' then do

Haikuo

Posts: 5,524

## Re: new to array

The only prroblem I see is the absence of quotes around f and m in your if statements. PG

PG
Super User
Posts: 8,089

## Re: new to array

Other than the missing quotes around the string literals and the strange indentation the program looks fine.

It is converting the existing weights to goal weights by multiplying by a fraction that depends on gender.

What did you want to do differently?

goal_      goal_

Obs    name    sex    weight1    weight2    weight1    weight2

1     sara     f       120        119        114       113.05

2     bill     m       220        219        198       197.10

Solution
‎02-08-2013 06:14 PM
SAS Super FREQ
Posts: 9,367

## Re: new to array

Hi:

In answer to your more general question about arrays, I have always found this to be a good paper.

http://support.sas.com/rnd/papers/sgf07/arrays1780.pdf

But, as you learned, the problem was not so much with array processing as IF statement syntax and the need for quoting character strings in comparisons.

cynthia

🔒 This topic is solved and locked.