## calculate the maximum value of a variable

Frequent Contributor
Posts: 128

# calculate the maximum value of a variable

Hi

Can we calculate the maximum value of a variable, in the data statement?
I don't want to use Proc means, since I need to merge this maximum value back to my dataset.

e.g. in the following dataset, do we have any function like

max_v2 = max( v2 ) , ??

-----------------------------
id v2

1 2
2 4
3 5
4 3
5 4
6 5
7 6
8 3

--------------------------------------

Thanks!!

Ken
Super Contributor
Posts: 474

## Re: calculate the maximum value of a variable

It can be done with datastep (by retaining the maximum value for example), but it is easier and clearer to be done through proc sql or even proc summary/means.

Give a look at the documentation of the RETAIN statement:
http://support.sas.com/documentation/cdl/en/lrdict/61724/HTML/default/a000214163.htm

Cheers from Portugal.

Daniel Santos @ www.cgd.pt.
Not applicable
Posts: 0

## Re: calculate the maximum value of a variable

Ken,

You don't say what you want to do with this maximum, so it is a bit hard to advise you. For example, you could figure out the maximum in a data step, but it wouldn't be known for sure until you get to the last observation. You could make an initial pass through the data file using the POINT option. Or, you could also use SQL:

proc sql noprint;
create table NEW as
select *, max(v2) as max_v2
from ORIGINAL
;
quit;
Frequent Contributor
Posts: 128

## Re: calculate the maximum value of a variable

Posted in reply to deleted_user
Thanks Kmg!!

This is exactly what I want!!

Thanks again! Problem FIXED!!
Discussion stats
• 3 replies
• 123 views
• 0 likes
• 3 in conversation