SAS Numeric Format – SAS Informat & Output Format
We read in the earlier tutorials that while defining a variable, it is sometimes required that we specify a SAS format for the same.
Today we will be learning about the SAS Numeric Format, types of Numeric Format in SAS Programming Language: SAS Informat and SAS Output Formats.
Moreover, how SAS Numeric format & types of numeric formats used in a program to meet our needs. At last, we will discuss syntax of Informat and output format of SAS Numeric format.
Let’s start with SAS Numeric Format.
SAS informat is one of the components of SAS Numeric Formate, specify how we wish to read a particular data. They are specified in the INPUT statement while defining variables.
A decimal (.) is always placed at the end of the informat and that is how SAS differentiates it from other variables. The informat instructs SAS on how to read data into SAS variables.
The syntax for a SAS informat is:
- Variable_name is the name of the variable that will be read using the informat.
- Format_name is the name we wish to give to the informat variable.
- w is the maximum number of columns that can be stored with the variable.
- d is the number of digits to the right of the decimal.
There are different types of SAS informat available depending upon the data type of variable. These are:
SAS Character Informat: $INFORMAT_namew.
SAS Numeric Informat: INFORMAT_namew.d
Date/Time Informat: INFORMAT_namew.
Stay updated with latest technology trends
Join DataFlair on Telegram!!
Reading SAS Numeric Format
There are two components in a SAS numeric format. The number of columns (width) v of the output and the number of decimal places. The SAS system uses floating-point representation referred to us as W.D, where W is the width and D is the number of digits to the right of the decimal place.
As an example, 8.2 will allocate a total of 8 spaces for the output. One space will be for the decimal and 2 for the digits to the right of the decimal; this will leave 5 spaces for the digits to the left of the decimal.
In the below table, numeric data 12345.66 is demonstrated how to allocate digits and decimal in the SAS system using 8.2 formats.
Input SAS Numeric Format (INFORMATS)
|n.||Maximum “n” number of columns with no decimal point.|
|n.p||Maximum “n” number of columns with “p” decimal points.|
|COMMAn.p||Maximum “n” number of columns with “p” decimal places which removes any comma or dollar signs.|
SAS Output Format
SAS Output Format is an instruction for displaying data in SAS. It is another component of SAS Numeric Format.
Example – If there is a salary column in our dataset, salary being a numeric data type will have decimal places in it. Let the salary be 3467.201.
Now, a person reading this look at this salary in terms of dollars, so we would want to place a dollar sign before it. Like this – $3467.20. For this, we would have to use an informat to instruct SAS that we want to read this salary in terms of dollars.
Now, consider somebody who reads this as three thousand four hundred and sixty-seven dollars. He would want to have commas after every 3 digits, to get a better understanding. In that case, it would look like this – $3,467.201
Now, say we want only 2 digits after the decimal, again we would have to specify in the informat the no. of digits we want after the decimal. Example- 3467.20
There are 3 common types of SAS Output formats for displaying numeric data:
- Simple W.D format: described in the above example of 8.2 formats. The SAS system writes the number in the total of W spaces, one space for decimal and D spaces for the numbers of decimals.
- CommaW.D format: as an example of comma8.2 will allocate a total of 8 spaces for the output. 1 space is allocated for the decimal, 2 spaces for the number of decimals and 1 space for comma as a separator in every 3 digits. The below is an example for the number 1,234.66 using 8.2 formats.
- DollarW.D format: in an example of dollar8.1 will allocate a total of 8 spaces for the output. 1 space is reserved for the decimal, 1 spaces for the number of decimals and 1 spaces for a dollar sign and 1 space for comma as a separator in every 3 digits. Here we display the number $1,234.9 in 8.1 formats.
We see that the COMMAw.d informat used above has a different function from the COMMAw.d format. SAS informat ignores dollar signs and commas while the COMMAw.d format outputs data includes commas but no dollar sign.
SAS Numeric Format Examples
At below, we discuss an example of SAS Numeric Format with it’s output/result.
DATA MYDATA1; input x 6.; /*maximuum width of the data*/ format x 6.3; datalines; 8722 93.2 .1122 15.116 PROC PRINT DATA = MYDATA1; RUN; DATA MYDATA2; input x 6.; /*maximum width of the data*/ format x 5.2; datalines; 8722 93.2 .1122 15.116 PROC PRINT DATA=MYDATA2; RUN; DATA MYDATA3; input x 6.; /*maximum width of the data*/ format x DOLLAR10.2; datalines; 8722 93.2 .1122 15.116 PROC PRINT DATA=MYDATA3; RUN;
1 8722.0 # Display 6 columns with zero appended after a decimal.
2 93.200 # Display 6 columns with zero appended after a decimal.
3 0.112 # No integers before a decimal, so display 3 available digits after a decimal.
4 15.116 # Display 6 columns with 3 available digits after a decimal.
1 8722 # Display 5 columns. Only 4 are available.
2 93.20 # Display 5 columns with zero appended after a decimal.
3 0.11 # Display 5 columns with 2 places after a decimal.
4 15.12 # Display 5 columns with 2 places after a decimal.
1 $8,722.00 # Display 10 columns with leading $ sign, a comma at a thousandth place and zeros appended after the decimal.
2 $93.20 # Only 2 integers available before decimal and one available after the decimal.
3 $0.11 # No integers available before decimal and two available after the decimal.
4 $15.12 # Only 2 integers available before decimal and two available after a decimal
Hence, today we discovered some great ways of reading SAS numeric Format by specifying SAS Informat and SAS Output format. So, that we get our data the way we want it too. Wasn’t this interesting? You can post your doubts and queries in the comments section below.