SAS Format – Specialise in User-defined Format & Built-in Format
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.
Keeping you updated with latest technology trends, Join DataFlair on Telegram
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;
List of predefined date formats available to change the output format of variables in SAS:
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:
- Display “MALE” and “FEMALE” instead of “M” and “F”.
- Re-define categories A, B, C, D and E as Ultra, Super, Average, Low and Poor.
- 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;
Rules for defining FORMAT NAME:
- 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.
- A name cannot end with a number.
- Cannot be the name of an existing SAS format.
- Should not end with a period in the VALUE statement.
Do you know? – How to Enter and Read Raw Data in SAS
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;
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;
This comes to the end of the SAS tutorial.
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.
Become an expert in SAS programming by reading SAS books.