Erica Jh Lee
Erica Jh Lee

Reputation: 121

SAS- Combining multiple variables into one by choosing the maximum value

Combining multiple variables into one by choosing the maximum value

id v1 v2 v3 v4 v5 v6
1  1  2  5  3  1  1
2  4  2  3     5  1
3  3     2  2  1  3
4  2  1  2  5  7  
5  6  7  1  2  1  7

into n1=max(v1,v2), n2=v3, n3=max(v4,v5,v6)

id n1 n2 n3
1  2  5  3
2  4  3  5
3  3  2  3
4  2  2  7
5  7  1  7

How do I do this in SAS? (It's so easy in excel.. It's relatively intuitive in R.. But I can't figure it out in SAS! Please help!)

Thank you for your time!

Upvotes: 0

Views: 5601

Answers (2)

catquas
catquas

Reputation: 720

I agree that the MAX function is what you want, but I would code it differently.

data want;
set have;
n1 = max(v1, v2);
n2 = v3;
n3 = max(v4, v5, v6);
run;

Alternatively:

data want;
set have;
n1 = max(v1, v2);
n2 = v3;
n3 = max(of v4-v6);
run;

Upvotes: 1

Joe
Joe

Reputation: 63424

MAX function is your friend.

data want;
set have;
n1 = max(of v1 v2);
n2 = v3;
n3 = max(of v4 v5 v6);
run;

Arrays and variable lists also work (such as, n3 = max(of v4-v6);).

Upvotes: 5

Related Questions