Search the site...

SASCRUNCH TRAINING
  • Home
  • Member's Area
  • How to Start
  • SAS Interface
  • Creating a Data Set
  • Practical SAS Training Course
  • SAS Certified Specialist Training Program
  • Proc SQL Course
  • Introduction to Time Series Analysis
  • SAS Project Training Course
  • Full Training / Membership
  • Sign up
  • About us
  • Contact us
  • Home
  • Member's Area
  • How to Start
  • SAS Interface
  • Creating a Data Set
  • Practical SAS Training Course
  • SAS Certified Specialist Training Program
  • Proc SQL Course
  • Introduction to Time Series Analysis
  • SAS Project Training Course
  • Full Training / Membership
  • Sign up
  • About us
  • Contact us
Data Manipulation [1-18]
(Sample)


Sorting the Data Set
Picture
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.
Picture

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.
Picture

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.

​
Picture

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.
Picture

This tells SAS to sort the data set by the NAME variable.

(try it!)
Picture

Let's take a look at another example.

Example

Proc Sort Data=Drinks;
By Calories;
Run;
Picture

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;
Picture

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 module 7.

Exercise

Sort the DRINKS data set by FAT in descending order within each drinks type (i.e. Cold and Hot). 
Next

Need some help? 


HINT:
You need to have two (2) variables in the BY statement.


SOLUTION:
Proc Sort Data=Drinks;
By Type Descending Fat;
Run;


Fill out my online form.
Already a member? Go to member's area.