Example: confidence

Introduction to SAS Informats and Formats

CHAPTER 1 Introduction to SAS Informats and Formats Chapter Using SAS Informats ..2 INPUT Statement ..3 INPUT Function ..7 INPUTN and INPUTC ATTRIB and INFORMAT Statements ..8 Using SAS Formats ..9 FORMAT Statement in Procedures ..10 PUT Statement ..11 PUT Function ..13 PUTN and PUTC BESTw. Format ..14 Additional Comments ..17 2 The Power of PROC FORMAT Chapter Overview In this chapter we will review how to use SAS Informats and Formats . We will first review a number of internal Informats and Formats that SAS provides, and discuss how these are used to read data into SAS and format output. Some of the examples will point out pitfalls to watch for when reading and formatting data.

Chapter 1: Introduction to SAS Informats and Formats 3 1.2.1 INPUT Statement One use of SAS informats is in DATA step code in conjunction with the INPUT

Tags:

  Introduction, Informats, Introduction to sas informats and

Information

Domain:

Source:

Link to this page:

Please notify us if you found a problem with this document:

Other abuse

Advertisement

Transcription of Introduction to SAS Informats and Formats

1 CHAPTER 1 Introduction to SAS Informats and Formats Chapter Using SAS Informats ..2 INPUT Statement ..3 INPUT Function ..7 INPUTN and INPUTC ATTRIB and INFORMAT Statements ..8 Using SAS Formats ..9 FORMAT Statement in Procedures ..10 PUT Statement ..11 PUT Function ..13 PUTN and PUTC BESTw. Format ..14 Additional Comments ..17 2 The Power of PROC FORMAT Chapter Overview In this chapter we will review how to use SAS Informats and Formats . We will first review a number of internal Informats and Formats that SAS provides, and discuss how these are used to read data into SAS and format output. Some of the examples will point out pitfalls to watch for when reading and formatting data.

2 Using SAS Informats Informats are typically used to read or input data from external files called flat files (text files, ASCII files, or sequential files). The informat instructs SAS on how to read data into SAS variables SAS Informats are typically grouped into three categories: character, numeric, and date/time. Informats are named according to the following syntax structure: Character Informats : $INFORMATw. Numeric Informats : Date/Time Informats : INFORMATw. The $ indicates a character informat. INFORMAT refers to the sometimes optional SAS informat name. The w indicates the width (bytes or number of columns) of the variable.

3 The d is used for numeric data to specify the number of digits to the right of the decimal place. All Informats must contain a decimal point (.) so that SAS can differentiate an informat from a SAS variable. SAS 9 lists other informat categories besides the three mentioned. Some of these are for reading Asian characters and Hebrew characters. The reader is left to explore these other categories. SAS provides a large number of Informats . The complete list is available in SAS Help and Documentation. In this text, we will review some of the more common Informats and how to use them. Check SAS documentation for specifics on reading unusual data.

4 Chapter 1: Introduction to SAS Informats and Formats 3 INPUT Statement One use of SAS Informats is in DATA step code in conjunction with the INPUT statement to read data into SAS variables. The first example we will look at will read a hypothetical data file that contains credit card transaction data. Each record lists a separate transaction with three variables: an ID (account identifier), a transaction date, and a transaction amount. The file looks like this: ID Transaction Date Transaction Amount 124325 08/10/2003 7 08/11/2003 114565 08/11/2003 The following program is used to read the data into a SAS data set.

5 Since variables are in fixed starting columns, we can use the column-delimited INPUT statement. Figure filename transact 'C:\BBU FORMAT\DATA\ '; data transact; infile transact; input @1 id $6. n @10 tran_date mmddyy10. o @25 amount p ; run; proc print data=transact; run; Starting Column INFORMAT VARIABLE 4 The Power of PROC FORMAT The ID variable is read in as a character variable using the $6. informat in line n. The $w. informat tells SAS that the variable is character with a length w. The $w. informat will also left-justify the variable (leading blanks eliminated).

6 Later in this section we will compare results using the $CHARw. informat, which retains leading blanks. Line o instructs SAS to read in the transaction date (Tran_Date) using the date informat MMDDYYw. Since each date field occupies 10 spaces, the w. qualifier is set to 10. Line p uses the numeric informat The informat provides instruction to read the numeric data having a total width of 8 (8 columns) with two digits to the right of the decimal point. SAS will insert a decimal point only if it does not encounter a decimal point in the specified w columns. Therefore, we could have coded the informat as 8. or The PROC PRINT output is shown here.

7 Note that the Tran_Date variable is now in terms of SAS date values representing the number of days since the first day of the year specified in the YEARCUTOFF option (for this run, yearcutoff=1920). Obs id tran_date amount 1 124325 15927 2 7 15928 3 114565 15928 Output We can make this example a bit more complicated to illustrate some potential problems that typically arise when reading from flat files. What if the Amount variable contained embedded commas and dollar signs?

8 How would we generate Chapter 1: Introduction to SAS Informats and Formats 5 the code to read in these records? Here is the modified data with the code that reads the file using the correct informat instruction: 124325 08/10/2003 $1, 7 08/11/2003 $12, 114565 08/11/2003 filename transact 'C:\BBU FORMAT\DATA\ '; data transact; infile transact; input @1 id $6. @10 tran_date mmddyy10. @25 amount n ; run; proc print data=transact; run; Line n uses the numeric informat named to tell SAS to treat the Amount variable as numeric and to strip out leading dollar signs and embedded comma separators.

9 The PROC PRINT output is shown here: Obs id tran_date amount 1 124325 15927 2 7 15928 3 114565 15928 Output Note that the output is identical to the previous run when the data was not embedded with commas and dollar signs. Also note that the width of the informat in the code is now larger (10 as opposed to 8 to account for the extra width taken up by commas and the dollar sign). What seemed like a programming headache was solved simply 6 The Power of PROC FORMAT by using the correct SAS informat.

10 When you come across nonstandard data, always check the documented Informats that SAS provides. Now compare what would happen if we changed the informat for the ID variable from a $w. informat to a $CHARw. informat. Note that the $CHARw. informat will store the variable with leading blanks. filename transact 'C:\BBU FORMAT\DATA\ '; data transact; infile transact; input @1 id $CHAR6. @10 tran_date mmddyy10. @25 amount ; run; proc print data=transact; run; Obs id tran_date amount 1 124325 15927 2 7 15928 3 114565 15928 Output Note that the ID variable now retains leading blanks and is right-justified in the output.


Related search queries