Presentation is loading. Please wait.

Presentation is loading. Please wait.

Introduction to SAS Essentials Mastering SAS for Data Analytics Alan Elliott and Wayne Woodward SAS Essentials - Elliott & Woodward1.

Similar presentations


Presentation on theme: "Introduction to SAS Essentials Mastering SAS for Data Analytics Alan Elliott and Wayne Woodward SAS Essentials - Elliott & Woodward1."— Presentation transcript:

1 Introduction to SAS Essentials Mastering SAS for Data Analytics Alan Elliott and Wayne Woodward SAS Essentials - Elliott & Woodward1

2 Intro to SAS Chapter 3 Part 2

3 3.9 GOING DEEPER: UNDERSTANDING HOW THE DATA STEP READS AND STORES DATA  To understand how SAS works, it can be helpful to "look under the hood" and see what is happening in all those bits and bytes as SAS reads and processes data. Knowing how SAS handles data is a little bit like knowing how the motor in your car works. You can usually drive around okay without knowing anything about pistons, but sometimes, it is good to know what that knocking sound means.

4 How SAS Thinks  UNDERSTANDING HOWTHE DATA STEP READS AND STORES DATA  Data Step Processing  The DATA Step vs The PROC Step  More about reading data files  Review of how to read data into SAS

5 Consider the following program  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; How does SAS read in this data and create a SAS data set? This code calculates a value and creates a variable named TEMPF. We’ll learn more about calculations later…

6 Overview of SAS Data Step Compile Phase (Look at Syntax) Execution Phase (Read data, Calculate) Output Phase (Create Data Set)

7 Concepts… COMPILE - SAS reads the syntax of the SAS program to see if there are any errors in the code. If there are no errors found, SAS “compiles” this code – that is, it transforms the SAS code into a code used internally by SAS. (You don’t need to know this internal code.) EXECUTION - If the code syntax checks out, SAS begins performing the tasks specified by the code. For example, the first line of code is DATA NEW, so during the execution phase, SAS creates a “blank” dataset (no data in it) named NEW that it will use to put the data into as it is read. OUTPUT -SAS reads in each data line. It interprets this line of data into the values for each variable and stores them into the data set one line at a time until all data have been output into the specified data set.

8 Compile Phase DATA NEW; INPUT ID $ AGE TEMPC; TEMPF=TEMPC*(9/5)+32; DATALINES; 0001 24 37.3 0002 35 38.2 ; run; proc print;run; 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.

9 Create Input Buffer  SAS creates an input buffer  INPUT BUFFER contains data as it is read in DATALINES; 0001 24 37.3 0002 35 38.2 ; 123456789101112 00012437.3 INPUT BUFFER

10 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. _N__ERROR_IDAGETEMPCTEMPF 10...

11 Buffer to PDV 123456789101112 00012437.3 _N__ERROR_IDAGETEMPCTEMPF 1000012437.3. Calculated value Buffer PDV _N__ERROR_IDAGETEMPCTEMPF 1000012437.399.14 Processes the code TEMPF=TEMPC*(9/5)+32; Initially missing Reads 1 st record If there is an executable statement…

12 Output Phase  The values in the PDV are written to the output data set (NEW) as the first observation: _N__ERROR_IDAGETEMPCTEMPF 1000012437.399.14 IDAGETEMPCTEMPF 00012437.399.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.

13 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. _N__ERROR _ IDAG E TEMP C TEMPF 10... Initial values usually set to missing in PDV

14 Know the Difference  INPUT BUFFER  PROGRAM DATA VECTOR

15 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. IDAGETEMPCTEMPF 00012437.399.14 00023538.2100.76

16 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.

17 Data Set Description proc datasets ; contents data=new; run; Contents output… (abbreviated) #NameMember Type File SizeLast Modified 1NEWDATA512020Nov13:0 8:59:32 Alternate program proc contents data= new; run;

18 Description output continued… Data Set NameWORK.NEWObservations2 Member TypeDATAVariables4 EngineV9Indexes0 CreatedWed, Nov 20, 2013 08:59:32 AM Observation Length32 Last ModifiedWed, Nov 20, 2013 08:59:32 AM Deleted Observations 0 ProtectionCompressedNO Data Set TypeSortedNO Label Data RepresentationWINDOWS_64 Encodingwlatin1 Western (Windows)

19 Description output continued… Alphabetic List of Variables and Attributes #VariableTypeLen 2AGENum8 1IDChar8 3TEMPCNum8 4TEMPFNum8

20 Example -- How Errors are Found During Compilation  REVIEW THIS 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;

21 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; ObsIDAGETEMP C TEMP F 100012437.399.14 200023538.2100.76 Program output

22 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; Missing Semi-colon

23 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. Error found during compilation

24 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=

25 Summary – Execution Phase 1. 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.

26 Quiz - Find Syntax Errors DATA MYDATA; INPUT ID $ SBP DBP GENDER $ AGE WT; DATALINES; 1 120 80 M 15 115 2 130 70 F 25 180 3 140 100 M 89 170 4 120 80 F 30 150 5 125 80 F 20 110; PROC PRINT; RUN; Where is the syntax error?

27 Find Syntax Errors DATA MYDATA; INFILE 'C:\SASDATA\EXAMPLE.DAT'; INPUT ID $ 1-3 GP $ 5 AGE 6-9 TIME1 10-14 TIME2 15-19 TIME3 20-24; DATALINES; PROC MEANS; RUN; Where is the syntax error?

28 Find Syntax Errors DATA MYDATA; INFILE 'C:\SASDATA\EXAMPLE.CSV'; DLM=', ' FIRSTOBS=2 OBS=26; INPUT GROUP $ AGE TIME2 TIME3 Time4 SOCIO; PROC MEANS; RUN ; Where is the syntax error?

29 Character Variable LENGTH in SAS  By default, character variables have a length of 8. DATA NAMES; INPUT FIRST $ LAST $ AGE; DATALINES; GEORGE WASHINGTON 30 JAMES ADAMS 34 BERNIE RUMPELSTILTSKIN 55 ; proc print; run;

30 Results ObsFIRSTLASTAGE 1GEORGEWASHINGT30 2JAMESADAMS34 3BERNIERUMPELST55 NOTE THE PROBLEM

31 Use LENGTH Statement data names; LENGTH LAST $15.; input FIRST $ LAST $ AGE; Etc… ObsLASTFIRSTAGE 1WASHINGTONGEORGE30 2ADAMSJAMES34 3RUMPELSTILTSKINBERNIE55 Problem corrected…

32 Missing Data in Freeform data names; LENGTH LAST $15. input FIRST $ LAST $ AGE; DATALINES; GEORGE WASHINGTON 30 JAMES ADAMS BERNIE RUMPELSTILTSKIN 55 ; proc print; run; Note: No AGE for JAMES Adams

33 Results are ObsLASTFIRSTAGE 1WASHINGTONGEORGE30 2ADAMSJAMES Did not read all of the data!

34 Indicate missing data data names; LENGTH LAST $15. input FIRST $ LAST $ AGE; DATALINES; GEORGE WASHINGTON 30 JAMES ADAMS. BERNIE RUMPELSTILTSKIN 55 ; proc print; run; Note: Note missing value denoted as dot (.)

35 Results ObsLASTFIRSTAGE 1WASHINGTONGEORGE30 2ADAMSJAMES. 3RUMPELSTILTSKINBERNIE55

36 Missing Data in Column DATA MYDATA; INPUT ID $ 1 SBP 2-4 DBP 5-7 GENDER $ 8 AGE 9-10 WT 11-13; DATALINES; 1120 M15115 2130 70F25180 3140100 89170 4120 80F30 5125 80F20110 ; RUN; PROC PRINT; RUN; 1.Read in DCOLUMN.SAS 2.Delete the 80 in the first record, the 150 in the 4 th record, and M in record 3 (preserve columns) 3.Change PROC MEANS to PROC PRINT 4.Run the program and observe output

37 Resulting Output ObsIDSBPDBPGENDERAGEWT 11120.M15115 2213070F25180 3314010089170 4412080F30. 5512580F20110 Note the blanks in the data set are read as missing values – the numeric missing values are indicated by dot (.) and text missing values are indicated with a blank. This works if data are read using column or formatted input.

38 Location of created Data sets  In the left window in SAS, click on the Explore tab. Notice the Contents of “SAS Environment”  Click on the Libraries icon.  You will see several “Libraries” including Work.  Click on Work

39 Work Library  The WORK library contains all of the SAS data sets we’ve created so far. (You may have a little different list)  Double Click on the one named Employees

40 SAS Viewtable  SAS Viewtable displays the contents of a data set.  Click on “x” to close  Note that the name is work.employees  Close viewer

41 3.10 SUMMARY  This chapter defined the difference between temporary and permanent data sets and illustrated  several methods for importing data sets into SAS using either the SAS Wizard or  PROC IMPORT. Finally, the way SAS "thinks" as it is inputting data is explained.

42 SAS ESSENTIALS -- Elliott & Woodward42 These slides are based on the book: Introduction to SAS Essentials Mastering SAS for Data Analytics, 2 nd Edition By Alan C, Elliott and Wayne A. Woodward Paperback: 512 pages Publisher: Wiley; 2 edition (August 3, 2015) Language: English ISBN-10: 111904216X ISBN-13: 978-1119042167 These slides are provided for you to use to teach SAS using this book. Feel free to modify them for your own needs. Please send comments about errors in the slides (or suggestions for improvements) to acelliott@smu.edu. Thanks.


Download ppt "Introduction to SAS Essentials Mastering SAS for Data Analytics Alan Elliott and Wayne Woodward SAS Essentials - Elliott & Woodward1."

Similar presentations


Ads by Google