Hi,
Is there any way to find the max value for each observation without "max" and "if" in a data step?
INPUT:
ID value1 value2 value3
1 22 33 44
2 33 55 22
OUTPUT:
ID max
1 44
2 55
Thanks,
Why the restriction to not use MAX and not use IF???
SAS does a fine job of finding maximums with the tools provided, there's no need to write your own code to find maximums.
but to answer your question directly:
Is there any way to find the max value for each observation without "max" and "if" in a data step?
The answer is YES.
I note that you have not answered my question: why are you doing this?
Anyway, I have consistently advocated for people to not write their own code to do what SAS has already created. Not only is it unnecessary work, but SAS has debugged it and proved that it works in a bazillion real-world examples. If you write code to do this, then YOU have to verify that it works and vouch for it if there is ever a question. So my answer remains ... and the only answer I'm going to give (without strong justification from you why you can't use MAX) ... use the MAX function.
If this is a homework assignment, then my advice is also consistent over time ... first you give it a try, and if you get stuck we'll help if we can. (and if this is a homework assignment, I think your professor is teaching SAS very poorly, making students do from scratch what is already programmed in SAS).
Yet another way:
data have;
input ID value1-value3;
cards;
1 22 33 44
2 33 55 22
;
data want;
set have;
max = largest(1, of value1-value3);
put _all_;
run;
Hi @lansoprazole,
You can use the MIN function and a couple of minus signs.
data have;
input ID value1-value3;
cards;
1 22 33 44
2 33 55 22
;
data want(drop=v:);
set have;
max=-min(-value1,-value2,-value3);
run;
With this simple code missing values would cause a note in the log about "performing an operation on missing values."
I agree it is pretty silly request.
He is a LESS efficient way to do it the way MAX() would.
data want ;
set have;
call sortn(of value1-value3);
max=value3;
drop value1-value3;
run;
"I need to knock in a nail without using a hammer."
Sounds pretty stupid, doesn't it?
@lansoprazole wrote:
Hi,
Is there any way to find the max value for each observation without "max" and "if" in a data step?
INPUT:
ID value1 value2 value3
1 22 33 44
2 33 55 22
OUTPUT:
ID max
1 44
2 55
Thanks,
If this is an interview question the response I would give is :
"There is a way. But I shouldn't because it is likely to create code that is harder to maintain and follow when changes are needed and very likely to be less efficient at run time."
data have;
input ID value1 value2 value3 ;
cards;
1 22 33 44
2 33 55 22
;
data want;
set have;
array x{*} value:;
call sortn(of x{*});
max=x{dim(x)};
keep id max;
run;
Registration is now open for SAS Innovate 2025 , our biggest and most exciting global event of the year! Join us in Orlando, FL, May 6-9.
Sign up by Dec. 31 to get the 2024 rate of just $495.
Register now!
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.
Ready to level-up your skills? Choose your own adventure.