SAS : PROC FORMAT with Examples

Deepanshu Bhalla 3 Comments

This tutorial explains the uses of PROC FORMAT in data analysis tasks. It includes several examples to help understand the practical application of PROC FORMAT.

Sample Data

In this tutorial, we will be using the dataset sashelp.cars for demonstration purpose. See the sample data shown in the image below.

PROC FORMAT
Sample Data

Example 1 : Suppose you are asked to group MSRP variable based on the following conditions and check the number of observations falling in each groups.

  1. Values greater than 40,000 should be labeled as 'High'
  2. Values between 26,000 and 40,000 should be labeled as 'Medium'
  3. Otherwise, label them as 'Low'
Solution
proc format;
value range
40000-high='High'
26000-< 40000='Medium'
other ='Low';
run;

proc freq data = sashelp.cars;
table msrp;
format msrp range.;
run;
PROC FORMAT Example

Example 2 : Same as with example 1. Here we are creating a new variable called TSRP based on the conditions applied on the MSRP variable.

Solution :
proc format;
value range
40000-high='High'
26000-< 40000='Medium'
other ='Low';
run;

data temp;
set sashelp.cars;
TSRP = put(msrp, range.);
run;

The put() function is used to apply the format. The line TSRP = put(msrp, range.); applies the custom format range to the msrp variable and stores the formatted values in a new variable named TSRP.

Example 3 : Subset Data - You can also filter data using PROC FORMAT.

The following code filters the dataset to only include observations where the formatted value is either 'High' or 'Medium'.

Method 1 :
data temp;
set sashelp.cars;
where put(msrp, range.) IN ('High' 'Medium');
run;
Method 2 :
data temp (where = (tsrp IN ('High' 'Medium')));
set sashelp.cars;
tsrp = put(msrp, range.);
run;
Method 3 :
proc sql;
select *,
put(msrp, range.) as tsrp
from sashelp.cars
where calculated tsrp in ('High', 'Medium');
quit;
Related Posts
Spread the Word!
Share
About Author:
Deepanshu Bhalla

Deepanshu founded ListenData with a simple objective - Make analytics easy to understand and follow. He has over 10 years of experience in data science. During his tenure, he worked with global clients in various domains like Banking, Insurance, Private Equity, Telecom and HR.

3 Responses to "SAS : PROC FORMAT with Examples"
  1. How to sum saary department wise using proc format

    ReplyDelete
  2. How do you make a format library auto-start for each new SAS session?

    ReplyDelete
Next → ← Prev