Presentation is loading. Please wait.

Presentation is loading. Please wait.

Computing with SAS Software A SAS program consists of SAS statements. 1. The DATA step consists of SAS statements that define your data and create a SAS.

Similar presentations


Presentation on theme: "Computing with SAS Software A SAS program consists of SAS statements. 1. The DATA step consists of SAS statements that define your data and create a SAS."— Presentation transcript:

1 Computing with SAS Software A SAS program consists of SAS statements. 1. The DATA step consists of SAS statements that define your data and create a SAS data set. 2. The PROC steps are groups of SAS statements that indicate what kind of statistical analysis to perform The following slides present the DATA STEP commands to read the data into SAS.

2 The SAS Data Step RAW DATA DATA STEP SAS DATA SET Raw Data: Rows are observations and columns are variables This is created by entering data, reading raw data, or accessing files created by other software according to a specific syntax. AgeGenderExam gradeHomework grade 19F 9094 20M8990 20F7886 19M9590 21M8385

3 In SAS nomenclature: – Columns specify Variables – Rows contain the Observations. The general form of the data step is DATA name; Statements; DATALINES ; NOTICE: All SAS statements end with a semicolon ";". Remember the most common cause of error in SAS is the omission of the semicolon. ALWAYS CHECK FOR IT!! Name is the SAS data set name

4 /* This is an example on how to input data into SAS */ TITLE 'Example of data input in SAS'; DATA grades; INPUT age gender $ exam hwork; DATALINES; 19 F 90 94 20 M 89 90 20 F 78 86 19 M 95 90 21 M 83 85 ; Required: Semi-colon on a line by itself --- The null statement SAS data set name statement

5 Rules for SAS names Total number of characters: 8 or less (Prior to SAS version 8). Must start with a letter or an underscore ( _ ). Must not contain blanks Other rules For a missing value of an observation: – enter a period (.) for missing numeric – enter a blank ( ) for missing name (character variable).

6 The input statement It defines the variable name, type and location. 1. If data are in columns, use 2.To read several observations separated by at least one blank space: use the symbol @@ at the end of the input line. Example: DATA grades; INPUT age gender $ exam hwork @@; DATALINES; 19 F 90 94 20 M 89 90 20 F 78 86 19 M 95 90 21 M 83 85 ; INPUT age 1-2 gender $ 4 exam 6-7 hwork 9-10; Col location

7 The datalines statement Immediately follows the input statement: DATA grades; INPUT age gender $ exam hwork @@; DATALINES; 19 F 90 94 20 M 89 90 20 F 78 86 19 M 95 90 21 M 83 85 ;

8 However, if the data are to be read from an external file (ex: mydata.txt), use the infile statement as follows: If data are in columns: DATA grades; INFILE ‘c:\tmp\grades.dat’; INPUT age 1-2 gender $ 4 exam 6-7 hwork 9-10; If data are NOT in columns: DATA grades; INFILE ‘c:\tmp\grades.dat’; INPUT age gender $ exam hwork @@;

9 steps SAS Proc steps PROC DATA=dataset1 [options1]; [Statements / options2]; proc_name  name of the PROC being used, DATA=dataset1  name of the SAS data set to be analyzed (If omitted, the most recently created SAS data set is used.) Options and statements vary from PROC to PROC: Descriptions in your SAS manual and the Help Window!!

10 Is a procedure to display your data on screen (or into a file) as part of the output. PROC PRINT DATA=grades; TITLE2 'Listing of data'; TITLE3 'Exam and Homework grades'; RUN; PROC Print Example of data input in SAS Listing of data Exam and Homework grades Obs AGE GENDER EXAM HWORK 1 19 F 90 94 2 20 M 89 90 3 20 F 78 86 4 19 M 95 90 5 21 M 83 85

11 If we had more variables and we wanted to print only the variable age and gender, specify it in proc print by using the keyword var: proc print data = grades ; var age gender ; run;

12 Labeling variables Labeling variables: to describe the variables, you can attach labels to variable names. DATA Grades; INPUT AGE GENDER $ EXAM HWORK @@; LABEL AGE=’Student’s age’ GENDER=’Student’s sex’ EXAM = ‘Final exam grade’ HWORK=’Homework grade’; DATALINES;...

13  To format values of variables PROC FORMAT; VALUE $SX ‘M’=’Male’ ‘F’=’Female’; RUN; DATA GRADES; INPUT AGE GENDER $ EXAM HWORK; FORMAT GENDER $SX.; Notice the dot after $SX DATALINES;... PROC format REMEMBER: Labels: for names of variables Formats: for values of variables

14 Built-in formats in SAS Example of SAS Program for CSC323 Students' grades in Age order AGE GENDER EXAM HWORK 19 Female 90 94 19 Male 95 90 20 Male 89 90 20 Female 78 86 21 Male 83 85 PROC SORT DATA=GRADES; BY AGE; RUN; PROC PRINT DATA=GRADES; TITLE2 "Students' grades in Age order"; ID AGE; VAR GENDER EXAM HWORK; RUN;

15 Data manipulation Data Transformations: Assignment statements create new variables. The usual arithmetic operations and transformations are available. DATA GRADES; INPUT AGE GENDER $ EXAM HWORK; MEANGRADE=(EXAM+HWORK)/2; AGE_LOG=LOG(AGE); DATALINES;...

16 /* Example of Program: Data on students’ grades*/ OPTIONS NODATE; /* Suppress the date that is normally printed in the output*/ TITLE 'Example of SAS Program for CSC323'; PROC FORMAT; VALUE $SX 'M'='Male' 'F'='Female';

17 /* It inputs the data; computes the final score as the average of the exam score and the homework grade and it assigns a letter grade */ DATA GRADES; INPUT AGE GENDER $ EXAM HWORK; FORMAT GENDER $SX.; FINAL= (EXAM + HWORK)/2; IF FINAL < 75 THEN GRADE ='C'; ELSE IF FINAL >=75 AND FINAL <=85 THEN GRADE = 'B'; ELSE IF FINAL >=85 THEN GRADE = 'A'; LABEL AGE='Student's age' GENDER='Student's sex' EXAM = 'Final exam grade' HWORK='Homework grade'; DATALINES; 19 F 90 94 20 M 89 90 20 F 78 86 19 M 95 90 21 M 83 85 ;

18 /* It lists the student's grades in student's age order*/ PROC SORT DATA=GRADES; BY AGE; RUN; PROC PRINT DATA=GRADES; TITLE2 "Students' grades in Age order"; ID AGE; VAR GENDER EXAM HWORK FINAL GRADE; RUN;

19 Example of SAS Program for CSC323 Students' grades in Age order AGE GENDER EXAM HWORK FINAL GRADE 19 Female 90 94 92.0 A 19 Male 95 90 92.5 A 20 Male 89 90 89.5 A 20 Female 78 86 82.0 B 21 Male 83 85 84.0 B Program Output


Download ppt "Computing with SAS Software A SAS program consists of SAS statements. 1. The DATA step consists of SAS statements that define your data and create a SAS."

Similar presentations


Ads by Google