BookmarkSubscribeRSS Feed
lansoprazole
Obsidian | Level 7

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,

9 REPLIES 9
PaigeMiller
Diamond | Level 26

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.

--
Paige Miller
lansoprazole
Obsidian | Level 7
Could you tell me how?

PaigeMiller
Diamond | Level 26

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).

--
Paige Miller
SASKiwi
PROC Star

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;
FreelanceReinh
Jade | Level 19

Hi @lansoprazole,

 

You can use the MIN function and a couple of minus signs.

 

Spoiler
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."

 

 

Tom
Super User Tom
Super User

I agree it is pretty silly request.

He is a LESS efficient way to do it the way MAX() would.

 

Spoiler

 

data want ;
  set have;
  call sortn(of value1-value3);
  max=value3;
  drop value1-value3;
run;

 

 

ballardw
Super User

@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."

 

 

Ksharp
Super User
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;

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

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
  • 968 views
  • 1 like
  • 8 in conversation