BookmarkSubscribeRSS Feed
rfarmenta
Obsidian | Level 7

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

9 REPLIES 9
robby_beum
Quartz | Level 8

How about using:

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

or

fev_max=MAX(fev11, fev12, fev13);

rfarmenta
Obsidian | Level 7

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!

robby_beum
Quartz | Level 8

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!

Linlin
Lapis Lazuli | Level 10

Hi Robby,

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

robby_beum
Quartz | Level 8

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

rfarmenta
Obsidian | Level 7

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!

Reeza
Super User

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

Linlin
Lapis Lazuli | Level 10

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

MikeZdeb
Rhodochrosite | Level 12

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 v:);

  else output;

end;

run;

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

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
  • 9 replies
  • 15090 views
  • 1 like
  • 5 in conversation