To sort our data, we will use the PROC SORT statement. By default, SAS will sort the last data set you created save over it (same observations, different order). Let’s try sorting our table from yesterday by the number of fatalities:
/*Create table*/ DATA table; INPUT Year Country $ Fatalities Rate; DATALINES; 2015 USA 35092 11.3 2014 USA 32675 10.3 2013 USA 32719 10.3 2013 FRANCE 3268 5.1 ; RUN;
/*Sort table*/ PROC SORT; BY Fatalities; RUN;
Our table is now sorted by number of fatalities, from smallest to largest. Please note this is the default sort order for SAS (ascending). The default sort order for character variables is A to Z. Now, let’s say you wanted to keep your original table intact (not sorted). You would include the OUT option to tell SAS to save the sorted data set to a new table:
PROC SORT OUT = newTable; BY DESCENDING Fatalities; RUN;
This is an important concept to remember: procedures have a default way of doing things, but those defaults can be overridden by playing with the options. In this example, we replaced the default “ASCENDING” sort option with “DESCENDING” to change how our table is sorted.
Practice Question: How would you sort the table first by country, in alphabetical order, and then by most recent year?
a) PROC SORT DATA=table; BY Country DESCENDING Year; RUN;
b) PROC SORT DATA=table; BY DESCENDING Country Year; RUN;
c) PROC SORT DATA=table; BY Country Year DESCENDING; RUN;
d) PROC SORT DATA=table; BY Country ASCENDING Year; RUN;
The DESCENDING option should be placed before the variable you wish to sort in descending order, not after. By default, SAS sorts character variables from A to Z and numeric variables from smallest to largest. In this example, we used the DESCENDING option to tell SAS to sort the Year variable from largest to smallest.