Create a new variable that finds the max of other variables

Reply
Regular Contributor
Posts: 150

Create a new variable that finds the max of other variables

Hi! I have three variables, fev11, fev12, and fev13 and I need to create a new variable called fev_max that scans those three variables and finds that maximum value of the three to fill in fev_max. I need to do this by participant ID (PID). What is the best way to create this new variables. I have only ever done this using R and am not sure how to do it in SAS. Thank you in advance for any help you can give me. es

Contributor
Posts: 73

Re: Create a new variable that finds the max of other variables

Posted in reply to rfarmenta

How about using:

fev_max=LARGEST(1, fev11, fev12, fev13);

or

fev_max=MAX(fev11, fev12, fev13);

Regular Contributor
Posts: 150

Re: Create a new variable that finds the max of other variables

Posted in reply to robby_beum

That worked, thank you! I have never used the MAX or LARGEST functions in SAS so I did not know they existed! Thank you for your help!

Contributor
Posts: 73

Re: Create a new variable that finds the max of other variables

Posted in reply to rfarmenta

A really helpful book to get as you build your SAS library is:

"SAS Functions by Example" By Ron Cody.

Great functions and their usage in this book!

Super Contributor
Posts: 1,636

Re: Create a new variable that finds the max of other variables

Posted in reply to robby_beum

Hi Robby,

I like your original photo. Why did you make the change?

Contributor
Posts: 73

Re: Create a new variable that finds the max of other variables

HA! - I was just sitting around, had a few minutes on my hands and thought "why not change my picture!"

Regular Contributor
Posts: 150

Re: Create a new variable that finds the max of other variables

Posted in reply to robby_beum

Thank you for the book suggestion, I will look into purchasing it. Thank you everyone again for the code. These forums are a great place for people like me who still have a lot to learn about SAS and I appreciate being able to come here to get help!

Super User
Posts: 19,770

Re: Create a new variable that finds the max of other variables

Posted in reply to rfarmenta

Can a participant have more than one record, ie more than one line in the dataset?

Super Contributor
Posts: 1,636

Re: Create a new variable that finds the max of other variables

Posted in reply to rfarmenta

data have;
input id v1-v3;
cards;
1 20 21 19
1 30 32 39
2 15 18 13
;
data temp;
set have;
max=largest(1,of v1-v3);
proc sort;
by id descending max;

data want(drop=max);
   retain fev_max;
   set temp;
   by id;
   fev_max=ifn(first.id=1,max,fev_max);
proc print;
run;
Obs    fev_max    id    v1    v2    v3

1        39       1    30    32    39
2        39       1    20    21    19
3        18       2    15    18    13

Valued Guide
Posts: 765

Re: Create a new variable that finds the max of other variables

hi ... here's a variation on Linlin's idea (where there might be multiple observations with the same ID) ...

data want;

do until (last.id);

  set have (in=one) have;

  by id;

  if one then vmax = max(vmax, of vSmiley Happy;

  else output;

end;

run;

Ask a Question
Discussion stats
  • 9 replies
  • 386 views
  • 1 like
  • 5 in conversation