SAS Formats

Ultimate Guide to SAS Formats and Informats

  • Post author:
  • Post category:Base SAS
  • Post comments:1 Comment
  • Reading time:22 mins read

SAS format is the instruction that specifies how the value of a variable should be printed or displayed, and SAS informats are the specification for how raw data should be read.

What is a SAS format?

SAS Format tells SAS how to print the variable. Formats and informats for a variable may or may not be the same for the variable.

There are many inbuilt SAS formats and informats, and you can also create your formats and informats using the PROC FORMAT procedure.

SAS Formats and informats are of three main types. These are characters, Numeric and dates.

Type Informat Name What it Does
Character $w. Reads in character data of length w.
Numeric w.d Reads in numeric data of length w with d decimal points
Date MMDDYYw. Reads in date data in the form of mm-dd-yy

For a complete list of built-in formats, you can refer to SAS 9.4 Formats and Informats: Reference.

Every variable in SAS will have a format – whether you assign one or you let SAS assign one automatically.

SAS informats are declared when you are reading in data or creating a new variable in a data step, whereas the format statement can be used in either a data step or a proc step:

Syntax:

FORMAT variable-name <$>FORMAT-NAME.;

$ → indicates a character format; its absence indicates a numeric format.

FORMAT variable-name <$>FORMAT-NAME.;

SAS Format always contains a period (.) as a part of the name. Default values are used if you omit the format’s w and the d values.

The d value you specify with SAS formats indicates the number of decimal places. The internal data values are not truncated or changed with the formats.

For example, in DOLLAR10.2, the w value of 10 specifies printing a maximum of 10 columns for the value, and the d value of 2 specifies that two of these columns are for the decimal part of the value. This leaves eight columns for all the remaining characters in the value.

The remaining columns include the decimal point, the remaining numeric value, a minus sign if the value is negative, the dollar sign, and commas, if any.

SAS tries to adjust the value into the space available if the format width is too narrow to represent a value. If adequate is not specified, SAS prints asterisks. In the following example, the result is x=**.

x=123;
put x= 2.; 

Character formats truncate values on the right. Numeric formats sometimes revert to the BESTw.d format.

If incompatible SAS formats are used, such as using a numeric format to write character values, SAS first attempts to use a comparable format of the other type. If this attempt fails, an error message that describes the problem appears in the SAS log.

When the value of d is greater than fifteen, the precision of the decimal value after the 15th decimal place might not be accurate.

Ways to Specify SAS Formats

You can use formats in the following ways:

  • In a PUT statement
  • With the PUT, PUTC, or PUTN functions
  • With the %SYSFUNC macro function
  • In a FORMAT statement in a DATA step or a PROC step
  • In an ATTRIB statement in a DATA step or a PROC step.

PUT Statement

The PUT statement with a format after the variable name uses a format to write data values in a DATA step.

For example, the PUT statement with the DOLLARw.d format can be used to write the numeric value for AMOUNT as a dollar amount:

amount=1145.32;
put amount dollar10.2;

The DOLLARw.d format in the PUT statement produces this result as $1,145.32

PUT Function

PUT function is used to convert a numeric variable to a character variable that returns the resulting character value.

For example, the following statement converts the value of a numeric variable into a two-character hexadecimal representation.

num=10;
char=put(num,hex2.);

The PUT function returns a value of 0A, assigned to the variable CHAR. The PUT function is also useful for converting a numeric value to a character value.

Using %SYSFUNC

The %SYSFUNC (or %QSYSFUNC) macro function executes SAS functions and applies the format to the result of the function outside the DATA step.

For example, the below program writes a numeric value in a macro variable as a dollar amount.

%macro test(amount);
%put %sysfunc(putn(&amount,dollar10.2));
%mend test;
%test (1154.23);

FORMAT Statement

SAS Formats can be used with the FORMAT statement. Using a Format statement permanently associates a format with a variable. SAS uses the format to write the values of the variable you specify.

For example, the following statement in a DATA step associates the COMMAw.d numeric format with the variables SALES1 through SALES3:

format sales1-sales3 comma10.2;

Note: All leading blanks are trimmed if you assign formats with a FORMAT statement before a PUT statement.

Formats associated with variables by using a FORMAT statement behave like formats used with a colon (:) modifier in a subsequent PUT statement.

ATTRIB Statement

The ATTRIB statement can also be used to specify a format for one or more variables.

For example, in the following statement, the ATTRIB statement permanently applies the COMMAw.d format for the variables SALES1 through SALES3:

format sales1-sales3 comma10.2;

Permanent and Temporary SAS Formats

When you specify a format in a PUT statement, SAS uses the format to write data values during the DATA step. Still, it does not permanently apply the format with that variable.

To permanently apply a format to a variable, you can use a FORMAT statement or an ATTRIB statement in a DATA step.

Using a FORMAT statement or an ATTRIB statement in a PROC step associates a format with a variable for that PROC step and for any output data sets that the procedure creates that contain formatted variables.

SAS date formats

The date formats can be used wherever formats are supported in the SAS language, and the applying methods remain the same as discussed earlier.

Built-in SAS Date formats

The table below shows how the data value corresponding with August 15, 2019 (SAS date 21776) would appear when the format is applied.

Format  Description Output
date5. ddmmmm 44423
date7. ddmmmyy. 43692
date9. ddmmmyyyy. 43692
date11. dd-mmm-yyyy 43692
day2. writes date values as the day of the month 15
ddmmyy8. dd/mm/yy 7167
ddmmyyd10. dd-mm-yyyy 43692
downame. writes date values as the name of the weekday Thursday
e8601da. yyyy-mm-dd 43692
mmddyy8. mm/dd/yy 08/15/19
mmddyyd10. mm-dd-yyyy 08-15-2019
monname. writes date values as the name of the month August
month2. writes date values as the number of the month 8
monyy7. mmmyyyy 43678
qtr1. writes date values as the quarter of the year 3
weekdate. day-of-week,month-name dd,yyyy Thursday, August 15, 2019
weekdatx. dat-of-week,dd month-name yyyyy Thursday, 15 August 2019
worddate. month-name dd,yyyy August 15, 2019
worddatex. dd month-name yyyy 43692
year4. writes date values as the year 2019
yymmddp10. yyyy.mm.dd 2019.08.15

Built-in SAS time formats

The table below shows how the time value corresponding with 15 seconds after 1:14 PM (SAS time 47655) would appear when the format is applied.

Format  Description Output
time5. hh:mm 13:14
time8. hh:mm:ss 13:14:15
time11.2 hh:mm:ss:ss 13:14:15.00
timeampm8. hh:mm AM or PM 1:14 PM
timeampm11. hh:mm:ss AM or PM 0.5515625

Built-in SAS DateTime formats

The table below shows how the DateTime value corresponding with 15 seconds after 1:14 PM on December 31, 2018 (SAS DateTime 1,861,881,255) would appear when the format is applied.

Format  Description Output
dateampm. ddmmmyy:hh:mm:ss AM or PM 15AUG
datetime18. ddmmmyy:hh:mm:ss 15AUG19
datetime20. ddmmmyyyy:hh:mm:ss 15AUG2019
e8601dt. yyyy-mm-ddThh:mm:ss.ffffff. 15-AUG-2019
mdyampm25. mm/dd/yyyy hh:mm AM or PM 15

Informat

An informat is an instruction that SAS uses to read data values into a variable. For example, the following value contains a dollar sign and commas: $1,000,000

You can remove the dollar sign ($) and commas (,) before storing the numeric value 1000000 in a variable by applying the COMMA11. informat to this value.

Syntax :

<$>informat. 

$ indicates a character informat; its absence indicates a numeric informat.
informat names the informat. The informat is a SAS informat or a user-defined informat that was previously defined with the INVALUE statement in PROC FORMAT.

  • w specifies the informat width, which for most informats is the number of columns in the input data.
  • d specifies an optional decimal scaling factor in the numeric informats. SAS divides the input data by 10 to the power of d.

Unless you explicitly define a variable first, SAS uses the informat to determine whether the variable is numeric or character. SAS also uses the informat to determine the length of character variables.

Informats always contain a period (.) as a part of the name. If you omit the w and the d values from the informat, SAS uses default values.

If the data contain decimal points, SAS ignores the d value and reads the number of decimal places that are actually in the input data.

If the informat width is too narrow to read all the columns in the input data, you might get unexpected results.

This problem frequently occurs with the date and time informats. To avoid this, you must adjust the width of the informat to include blanks or special characters between the day, month, year, or time.

When a problem occurs with an informat, SAS writes a note to the SAS log and assigns a missing value to the variable.Problems usually occur if you use an incompatible informat, such as a numeric informat to read character data, or if you specify the width of a date and time informat that causes SAS to read a special character in the last column.

Using informats

  • in an INPUT statement
  • with the INPUT, INPUTC, and INPUTN functions
  • in an INFORMAT statement in a DATA step or a PROC step
  • in an ATTRIB statement in a DATA step or a PROC step.

INPUT Statement

The INPUT statement with an informat after a variable name is the simplest way to read values into a variable. For example, the following INPUT statement uses two informats:

data test;
FORMAT DT MMDDYY8.;
input @1 id $3. @4 price comma10.3 @14 dt mmddyy10.;
datalines;
ID1 $1250.03 02/14/2020
;
run;
Ultimate Guide to SAS Formats and Informats

The $w. character informat reads values into the variable id. The commaw.d numeric informat reads values into the variable PRICE and mmddyy8. reads date informat into the dt variable.

INPUT Function

The INPUT function is used to convert a SAS character expression using a specified informat. The informat determines whether the resulting value is numeric or character.

TempCharacter='98.6';
TemperatureNumber=input(TempCharacter,4.);

In the above example, the INPUT function is combined with the w.d informat that converts the character value of TempCharacter to a numeric value and assigns the numeric value 98.6 to TemperatureNumber.

You can use the PUT function with a SAS format to convert numeric values to character values. For a complete discussion of the INPUT function, see our article on Variable Conversion in SAS.

INFORMAT Statement

The INFORMAT statement applies an informat to a variable. SAS uses the specified informat in any subsequent INPUT statement to read values into the variable.

For example, in the following statements, the INFORMAT statement associates the $3 informat with the variables id, comma10.3 with variable price and mmddyy10. for variable dt.

data test;
informat id $3. price comma10.3 dt mmddyy10.;
FORMAT DT MMDDYY8.;
input id price dt;
datalines;
ID1 $1250.03 02/14/2020
;
run;

An informat associated with an INFORMAT statement behaves like an informat that you specify with a colon (:) format modifier in an INPUT statement.

Therefore, SAS uses a modified list input to read the variable so that the w value in an informat does not determine column positions or input field widths in an external file.

The blanks embedded in input data are treated as delimiters unless you change the DLM= or DLMSTR= option in an INFILE statement.

For character informats, the w value in an informat specifies the length of the character variable. For numeric informats, the w value is ignored.

If the INPUT statements use another style of input, such as formatted or column input, that input style is not used when you use the INFORMAT statement.

ATTRIB Statement

The ATTRIB statement can also associate an informat and other attributes with one or more variables. For example, in the following statements, the ATTRIB statement associates the DATEw. informat with the variables Birthdate and Interview:

attrib Birthdate Interview informat=date9.;
input @63 Birthdate Interview;

If the informat is applied by using the INFORMAT= option in the ATTRIB statement, it behaves like an informat that you specify with a colon (:) format modifier in an INPUT statement.

In this case, SAS uses a modified list input and reads the variables are read in the same ways as it does for the INFORMAT statement.

SAS does not permanently apply the informat to the variable unless an INFORMAT statement or an ATTRIB statement is used.

Date Informats

Sometimes we may have formatted text that represents a date and/or time information, and we wish to store this information in a SAS data set as date, time, or DateTime values.

SAS DateTime Informats

Format  Description Output
DATE. ddmmmm 15AUG
DATETIME. ddmmmyy. 15AUG19
DDMYY. ddmmmyyyy. 15AUG2019
E8601DA. dd-mmm-yyyy 15-AUG-2019
E8601DT. writes date values as the day of the month 15
MMDDYY. dd/mm/yy 43692
TIME. dd-mm-yyyy 15-08-2019
YYMMDD. writesdate values as the name of the weekday Thursday
e8601da. yyyy-mm-dd 2019-08-15

Most of the informats listed above provides some degree of flexibility in the exact formatting of the text they read.

For example, the TIME. informat will work with colons separating the hours, minutes, and seconds and also with periods, slashes, dashes, and other characters.

It also works irrespective of whether “PM” is in uppercase or lowercase.

For more flexibility, SAS also includes a set of three “ANY” informats that will correctly interpret a wide variety of formatted date and time information.

SAS Any Date and Time Informats

Format  Description Output
ANYDTDDTE. ddmmmm 15AUG
ANYDTDTM. ddmmmyy. 15AUG19
ANYDTTME. ddmmmyyyy. 15AUG2019

Every week we'll send you SAS tips and in-depth tutorials

JOIN OUR COMMUNITY OF SAS Programmers!

Subhro

Subhro provides valuable and informative content on SAS, offering a comprehensive understanding of SAS concepts. We have been creating SAS tutorials since 2019, and 9to5sas has become one of the leading free SAS resources available on the internet.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

This Post Has One Comment

  1. Skn

    Nice explanation