Lesson 6: how to sort your data

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;
2015 USA 35092 11.3
2014 USA 32675 10.3
2013 USA 32719 10.3
2013 FRANCE 3268 5.1
/*Sort table*/
BY Fatalities;

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;

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;







Answer: a

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.



Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s