Sorting the Data Set
Sorting the data set is one of the most common data manipulation tasks in SAS.
It can be done by using a procedure called Proc Sort.
Example
Copy and run the DRINKS data set from the yellow box below.
The DRINKS data set contains a list of hot and cold drinks from Starbucks.
It also has the information about the calories and fat associated with each drink.
The data set is currently sorted by a variable called TYPE (in the first column).
The cold drinks are listed followed by the hot drinks.
You can easily change the order of the data set by using Proc Sort.
Example
Proc Sort Data=Drinks;
By Name;
Run;
The procedure Proc Sort has a BY statement on the NAME variable.
This tells SAS to sort the data set by the NAME variable.
(try it!)
Let's take a look at another example.
Example
Proc Sort Data=Drinks;
By Calories;
Run;
With a BY statement on the CALORIES variable, the data set is sorted by the CALORIES in ascending order.
Sorting in Descending Order
By default, the procedure sorts the data set in ascending order.
If descending order is preferred, you can simply add the option DESCENDING before the variable in the BY statement.
Example
Proc Sort Data=Drinks;
By Descending Calories;
Run;
The data set is now sorted by calories in descending order!
Sorting the data set is often required prior to statistical analysis that involves multiple segments.
This will be explained in lesson 6.
Exercise
Sort the DRINKS data set by FAT in descending order within each drinks type (i.e. Cold and Hot).
You need to have two (2) variables in the BY statement.
Proc Sort Data=Drinks;
By Type Descending Fat;
Run;
Cookie | Duration | Description |
---|---|---|
cookielawinfo-checkbox-analytics | 11 months | This cookie is set by GDPR Cookie Consent plugin. The cookie is used to store the user consent for the cookies in the category "Analytics". |
cookielawinfo-checkbox-functional | 11 months | The cookie is set by GDPR cookie consent to record the user consent for the cookies in the category "Functional". |
cookielawinfo-checkbox-necessary | 11 months | This cookie is set by GDPR Cookie Consent plugin. The cookies is used to store the user consent for the cookies in the category "Necessary". |
cookielawinfo-checkbox-others | 11 months | This cookie is set by GDPR Cookie Consent plugin. The cookie is used to store the user consent for the cookies in the category "Other. |
cookielawinfo-checkbox-performance | 11 months | This cookie is set by GDPR Cookie Consent plugin. The cookie is used to store the user consent for the cookies in the category "Performance". |
viewed_cookie_policy | 11 months | The cookie is set by the GDPR Cookie Consent plugin and is used to store whether or not user has consented to the use of cookies. It does not store any personal data. |