Help using Base SAS procedures

new to array

Accepted Solution Solved
Reply
Valued Guide
Posts: 858
Accepted Solution

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: 8,743

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

View solution in original post


All Replies
Respected Advisor
Posts: 3,124

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

Respected Advisor
Posts: 4,651

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
Super User
Posts: 6,502

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: 8,743

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.

Need further help from the community? Please ask a new question.

Discussion stats
  • 4 replies
  • 204 views
  • 2 likes
  • 5 in conversation