Into to SAS ®
2 List the components of a SAS program. Open an existing SAS program and run it. Objectives
3 DATA steps are typically used to create SAS data sets. PROC steps are typically used to process SAS data sets (that is, generate reports and graphs, edit data, and sort data). A SAS program is a sequence of steps that the user submits for execution. Raw Data DATA Step Report SAS Data Set PROC Step SAS Programs
4 data work.staff; infile 'raw-data-file'; input LastName $ 1-20 FirstName $ JobTitle $ Salary 54-59; run; proc print data=work.staff; proc means data=work.staff; class JobTitle; var Salary; run; Step Boundaries
5 Interactive windows enable you to interface with SAS. SAS Windowing Environment
6 Open the SAS program “example.sas.” Submit the program and examine the results. Data for today's class located at Exercises
7 Learn the two fundamental SAS syntax programming rules. Learn to create a SAS dataset from a text file. Write a Data Step to read a course data file. Objectives
8 SAS statements have these characteristics: usually begin with an identifying keyword always end with a semicolon data staff; input LastName $ FirstName $ JobTitle $ Salary; datalines; …insert text here… run; proc print data=staff; run; Fundamental SAS Syntax Rules
9 Reading Raw Data Files /11/00LAX /11/00DFW /12/00LAX /12/00dfw /13/00LAX /13/00DFW /14/00LaX /14/00DFW /15/00LAX /15/00DFW Data for flights from New York to Dallas (DFW) and Los Angeles (LAX) is stored in a raw data file. Create a SAS data set from the raw data.
10 data SAS-data-set-name; infile 'raw-data-filename'; input input-specifications; Creating a SAS Data Set /11/00LAX /11/00DFW /12/00LAX Raw Data File DATA Step SAS Data Set In order to create a SAS data set from a raw data file, you must do the following: 1.Start a DATA step and name the SAS data set being created (DATA statement). 2.Identify the location of the raw data file to read (INFILE statement). 3.Describe how to read the data fields from the raw data file (INPUT statement). run;
11 Creating a SAS Data Set General form of the DATA statement: Example:This DATA statement creates a temporary SAS data set named dfwlax : Example:This DATA statement creates a permanent SAS data set named dfwlax : DATA libref.SAS-data-set(s); data work.dfwlax; libname ia 'SAS-data-library'; data ia.dfwlax;
12 Pointing to a Raw Data File General form of the INFILE statement: Examples: The PAD option in the INFILE statement is useful for reading variable-length records typically found in Windows and UNIX environments. z/OS (OS/390) infile 'userid.prog1.dfwlax'; UNIX infile '/users/userid/dfwlax.dat'; Windows infile 'c:\workshop\winsas\prog1\dfwlax.dat'; INFILE 'filename' ;
13 Reading Data Fields General form of the INPUT statement: input-specifications names the SAS variables identifies the variables as character or numeric specifies the locations of the fields in the raw data can be specified as column, formatted, list, or named input. INPUT input-specifications;
14 Reading Data Using Column Input Column input is appropriate for reading the following: data in fixed columns standard character and numeric data General form of a column INPUT statement: Examples of standard numeric data: The term standard data refers to character and numeric data that SAS recognizes automatically E E-3 INPUT variable startcol-endcol... ;
15 Reading Data Using Column Input 43912/11/00LAX /11/00DFW /12/00LAX input Flight $ 1-3 Date $ 4-11 Dest $ FirstClass Economy 18-20;...
16 Creating Temporary SAS Data Sets NOTE: The data set WORK.DFWLAX has 10 observations and 5 variables. data work.dfwlax; infile 'raw-data-file'; input Flight $ 1-3 Date $ 4-11 Dest $ FirstClass Economy 18-20; run; Store the dfwlax data set in the work library. c06s1d1
17 Assignment Statements: Creating variables NOTE: The data set WORK.DFWLAX has 10 observations and 5 variables. data work.dfwlax; infile 'raw-data-file'; input Flight $ 1-3 Date $ 4-11 Dest $ FirstClass Economy 18-20; Total=Firstclass+Economy; LogEconomy=LOG(Economy); run; c06s1d1
18 Formatted input is appropriate for reading the following: data in fixed columns standard and nonstandard character and numeric data calendar values to be converted to SAS date values Reading Data Using Formatted Input
19 Date values that are stored as SAS dates are special numeric values. A SAS date value is interpreted as the number of days between January 1, 1960, and a specific date. 01JAN JAN JAN /01/195901/01/ /01/1961 informat format Working with Date Values
20 General form of the INPUT statement with formatted input: Reading Data Using Formatted Input INPUT pointer-control variable informat... ; Formatted input is used to read data values by doing the following: moving the input pointer to the starting position of the field specifying a variable name specifying an informat
21 Pointer the pointer to column n. +nmoves the pointer n positions. An informat specifies the following: the width of the input field how to read the data values that are stored in the field Reading Data Using Formatted Input
22 informat-namew. An informat is an instruction that SAS uses to read data values. SAS informats have the following form: Informat name Number of decimal places Indicates a character informat What Is a SAS Informat? Total width of the field to read Required delimiter
23 w. standard numeric informat Raw Data Value Informat SAS Data Value 8.0 Selected Informats 8.0
24 COMMAw. reads numeric data and removes selected nonnumeric characters such as dollar signs and commas. Raw Data Value Informat SAS Data Value MMDDYYw. reads dates of the form mm/dd/yyyy. Raw Data Value Informat SAS Data Value COMMA7.0 MMDDYY8. Selected Informats
25 $8. Selected Informats $w. standard character informat (removes leading blanks) Raw Data Value Informat SAS Data Value
26 SAS uses date informats to read and convert dates to SAS date values. 10/29/2001 MMDDYY /29/01 MMDDYY OCT2001 DATE /10/2001 DDMMYY Informat Raw Data Value Converted Value Examples: Number of days between 01JAN1960 and 29OCT2001 Converting Dates to SAS Date Values
/11/00LAX /11/00DFW /12/00LAX Reading Data: Formatted Input Flight Date Dest FirstClass Economy 3.;...
28 Raw Data File 43912/11/00LAX /11/00DFW /12/00LAX Reading Data: Formatted Input data work.dfwlax; infile 'raw-data-file'; Flight Date Dest FirstClass Economy 3.; run; c06s2d1
29 The SAS System First Obs Flight Date Dest Class Economy LAX DFW LAX dfw LAX DFW LaX DFW LAX DFW Reading Data: Formatted Input proc print data=work.dfwlax; run; SAS date values c06s2d1
30 List Input with the Default Delimiter The data is not in fixed columns. The fields are separated by spaces. There is one nonstandard field. 51 4feb nov oct feb jun dec
31 Delimiters tab characters A space (blank) is the default delimiter. blankscommas Common delimiters are
32 List Input General form of the INPUT statement for list input: You must specify the variables in the order that they appear in the raw data file. Specify a $ after the variable name if it is character. No symbol after the variable name indicates a numeric variable. INPUT var-1 $ var-2... var-n;
33 Input Data The second field is a date. How does SAS store date values? feb nov oct feb jun dec
34 Informats To read nonstandard data, you must apply an informat. General form of an informat: Informats are instructions that specify how SAS reads raw data. INFORMAT-NAME.
35 Specifying an Informat To specify an informat when using list input, use the colon (:) format modifier in the INPUT statement between the variable name and the informat. General form of a format modifier in an INPUT statement: INPUT variable : informat;
36 Reading a Delimited Raw Data File data airplanes; infile 'raw-data-file'; input ID $ InService : date9. PassCap CargoCap; run;
37 Non-Default Delimiter The fields are separated by commas , 4feb1989,132, , 11nov1989,152, , 22oct1991,90, , 4feb1993,172, , 24jun1993, 170, , 20dec1994, 180, 520
38 Using the DLM= Option The DLM= option sets a character or characters that SAS recognizes as a delimiter in the raw data file. General form of the INFILE statement with the DLM= option: Any character you can type on your keyboard can be a delimiter. You can also use hexadecimal characters. INFILE 'raw-data-file' DLM='delimiter(s)';
39 Multiple Records Per Observation A raw data file has three records per employee. Record 1 contains the first and last names, record 2 contains the city and state of residence, and record 3 contains the employee’s phone number. Farr, Sue Anaheim, CA Anderson, Kay B. Chicago, IL Tennenbaum, Mary Ann Jefferson, MO
40 Desired Output The SAS data set should have one observation per employee. LName FName City State Phone Farr Sue Anaheim CA Anderson Kay B. Chicago IL Tennenbaum Mary Ann Jefferson MO
41 data address; infile 'raw-data-file' dlm=','; input #1 LName $ FName $ #2 City $ State $ #3 Phone $; run; Reading Multiple Records per Observation...