SAS Format – Specialise in User-defined Format & Built-in Format

FREE Online Courses: Your Passport to Excellence - Start Now

After learning the concept of SAS syntax, we will move on to particularly discuss methods to work with the SAS format of data values.

It should be noted that these changes are only applied while displaying the results. Changing the format of output does not change the way, the data gets stored at the back end.

Let’s start with the definition of SAS format.

What is SAS Format?

SAS format decides the way in which we want our data to get displayed. For example, we may have coded Male and Female as M and F (or 0 and 1), but while printing we would want to display the field as MALE and FEMALE only.

Another common example is to display area codes in 10 digit telephone numbers (e.g. 123-3456-789).

Types of Formats in SAS

Here, we will discuss two types of SAS formats:

1. Built-in Format

We looked at SAS format while discussing SAS Numeric Format, where we applied the SAS format on a date and got desired results. Let us again make use of built-in SAS format through an example:

Suppose we have a dataset where we want sales-amount in dollars with a decimal position. We can make use of built-in format, dollarw.d in the dataset.

Proc print Data=Sales;
Format Salesamount Dollar9.1 ;
Run;

Output:-

SAS Format

List of predefined date formats available to change the output format of variables in SAS:

SAS Format

2. User-defined Format

Till now we have seen, how to change the SAS format of numbers and Dates with in built SAS formats. But there can be many occasions when SAS built-in formats do not suffice our needs. Like in the current dataset, we want to:

  1. Display “MALE” and “FEMALE” instead of “M” and “F”.
  2. Re-define categories A, B, C, D and E as Ultra, Super, Average, Low and Poor.
  3. Display the frequency of Sales amount in three categories “< 8000”,”8000-12000” and “>=12000”.

If we only want to change the display (and not the values in the data set), then creating a user-defined format using PROC FORMAT is a more efficient way to make these changes.

PROC SAS FORMAT is a procedure that creates a mapping of data values.

The syntax of User-Defined Format – 

Proc Format;
Value Format_Name Range1='Label1'
                  Range2='Label2'
                  Range2='Label2'
                  Range3='Label3'
                  ........
                  ....... ;
Run;

 

SAS Format

 Rules for defining FORMAT NAME:

  1. For character values, the first character must be a dollar sign ($), and a letter or underscore as the second character. For numeric values, a name must have a letter or underscore as the first character.
  2. A name cannot end with a number.
  3. Cannot be the name of an existing SAS format.
  4. Should not end with a period in the VALUE statement.

Example – Displaying M and F as Male and Female.

Proc Format;
Value $Genderfmt ‘M’=’Male’
‘F’ =’Female’;
Run;
Proc Print Data = Sales ;
Format Gender $Genderfmt. ;
Run;

Output:

SAS Format

Let us look at another example – we want to display the sales amount ending with a % sign and preceded with a $ sign.

Proc Format;
Picture New_fmt low-High=’000000%’ (Prefix=’$’);
Run;
Proc print data=Sales;
Format Salesamount New_fmt.;
Run;

Output:

SAS Format

This comes to the end of the SAS tutorial.

Summary

We looked at various methods to display the SAS format of data values using built-in and user-defined formats. We have already covered In-format in previous articles. Hope you found this article useful. If you need any help, please feel free to ask your questions through the comments below.

Did we exceed your expectations?
If Yes, share your valuable feedback on Google

follow dataflair on YouTube

2 Responses

  1. Nishant Kumar says:

    your new GUI is very good

Leave a Reply

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