How to Create User-Defined Format in SAS?

How to Create User-Defined Format in SAS?

SAS is a powerful programming language used for data analysis, statistical analysis, and reporting. One of the key features of SAS is its ability to create and manipulate datasets. In order to effectively work with datasets, it is often necessary to define custom formats. Custom formats can be used to convert values in a dataset from one form to another. For example, you might want to convert a numerical value to a categorical variable. In this article, we will discuss how to create user-defined formats in SAS.

What is a SAS format?

A SAS format is a set of instructions that tells SAS how to display or interpret values in a dataset. SAS formats are used to convert values from one form to another. For example, you might have a dataset that contains numeric values for age, but you want to display age in categories such as "child," "adolescent," "adult," and "elderly." A SAS format can be used to define these categories and to assign each numeric value to the appropriate category.

SAS formats can also be used to format dates, times, and currency values. There are many built-in SAS formats that can be used for these purposes, but sometimes it is necessary to create a custom format.

Creating a user-defined format in SAS:

To create a user-defined format in SAS, you need to follow these steps:

Step 1: Define the format

The first step in creating a user-defined format is to define the format using the FORMAT procedure. The FORMAT procedure creates a new format and assigns it a name. The syntax for creating a new format is as follows:

PROC FORMAT;

    VALUE format-name

        low-high = 'format';

RUN;

The PROC FORMAT statement starts the FORMAT procedure. The VALUE statement creates a new format and assigns it a name (format-name). The low-high statement specifies the range of values that the format applies to. The format statement specifies how to format the values.

For example, to create a format that converts numeric values for age into categories, you might use the following syntax:

PROC FORMAT;

    VALUE agefmt

        0-12 = 'Child'

        13-18 = 'Adolescent'

        19-64 = 'Adult'

        65-high = 'Elderly';

RUN;

This code creates a format called agefmt that converts numeric values for age into categories. The values 0-12 are converted to 'Child', 13-18 are converted to 'Adolescent', 19-64 are converted to 'Adult', and values 65 and above are converted to 'Elderly'.

Step 2: Apply the format

Once you have defined the format, you need to apply it to a variable in a dataset. You can apply the format to a variable using the FORMAT statement in a DATA step. The syntax for applying a format is as follows:

DATA dataset-name;

    SET dataset-name;

    FORMAT variable-name format-name;

RUN;

The DATA statement starts the DATA step. The SET statement specifies the input dataset. The FORMAT statement applies the format to the variable specified by variable-name. The format is specified by format-name.

For example, to apply the agefmt format to a variable named age in a dataset named people, you might use the following syntax:

DATA people;

    SET people;

    FORMAT age agefmt.;

RUN;

DATA le; SET people; FORMAT age


This code applies the agefmt format to the age variable in the people dataset.

Step 3: Test the format

After you have defined and applied the format, you should test it to

To view or add a comment, sign in

More articles by Sankhyana Consultancy Services Pvt. Ltd.

Insights from the community

Others also viewed

Explore topics