variablessasmaxminobservation

How to extract entire row of max and min using proc function in SAS?


I have to identify which sneaker (variable 1) is matched (same row) with the highest and lowest sales (variable 2) by using a SAS procedure function.

Current code:

PROC MEANS DATA = M4.sneaker min max; Class salesThisPeriod; RUN;


Solution

  • You can try using the IDGROUP feature of the OUTPUT statement of PROC MEANS (aka PROC SUMMARY). But to get both min and max you will need to write two output datasets.

    proc summary data=sashelp.class ;
      class sex;
      output out=min idgroup(min(age) out[1] (name age height weight)= );
      output out=max idgroup(max(age) out[1] (name age height weight)= );
    run;
    
    data both;
      length stat indsname $32 ;
      set min max indsname=indsname;
      by _type_ sex;
      stat=scan(indsname,-1,'.');
    run;
    

    Result

    Obs    stat    Sex    _TYPE_    _FREQ_     Name     Age    Height    Weight
    
     1     MIN               0        19      Joyce      11     51.3       50.5
     2     MAX               0        19      Philip     16     72.0      150.0
     3     MIN      F        1         9      Joyce      11     51.3       50.5
     4     MAX      F        1         9      Janet      15     62.5      112.5
     5     MIN      M        1        10      Thomas     11     57.5       85.0
     6     MAX      M        1        10      Philip     16     72.0      150.0