Sorting Data

The SORT Procedure

By default, PROC PRINT lists observations in the order in which they appear in your data set. To sort your report based on values of a variable, you must use PROC SORT to sort your data before using the PRINT procedure to create reports from the data.
The SORT procedure does the following:
  • rearranges the observations in a SAS data set
  • creates a new SAS data set that contains the rearranged observations
  • replaces the original SAS data set by default
  • can sort on multiple variables
  • can sort in ascending or descending order
  • treats missing values as the smallest possible values
Note: PROC SORT does not generate printed output.
Syntax, PROC SORT step:
PROC SORT DATA=SAS-data-set <OUT=SAS-data-set>;
BY <DESCENDING> BY-variable(s);
RUN;
  • The DATA= option specifies the data set to be read.
  • The OUT= option creates an output data set that contains the data in sorted order.
  • BY-variable(s) in the required BY statement specifies one or more variables whose values are used to sort the data.
  • The DESCENDING option in the BY statement sorts observations in descending order. If you have more that one variable in the BY statement, DESCENDING applies only to the variable that immediately follows it.
CAUTION:
If you do not use the OUT= option, PROC SORT overwrites the data set that is specified in the DATA= option.

Example: PROC SORT

proc sort data=cert.admit out=work.wgtadmit;    /*#1*/
  by weight age;
run;
proc print data=work.wgtadmit;                  /*#2*/
  var weight age height fee;                    /*#3*/
  where age>30;                                 /*#4*/
run;
1 The PROC SORT step sorts the permanent SAS data set Cert.Admit by the values of the variable Age within the values of the variable Weight. The OUT= option creates the temporary SAS data set Wgtadmit.
2 The PROC PRINT step prints a subset of the Wgtadmit data set.
3 The VAR statement selects only the variables Weight, Age, Height, and Fee to print in the output.
4 The WHERE statement subsets the data by printing only those observations where the values of Age are greater than 30.
The report displays observations in ascending order of Age within Weight.
Figure 6.9 Observations Displayed in Ascending Order of Age within Weight
Observations Displayed in Ascending Order of Age Within Weight
Adding the DESCENDING option to the BY statement sorts observations in ascending order of age within descending order of weight. Notice that DESCENDING applies only to the variable Weight.
proc sort data=cert.admit out=work.wgtadmit; 
   by descending weight age; 
run; 
proc print data=work.wgtadmit; 
   var weight age height fee;  
   where age>30; 
run;
Figure 6.10 Observations Displayed in Descending Order
Observations Displayed in Descending Order
Last updated: August 23, 2018
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.133.124.21