In this video, I'll show you how to use SAS formats and the FORMAT statement to enhance data values in a report.
I'm going to start by using a PROC PRINT statement to generate a listing report of the customers table in the libsas library so you can
see some of the values that I want to change.
I'll run the report.
The report displays customer data, including an ID, name, country, gender, birthdate, and the customer's total purchases.
The values in Total are difficult to read. It would help to have commas inserted into the values, and maybe a leading dollar sign.
The Birth_Date values don't look like dates at all, and some have negative values.
That's because SAS stores dates as numeric values using a base date of January 1, 1960, so people born before 1960 have a negative
birthdate.
Numeric dates are great for calculations and sorting, but they don't make sense in reports.
To make these values easier to interpret, you can apply formats to the data.
A format is an instruction that tells SAS how to display a value.
SAS includes a library of formats.
This table shows several specific SAS formats and their effect on stored values.
These are just a few of the formats you can use.
Formats can be particularly useful when you want to control the display of monetary, date, and time values.
You can use a FORMAT statement to associate a particular format with a column.
The FORMAT statement starts with the keyword FORMAT, followed by one or more columns, and the format you want to apply to each of the
columns.
You can format as many columns as you'd like in a single FORMAT statement.
A SAS format begins with a format-name, followed by a number representing the total field width needed to display the values for the
column.
This width must include all of the characters and symbols that occupy a position in the final displayed value.
You need to make sure this number is large enough to display the largest value for this column, otherwise, values might not be
displayed properly.
The format ends with a period.
If you want to display decimal places in your values, you can place the number of decimal places to the right of the period.
If you do this, remember to add the number of decimal places and the period in the number you specified for the width of the values.
In this program, I am applying the COMMA9.2 format to Total.
It's values will display in a field 9 positions wide with 2 decimal places, and a comma that separates every three digits.
For Birth_Date I chose the DATE9. format.
It displays the calendar date, with a two-digit day, three-letter month abbreviation, and four- digit year. This is a total width of 9.
I'll run the report.
That's better! Now I can read the dates and the totals more easily.
By the way, formats only change the way a value is displayed. They do not change the stored value.
Okay, let's go back to the code and look at another example.
This time I'll use DOLLAR10.2 to format the values of Total.
It's similar to the comma format, but it adds a leading dollar sign.
I changed the width to 10 because the column must be wide enough to include the dollar sign.
For Birth_Date, I chose the MMDDYY10. format.
I'll run the program so you can see these format changes.
Birth_Date is displayed as a two-digit month, two-digit day, and four-digit year, separated by slashes.
Total has a leading dollar sign, comma, and two decimal places.
When a FORMAT statement is used in a PROC step, the format only affects the output from that step.
You can store formats as a column attribute in a SAS table by including a FORMAT statement in the DATA step.
I'll add a new DATA step that contains a FORMAT statement.
This DATA step reads the libsas.customers table and creates a new table named customersNew in the libsas library.
The FORMAT statement causes the formats to be stored in the table.
I'll highlight and submit this new code.
Let's look at the new table, libsas.customersNew.
Notice that the data is now formatted. The stored formats are used automatically.
Formats only change the way a value is displayed. They do not change the stored value.
What if I run a PROC PRINT step without a FORMAT statement?
I'll add a PROC PRINT step to display the new table.
Then, I'll highlight the code, and run the program.
This output is also formatted.
PROC PRINT and many other procedures automatically apply formats that are stored in the table.
Now that you've seen how to format data values, it's your turn to give it a try.