SAS Proc Sort

SAS Day 12: Proc Sort

We use Proc Sort to organize our data before presenting our final output such as plot the Waterfall graph or Merge with another dataset.

Basic Syntax:

Proc Sort data=a out=b;
by variable1;
run;

I have 6 favoriteoptions in Proc SortBy, Where, Nodupkey, Descending, Out, Dupout.

Example:  sashelp.cars

data car;
set sashelp.cars;
run;

 

congerdesign / Pixabay

 

  1. By: Sort the variables in the dataset according to the order of By ….
Proc sort data=car;
by make model ;
run;

 By Output:

 

2. Where: put a constraint in the sorting dataset

suppose we would like to see the records only made by BMW.

Proc sort data=car(where=(make="BMW"));
by model;
run;

Where Output:

 

3. Nodupkey: Remove the exactly duplicate records according to the By variable

Suppose we only care the brand and type of cars.

proc sort data=car nodupkey;
by make type;
run;

 Nodupkey Output:

 

4. Descending: Sort the dataset in a descending order according to the By variable

Suppose we would like to see the car price in descending order

proc sort data=car ;
by descending msrp;
run;

Descending Output:

 

5. Out:  Keep the original dataset and assign a new name the sorted data.

Suppose we would like to name the sorted car as “s_car“;

proc sort data=car out=s_car ;
by descending msrp;
run;

Out output:

 

6. Dupout: Store the duplicated records

Suppose we would like to see the duplicated records from ex.3.

proc sort data=car nodupkey dupout=dcar;
by make type;
run;

Dupout Output:

 

Note: I often see NODUP, however never really experienced. I just know it deletes a lot of records !!!

Happy SAS Coding! 💖

 

 

 

 

  One thought on “SAS Proc Sort

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Social media & sharing icons powered by UltimatelySocial
%d bloggers like this: