Previous Page | Next Page

The SORT Procedure

Example 4: Retaining the First Observation of Each BY Group


Procedure features:

PROC SORT statement option:

NODUPKEY

BY statement

Other features:

PROC PRINT

Data set: ACCOUNT
Interaction: The EQUALS option, which is the default, must be in effect to ensure that the first observation for each BY group is the one that is retained by the NODUPKEY option. If the NOEQUALS option has been specified, then one observation for each BY group will still be retained by the NODUPKEY option, but not necessarily the first observation.

In this example, PROC SORT creates an output data set that contains only the first observation of each BY group. The NODUPKEY option prevents an observation from being written to the output data set when its BY value is identical to the BY value of the last observation written to the output data set. The resulting report contains one observation for each town where the businesses are located.


Program

 Note about code
options nodate pageno=1 linesize=80 pagesize=60;
 Note about code
proc sort data=account out=towns nodupkey;
 Note about code
   by town;
run;
 Note about code
proc print data=towns;
 Note about code
   var town company debt accountnumber;
 Note about code
   title 'Towns of Customers with Past-Due Accounts';
run;

Output

 Note about figure
                   Towns of Customers with Past-Due Accounts                   1

                                                                  Account
      Obs    Town             Company                    Debt      Number

       1     Apex             Paul's Pizza               83.00      1019 
       2     Garner           World Wide Electronics    119.95      1122 
       3     Holly Springs    Ice Cream Delight         299.98      2310 
       4     Morrisville      Strickland Industries     657.22      1675 

Previous Page | Next Page | Top of Page