Getting highest and lowest values
From sasCommunity
Q: How do I get the highest and/or lowest values from a data set?
For example, who are the three tallest and three shortest individuals in the SASHELP.CLASS data set?
Tiny Url: http://tinyurl.com/6cay92
[edit] Proc Freq
Note: This is a derivative work of SmryEachVar ProcFreq routine.
See: SmryEachVar A Data Review Suite
%Let Libname = SAShelp;
%Let Memname = Class;
%Let Name = Height;
%Let Nobs2View = 3;*show how many rows?;
%Let Order = internal;* default: highest and lowest values;
* orders values by their unformatted values;
%*Let Order = freq;* orders values by descending frequency count;
* returns mode values:;
* highest and lowest -occuring- values;
* i.e.: data set is sorted by descending Count;
PROC Freq data = &LibName..&MemName.
order = &Order.;
format &Name.;%*remove formatting;
tables &Name.
/ list missing noprint
out = Work.Freq;
DATA Nobs2View;
drop DsId Rc;
DsId = open ("&LibName..&MemName.");
NobsData = attrn(DsId,'nobs');
Rc = close(DsId);
Nlevels = NrowsFreq;
CardRatio = Nlevels/NobsData;
do RowNmbr = 1 to NrowsFreq;
set Work.Freq nobs = NrowsFreq
point = RowNmbr;
%* case 1: output all rows;
if NrowsFreq le %eval(2 * &Nobs2View. + 2)
then link Assigns;
else do; %* case 2: lo and hi &Nobs2View. rows;
if RowNmbr le &Nobs2View.
or RowNmbr ge NrowsFreq - &Nobs2View. then link Assigns;
else if RowNmbr gt &Nobs2View. then do;
RowNmbr = NrowsFreq - &Nobs2View.;
Level = RowNmbr;
end; %*else if RowNmbr gt &Nobs2View.;
end; %*else do: case 2;
end; %*do RowNmbr;
stop;
return;
Assigns: Level+ +1;
output;
return;
run;
Proc Print data = &SysLast.;
run;
Nobs Card Obs Data Nlevels Ratio Height COUNT PERCENT Level
1 19 17 0.89474 51.3 1 5.26316 1 2 19 17 0.89474 56.3 1 5.26316 2 3 19 17 0.89474 56.5 1 5.26316 3 4 19 17 0.89474 67.0 1 5.26316 15 5 19 17 0.89474 69.0 1 5.26316 16 6 19 17 0.89474 72.0 1 5.26316 17
[edit] Proc Univariate
from SAS-L: Are you forgetting that SAS has procedures that produce the needed statistics directly. The data set of extreme observations can be created easily using that information.
%Let Libname = SAShelp;
%Let Memname = Class;
%Let Name = Height;
%Let Nobs2View = 3;*show how many rows?;
PROC Univariate data = &Libname..&Memname
nextrobs = &Nobs2View ;
ods select ExtremeObs ;
ods output ExtremeObs = ExtremeObs;
var Height;
run;
DATA Work.View&Nobs2View;
set Work.Extremeobs (keep= LowObs rename= (LowObs = point) )
Work.Extremeobs (keep= HighObs rename= (HighObs = point) );
ObsN = point;
set &Libname..&Memname point = obsN;
run;
PROC Print;
run;
Benefit: includes Primary Keys in output data set
Obs point Name Sex Age Height Weight<br> 1 11 Joyce F 11 51.3 50.5 2 13 Louise F 12 56.3 77.0 3 2 Alice F 13 56.5 84.0 4 17 Ronald M 15 67.0 133.0 5 1 Alfred M 14 69.0 112.5 6 15 Philip M 16 72.0 150.0
