How to Use PROC FORMAT in SAS (With Examples)


You can use PROC FORMAT in SAS to create a mapping of data values into data labels.

This procedure uses the following basic syntax:

proc format;
    value points_range
        25-high='High'
        15-<25='Medium'
        other ='Low';
run;

This particular example creates the following mapping:

  • Values equal to 25 or greater will be shown as ‘High
  • Values between 15 and 25 will be shown as ‘Medium
  • All other values will be shown as ‘Low

The following examples show how to use PROC FORMAT with the following dataset in SAS:

/*create dataset*/
data my_data;
    input team $ position $ points;
    datalines;
A Guard 25
A Guard 20
A Guard 30
A Forward 25
A Forward 10
B Guard 10
B Guard 22
B Forward 30
B Forward 10
B Forward 10
B Forward 25
;
run;

/*view dataset*/
proc print data=my_data;

Example 1: Use PROC FORMAT to Format Values as Labels in Frequency Table

Suppose we use PROC FREQ to create a frequency table of values in the points column of the dataset:

/*calculate frequency of values in points column*/
proc freq data = my_data;
    table points;
run;

The output displays the frequency of each individual value in the points column.

However, suppose we would like to format the values as follows:

  • Values equal to 25 or greater will be shown as ‘High
  • Values between 15 and 25 will be shown as ‘Medium
  • All other values will be shown as ‘Low

We can use PROC FORMAT to do so:

/*define formatting for points variable*/
proc format;
    value points_range
        25-high='High'
        15-<25='Medium'
        other ='Low';
run;

/*create frequency table for points variable, using formatting defined above*/
proc freq data = my_data;
    table points;
    format points points_range.;
run;

The frequency table now groups the values of the points variable into the labels that we specified using the PROC FORMAT statement.

Example 2: USE PROC FORMAT to Create New Variable

We can also use PROC FORMAT to create a new variable in a dataset that converts data values into data labels.

The following syntax shows how to do so:

/*define formatting for points variable*/
proc format; 
    value points_range
        25-high='High'
        15-<25='Medium' 
        other ='Low';
run;

/*create new dataset with points_range variable*/
data new_data;
    set my_data;
    point_range = put(points, points_range.);
run;

/*view dataset*/
proc print data=new_data;

The new variable called ‘point_range’ takes on a value of Low, Medium or High depending on the corresponding value for the ‘points’ variable.

Note: You can find the complete documentation for PROC FORMAT here.

Additional Resources

The following tutorials explain how to perform other common tasks in SAS:

How to Use Proc Summary in SAS
How to Use Proc Tabulate in SAS
How to Use Proc Rank in SAS

Leave a Reply

Your email address will not be published. Required fields are marked *