This tutorial explains how to use FORMATS in SAS, along with examples.
SAS Formats
SAS Formats decide how to display values of a variable. They define the appearance of variables when they are printed or exported. For example, you can use a format to display a numeric variable as a currency, percentage. Formats do not change the underlying data values; they only affect their presentation.
Below is a list of common SAS Formats.
| Data Type | Formats | Explanation |
|---|---|---|
| Character | $w. | Displays character values of length w. |
| Numeric | w.d | Displays numeric values of length w with d decimal points |
| Date | DATEw. | Displays SAS date values of length w. |
| Time | TIMEw.d | Displays time values in the form hh:mm:ss.ss |
Character Formats
$w.: Displays character values to a specified width.$UPCASEw.: Displays character values in uppercase and optionally truncates them to a specified width.
The FORMAT statement is used to format values in a specific format. Here we are using the $UPCASEw. format.
data mydata; set sashelp.cars; format make $upcase.; run;
Let's say we specify width 3 $upcase3., it will truncate to first 3 letters (as shown in the image below).
data mydata; set sashelp.cars; format make $upcase3.; run;
Numeric Formats
COMMAw.d: Displays numeric values with commas and a specified number of decimal places.DOLLARw.d: Displays numeric values as currency with a dollar sign, commas, and a specified number of decimal places.PERCENTw.d: Displays numeric values as percentages with a specified number of decimal places.
The "dollar8." format specifies that the values of the "msrp" variable should be displayed as currency with a dollar sign and commas, with a width of 8 characters. The "msrp" variable represents the manufacturer's suggested retail price of the cars.
data mydata; set sashelp.cars; format msrp dollar8.; run;
If you only want commas in numeric values, you can use the format comma8.
data mydata; set sashelp.cars; format msrp comma8.; run;
In the code below, the percent8.2 format specifies that the "percent_gain" values should be displayed as percentages with two decimal places, with a total width of 8 characters.
data mydata; percent_gain = 0.835; format percent_gain percent8.2; run;
Result: 83.50%
Date Formats
| Date Formats | Result |
|---|---|
| date9. | 30DEC2022 |
| date11. | 30-DEC-2022 |
| YYMMDD10. | 2022-12-30 |
| MMDDYY10. | 12-30-2022 |
| DDMMYY10. | 30-12-2022 |
| WORDDATE. | DECEMBER 30, 2022 |
| DDMMYYP10. | 30.12.2022 |
| DDMMYYS10. | 30/12/2022 |
| MMDDYYP10. | 12.30.2022 |
| MMDDYYS10. | 12/30/2022 |
| WORDDATX19. | 30 DECEMBER 2022 |
The line format date date11.; specifies that the "date" variable in the "mydata" dataset should be formatted using the "date11." format. This format represents the date values in the format "dd-MON-yyyy". The resulting dates will have a width of 11 characters.
data mydata; set sashelp.pricedata; format date date11.; run;





Share Share Tweet