BookmarkSubscribeRSS Feed
deleted_user
Not applicable
Hi

I have a list of 8 variables and want to find the largest variable for each record.
For example if my data looked like this:
Key age1 age2 age3 age4
001 0.5 0.6 0.7 0.3
002 0.8 1.7 1.2 4.6
I would like to create a variable that looks like this:
Key age1 age2 age3 age4 maxv
001 0.5 0.6 0.7 0.3 age3
002 0.8 1.7 1.2 4.6 age4

Anyone know how I could do this, or something similar?
8 REPLIES 8
LinusH
Tourmaline | Level 20
Hi,
try the MAX-function (in a data step):

maxv = max(age1, age2, age3, age4); or in your specific case with a variable list:
maxv = max(of age1-age4);

Regards,
Linus
Data never sleeps
deleted_user
Not applicable
thanks Linus, this gave me the value of the maximum variable, whereas I was after the variable name, however I will definitely use this in the future!
sbb
Lapis Lazuli | Level 10 sbb
Lapis Lazuli | Level 10
One technique is to use a SAS macro variable to identify/track the age values, presuming the data sample you provided. Here's some SAS code that works.

Scott Barry
SBBWorks, Inc.

%let ages = age1 age2 age3 age4;
data _null_;
input Key $ &ages;
* use array to interrogate age# var list for max. ;
array _agevars (*) &ages ;
do i=1 to dim(_agevars);
if _agevars(i) gt _maxval then do;
drop _maxval;
* keep track of the max var-value thus far. ;
_maxval = _agevars(i);
* keep track of the var-name having max value thus far. ;
maxv = upcase(scan("&ages",i,' '));
end;
end;
putlog _all_;
datalines;
001 0.5 0.6 0.7 0.3
002 0.8 1.7 1.2 4.6
run;
deleted_user
Not applicable
thank you Scott that worked perfectly
deleted_user
Not applicable
you can use the function varname and varnum combine with macro to do that
deleted_user
Not applicable
use these two function you do not need to know the variname, only do loop on the variable number, but be ware that you should use %sysfun in the macro enviroment
Olivier
Pyrite | Level 9
Hi.
What seems to me a simpler solution -- no macros needed, just use the VNAME function.
[pre]
data out (drop = i max_age) ;
set in ;
array ages age: ;
max_age=MAX(OF age:) ;
do i=1 TO DIM(ages) ;
if ages(i) = max_age then maxv=VNAME(ages(i)) ;
end ;
run ;
[/pre]
Regards,
Olivier
sbb
Lapis Lazuli | Level 10 sbb
Lapis Lazuli | Level 10
Very clean, Olivier. The VNAME function had escaped me and so I used the macro variable (parsing) approach. Thanks for the great tip!

Regards,

Scott Barry
SBBWorks, Inc.

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!

What is Bayesian Analysis?

Learn the difference between classical and Bayesian statistical approaches and see a few PROC examples to perform Bayesian analysis in this video.

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
  • 8 replies
  • 909 views
  • 0 likes
  • 4 in conversation