I am trying to get as a macro variable or a plain variable the number of the observation that is the min/max of one variable.
I can get easily the value of this min/max, via a proc sql
proc sql noprint;
select min(variable) into :minvariable
from have;
quit;
Or via a proc summary
proc summary data=want;
var variable;
output out=mintable min=minvariable;
run;
But I have not fouhd in either method an easy way to find the number of the observation this minimum corresponds to.
Background : I want to separate my sorted database based on this observation number, because I know that sorted in the right way there is a global minimum, and my variable of interest follows a U-curve
You can do this in a single pass through the data, using retain
. Where the same min/max value occurs multiple times, the observation of the first one seen is stored.
data want ; set have end=eof ; retain minval minobs maxval maxobs ; if value < minval or missing(minval) then do ; minval = value ; minobs = _n_ ; end ; if value > maxval or missing(maxval) then do ; maxval = value ; maxobs = _n_ ; end ; if eof then output ; run ;