0
votes

IN SAS - I was asked to add a certain variable to an XLS file and then print out max and min 3 observations of this variable (in this case Hsal). I sorted out the data by this variable, but I don't know how to print out several min/max values -

data SAL5; set SAL1;
Hsal=(salary/hours);
run;
proc print data=SAL5;run;

proc sort data=SAL5;by Hsal;run;
1

1 Answers

3
votes

You have a few options, depending on how you want the data displayed and whether you want to show the max/min 3 observations or values (i.e. do you want to display tied values more than once or not, this won't be a problem with your data if all values are unique).

If you want the output on 1 line then using the idgroup statement in proc summary is the easiest way.

Otherwise you can take advantage of proc univariate, which calculates the top and bottom extreme observations or values by default. You need to use ods output to create the relevant dataset, which you can then print out. You'll probably want to restrict some of the variables from the output, which you can easily do with a drop statement or a var statement within proc print

I've given you all 3 options here. If you want to show the max/min values (option 3) on 1 line then you can do a proc transpose after the dataset is created. I've used sashelp.class as an example, just substitute age with Hsal in your data.

/* output on 1 line, max/min 3 observations */
proc summary data=sashelp.class nway;
output out=maxminobs1 (drop=_:) 
        idgroup (max(age) out[3] (age) = max_age)
        idgroup (min(age) out[3] (age) = min_age);
run;

proc print data=maxminobs1;
run;

/* output on 3 lines, max/min 3 observations */
ods output extremeobs = maxminobs2;
proc univariate data=sashelp.class nextrobs=3;
var age;
run;

proc print data=maxminobs2;
run;

/* output on 3 lines, max/min 3 values */
ods output extremevalues = maxminobs3;
proc univariate data=sashelp.class nextrval=3;
var age;
run;

proc print data=maxminobs3;
run;