understanding sas data step processing

21
Understanding SAS Data Step Processing Ravi Mandal

Upload: ravi-mandal

Post on 18-Jul-2015

118 views

Category:

Data & Analytics


2 download

TRANSCRIPT

Page 1: Understanding sas data step processing

Understanding SAS Data Step Processing

Ravi Mandal

Page 2: Understanding sas data step processing

Reading Raw Data

• Using the following SAS program:DATA NEW;

INPUT ID $ AGE TEMPC;

TEMPF=TEMPC*(9/5)+32;

DATALINES;

0001 24 37.3

0002 35 38.2

;

run;

proc print;run;

Ravi M., [email protected]

Page 3: Understanding sas data step processing

Overview of SAS Data Step

Ravi M., [email protected]

Compile Phase(Look at Syntax)

Execution Phase(Read data, Calculate)

Output Phase(Create Data Set)

Page 4: Understanding sas data step processing

Compile PhaseDATA NEW;

INPUT ID $ AGE TEMPC;

TEMPF=TEMPC*(9/5)+32;

DATALINES;

0001 24 37.3

0002 35 38.2

;

run;

proc print;run;

Ravi M., [email protected]

SAS Checks the syntax of the program.• Identifies type and

length of each variable• Does any variable need

conversion?

If everything is okay, proceed to the next step.

If errors are discovered, SAS attempts to interpret what you mean. If SAS can’t correct the error, it prints an error message to the log.

Page 5: Understanding sas data step processing

Create Input Buffer• SAS creates an input buffer

• INPUT BUFFER contains data as it is read inDATALINES;

0001 24 37.3

0002 35 38.2

;

Ravi M., [email protected]

1 2 3 4 5 6 7 8 9 10 11 12

0 0 0 1 2 4 3 7 . 3

INPUT BUFFER

Page 6: Understanding sas data step processing

Execution Phase

• PROGRAM DATA VECTOR (PDV) is created and contains information about the variables

• Two automatic variables _N_ and _ERROR_ and a position for each of the four variables in the DATA step.

• Sets _N_ = 1 _ERROR_ = 0 (no initial error) and remaining variables to missing.

Ravi M., [email protected]

_N_ _ERROR_ ID AGE TEMPC TEMPF

1 0 . . .

Page 7: Understanding sas data step processing

Buffer to PDV

Ravi M., [email protected]

1 2 3 4 5 6 7 8 9 10 11 12

0 0 0 1 2 4 3 7 . 3

_N_ _ERROR_ ID AGE TEMPC TEMPF

1 0 0001 24 37.3 .

Calculated value

Buffer

PDV

_N_ _ERROR_ ID AGE TEMPC TEMPF

1 0 0001 24 37.3 99.14

Processes the code TEMPF=TEMPC*(9/5)+32; Initially missing

Reads 1st record

If there is an executable statement…

Page 8: Understanding sas data step processing

Output Phase• The values in the PDV are written to the

output data set (NEW) as the first observation:

Ravi M., [email protected]

_N_ _ERROR_ ID AGE TEMPC TEMPF

1 0 0001 24 37.3 99.14

ID AGE TEMPC TEMPF

0001 24 37.3 99.14

This is the first record in the output data set

named “NEW.” Note that _N_ and

_ERROR_ are dropped.

From PDV

Write data to data set.

Page 9: Understanding sas data step processing

Exceptions to Missing in PDV

• Some data values are not initially set to missing in the PDV • variables in a RETAIN statement

• variables created in a SUM statement

• data elements in a _TEMPORARY_ array

• variables created with options in the FILE or INFILE statements

• These exceptions are covered later.

Ravi M., [email protected]

_N_ _ERROR_ ID AGE TEMPC TEMPF

1 0 . . .

Initial values usually set to missing in PDV

Page 10: Understanding sas data step processing

Next data record read

• Once SAS finished reading the first data record, it continues the same process, and reads the second record…sending results to output data set (named NEW in this case.)

• …and so on for all records.

Ravi M., [email protected]

ID AGE TEMPC TEMPF

0001 24 37.3 99.14

0002 35 38.2 100.76

Page 11: Understanding sas data step processing

Descriptor Information

• For the data set, SAS creates and maintains a description about each SAS data set:• data set attributes

• variable attributes

• the name of the data set

• member type, the date and time that the data set was created, and the number, names and data types (character or numeric) of the variables.

Ravi M., [email protected]

Page 12: Understanding sas data step processing

Data Set Description

proc datasets ;

contents data=new;

run;

Contents output… (abbreviated)

Ravi M., [email protected]

# Name Member

Type

File Size Last

Modified

1 NEW DATA 5120 20Nov13:0

8:59:32

Alternate program

proc contents data= new; run;

Page 13: Understanding sas data step processing

Description output continued…Data Set Name WORK.NEW Observations 2

Member Type DATA Variables 4

Engine V9 Indexes 0

Created Wed, Nov 20, 2013

08:59:32 AM

Observation Length 32

Last Modified Wed, Nov 20, 2013

08:59:32 AM

Deleted

Observations

0

Protection Compressed NO

Data Set Type Sorted NO

Label

Data Representation WINDOWS_64

Encoding wlatin1 Western

(Windows)

Ravi M., [email protected]

Page 14: Understanding sas data step processing

Description output continued…

Alphabetic List of Variables and Attributes

# Variable Type Len

2 AGE Num 8

1 ID Char 8

3 TEMPC Num 8

4 TEMPF Num 8

Ravi M., [email protected]

Page 15: Understanding sas data step processing

Original Program

DATA NEW;

INPUT ID $ AGE TEMPC;

TEMPF=TEMPC*(9/5)+32;

DATALINES;

0001 24 37.3

0002 35 38.2

;

run;

proc print;run;

Ravi M., [email protected]

Page 16: Understanding sas data step processing

Original Program

DATA NEW;

INPUT ID $ AGE TEMPC;

TEMPF=TEMPC*(9/5)+32;

DATALINES;

0001 24 37.3

0002 35 38.2

;

run;

proc print;run;

Ravi M., [email protected]

Obs ID AGE TEMP

C

TEMP

F

1 0001 24 37.3 99.14

2 0002 35 38.2 100.76

Program output

Page 17: Understanding sas data step processing

Example of Error

DATA NEW;

INPUT ID $ AGE TEMPC;

TEMPF=TEMPC*(9/5)+32

DATALINES;

0001 24 37.3

0002 35 38.2

;

run;

proc print;run;

proc datasets ;

contents data=new;

run;

Ravi M., [email protected]

Missing Semi-colon

Page 18: Understanding sas data step processing

76 DATA NEW;

77 INPUT ID $ AGE TEMPC;

78 TEMPF=TEMPC*(9/5)+32

79 DATALINES;

---------

22

80 0001 24 37.3

----

180

ERROR 22-322: Syntax error, expecting one of the following: !, !!, &, *, **, +, -, /, <, <=, <>, =, >, ><, >=, AND, EQ, GE,

GT, IN, LE, LT, MAX, MIN, NE, NG, NL, NOTIN, OR, ^=, |, ||, ~=.

ERROR 180-322: Statement is not valid or it is used out of proper order.

81 0002 35 38.2

82 ;

83 run;

ERROR: No DATALINES or INFILE statement.Ravi M., [email protected]

Error found during compilation

Page 19: Understanding sas data step processing

Summary - Compilation Phase

• During Compilation• Check syntax

• Identify type and length of each new variable (is a data type conversion needed?)

• creates input buffer if there is an INPUT statement for an external file

• creates the Program Data Vector (PDV)

• creates descriptor information for data sets and variable attributes

• Other options not discussed here: DROP; KEEP; RENAME; RETAIN; WHERE; LABEL; LENGTH; FORMAT; ARRAY; BY; ATTRIB; END=, IN=, FIRST, LAST, POINT=

Ravi M., [email protected]

Page 20: Understanding sas data step processing

Summary – Execution Phase1. The DATA step iterates once for each observation being

created.

2. Each time the DATA statement executes, _N_ is incremented by 1.

3. Newly created variables set to missing in the PDV.

4. SAS reads a data record from a raw data file into the input buffer (there are other possibilities not discussed here).

5. SAS executes any other programming statements for the current record.

6. At the end of the data statements (RUN;) SAS writes an observation to the SAS data set (OUTPUT PHASE)

7. SAS returns to the top of the DATA step (Step 3 above)

8. The DATA step terminates when there is no more data.

Ravi M., [email protected]

Page 21: Understanding sas data step processing

End

Ravi M., [email protected]