Presentation and Data  Short Courses  Intro to SAS  Download Data to Desktop 1.

Slides:



Advertisements
Similar presentations
CC SQL Utilities.
Advertisements

The SAS ® System Additional Information on Statistical Analysis Programming.
Statistical Methods Lynne Stokes Department of Statistical Science Lecture 7: Introduction to SAS Programming Language.
Objectives Understand the software development lifecycle Perform calculations Use decision structures Perform data validation Use logical operators Use.
Introduction to SAS Programming Christina L. Ughrin Statistical Software Consulting Some notes pulled from SAS Programming I: Essentials Training.
Presentation and Data  Short Courses  Intro to SAS  Download Data to Desktop 1.
Where to find this presentation and data  Short Courses  “Data Analysis in SAS”  Course Materials  Download Data to Desktop.
I OWA S TATE U NIVERSITY Department of Animal Science Modifying and Combing SAS Data Sets (Chapter in the 6 Little SAS Book) Animal Science 500 Lecture.
Concepts of Database Management Sixth Edition
Exploring Microsoft Excel 2002 Chapter 7 Chapter 7 List and Data Management: Converting Data to Information By Robert T. Grauer Maryann Barber Exploring.
Introduction to Structured Query Language (SQL)
Chapter 7 Data Management. Agenda Database concept Import data Input and edit data Sort data Function Filter data Create range name Calculate subtotal.
November 4, 2009 Introduction to SAS LISA Short Course Series Mark Seiss, Dept. of Statistics.
Understanding SAS Data Step Processing Alan C. Elliott stattutorials.com.
Into to SAS ®. 2 List the components of a SAS program. Open an existing SAS program and run it. Objectives.
Creating SAS® Data Sets
Welcome to SAS…Session..!. What is SAS..! A Complete programming language with report formatting with statistical and mathematical capabilities.
Introduction to Access By Mary Ann Chaney and Alicia Harkleroad.
ASP.NET Programming with C# and SQL Server First Edition
SAS Workshop Lecture 1 Lecturer: Annie N. Simpson, MSc.
Chapter 10: Working with Large Data Spreadsheet-Based Decision Support Systems Prof. Name Position (123) University Name.
Introduction to SAS BIO 226 – Spring Outline Windows and common rules Getting the data –The PRINT and CONTENT Procedures Manipulating the data.
Chapter 6 Generating Form Letters, Mailing Labels, and a Directory
Presentation and Data  Short Courses  Intro to SAS  Download Data to Desktop 1.
Analyzing Data For Effective Decision Making Chapter 3.
Introduction to SAS. What is SAS? SAS originally stood for “Statistical Analysis System”. SAS is a computer software system that provides all the tools.
BMTRY 789 Introduction to SAS Programming Lecturer: Annie N. Simpson, MSc.
Chapter 1: Introduction to SAS  SAS programs: A sequence of statements in a particular order  Rules for SAS statements: –Every SAS statement ends in.
I OWA S TATE U NIVERSITY Department of Animal Science Getting Your Data Into SAS (Chapter 2 in the Little SAS Book) Animal Science 500 Lecture No. 3 September.
Chapter 17 Creating a Database.
Lesson 2 Topic - Reading in data Chapter 2 (Little SAS Book)
SQL Chapter Two. Overview Basic Structure Verifying Statements Specifying Columns Specifying Rows.
ISU Basic SAS commands Laboratory No. 1 Computer Techniques for Biological Research Animal Science 500 Ken Stalder, Professor Department of Animal Science.
Introduction to Enterprise Guide Jennifer Schmidt Rhonda Ellis Cassandra Hall.
1 EPIB 698E Lecture 1 Notes Instructor: Raul Cruz 7/9/13.
How to start using SAS Tina Tian. The topics An overview of the SAS system Reading raw data/ create SAS data set Combining SAS data sets & Match merging.
Chapter 5 Reading and Manipulating SAS ® Data Sets and Creating Detailed Reports Xiaogang Su Department of Statistics University of Central Florida.
DAY 21: MICROSOFT ACCESS – CHAPTER 5 MICROSOFT ACCESS – CHAPTER 6 MICROSOFT ACCESS – CHAPTER 7 Aliya Farheen October 29,2015.
 2008 Pearson Education, Inc. All rights reserved JavaScript: Introduction to Scripting.
Chapter 1: Overview of SAS System Basic Concepts of SAS System.
Summer SAS Workshop Lecture 3. Summer SAS Workshop Website
How to start using SAS SARBAJIT MUKHERJEE. WHAT IS SAS? SAS stands for Statistical Analysis System. Useful for the following types of task: 1. Data entry,
SAS for Data Management and Analysis
An Introduction Katherine Nicholas & Liqiong Fan.
14b. Accessing Data Files in SAS ®. 1 Prerequisites Recommended modules to complete before viewing this module  1. Introduction to the NLTS2 Training.
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.
1 PEER Session 02/04/15. 2  Multiple good data management software options exist – quantitative (e.g., SPSS), qualitative (e.g, atlas.ti), mixed (e.g.,
1 Introduction to SAS Available at
 2006 Pearson Education, Inc. All rights reserved Control Statements: Part 2.
Lesson 2 Topic - Reading in data Programs 1 and 2 in course notes –Chapter 2 (Little SAS Book)
Chapter 6: Modifying and Combining Data Sets  The SET statement is a powerful statement in the DATA step DATA newdatasetname; SET olddatasetname;.. run;
1 EPIB 698C Lecture 1 Instructor: Raul Cruz-Cano
SAS Programming Training Instructor:Greg Grandits TA: Textbooks:The Little SAS Book, 5th Edition Applied Statistics and the SAS Programming Language, 5.
Based on Learning SAS by Example: A Programmer’s Guide Chapters 1 & 2
The Urban Institute - SAS Training6/9/20161 SAS Training This SAS Training Course was designed to introduce users at The Urban Institute to SAS programming.
FILES AND EXCEPTIONS Topics Introduction to File Input and Output Using Loops to Process Files Processing Records Exceptions.
SAS ® 101 Based on Learning SAS by Example: A Programmer’s Guide Chapters 3 & 4 By Tasha Chapman, Oregon Health Authority.
Chapter 6: Modifying and Combining Data Sets
Introduction to Scripting
SAS Programming I Matthew A. Lanham Doctoral Student
ECONOMETRICS ii – spring 2018
Chapter 1: Introduction to SAS
Instructor: Raul Cruz-Cano
Tamara Arenovich Tony Panzarella
Topics Introduction to File Input and Output
Introduction to DATA Step Programming SAS Basics II
Introduction to DATA Step Programming: SAS Basics II
Instructor: Raul Cruz 9/4/13
Topics Introduction to File Input and Output
Introduction to C Programming
Presentation transcript:

Presentation and Data  Short Courses  Intro to SAS  Download Data to Desktop 1

Mark Seiss, Dept. of Statistics Introduction to SAS Part 1 February 21, 2011

Reference Material The Little SAS Book – Delwiche and Slaughter SAS Programming I: Essentials SAS Programming II: Manipulating Data with the DATA Step Presentation and Data

Presentation Outline Part 1 1. Introduction to the SAS Environment 2. Working With SAS Data Sets Part 2 1. Summary Procedures 2. Basic Statistical Analysis Procedures

Presentation Outline Questions/Comments Individual Goals/Interests

Introduction to the SAS Environment 1.SAS Programs 2.SAS Data Sets and Data Libraries 3.SAS System Help 4.Creating SAS Data Sets

SAS Programs File extension -.sas Editor window has four uses: Access and edit existing SAS programs Write new SAS programs Submitting SAS programs for execution Saving SAS programs SAS program – sequence of steps that the user submits for execution Submitting SAS programs Entire program Selection of the program

SAS Programs Syntax Rules for SAS statements Free-format – can use upper or lower case Usually begin with an identifying keyword Can span multiple lines Always end with a semicolon Multiple statements can be on the same line Errors Misspelled key words Missing or invalid punctuation (missing semi-colon common) Invalid options Indicated in the Log window

SAS Programs 2 Basic steps in SAS programs: Data Steps Typically used to create SAS datasets and manipulate data, Begins with DATA statement Proc Steps Typically used to process SAS data sets Begins with PROC statement The end of the data or proc steps are indicated by: RUN statement – most steps QUIT statement – some steps Beginning of another step (DATA or PROC statement)

SAS Programs Output generated from SAS program – 2 Windows SAS log Information about the processing of the SAS program Includes any warnings or error messages Accumulated in the order the data and procedure steps are submitted SAS output Reports generated by the SAS procedures Accumulates output in the order it is generated

SAS Data Sets and Data Libraries SAS Data Set Specifically structured file that contains data values. File extension -.sas7bdat Rows and Columns format – similar to Excel Columns – variables in the table corresponding to fields of data Rows – single record or observation Two types of variables Character – contain any value (letters, numbers, symbols, etc.) Numeric – floating point numbers Located in SAS Data Libraries

SAS Data Sets and Data Libraries SAS Data Libraries Contain SAS data sets Identified by assigning a library reference name – libref Temporary Work library SAS data files are deleted when session ends Library reference name not necessary Permanent SAS data sets are saved after session ends SASUSER library You can create and access your own libraries

SAS Data Sets and Data Libraries SAS Data Libraries cont. Assigning library references Syntax LIBNAME libref ‘SAS-data-library’; Rules for Library References 8 characters or less Must begin with letter or underscore Other characters are letters, numbers, or under scores

SAS Data Sets and Data Libraries SAS Data Libraries cont. Identifying SAS data sets within SAS Data Libraries libref.filename Accessing SAS data sets within SAS Data Libraries Example:DATA new_data_set; set libref.filename; run; Creating SAS data sets within SAS Data Libraries Example:DATA libref.filename; set old_data_set; run;

SAS System Help SAS Help and Documentation Help  SAS Help and Documentation Red Book Icon SAS Online Help

Creating SAS Data Sets Creating a SAS data sets from raw data 4 methods 1.Importing existing data sets using Import menu option 2.Importing existing raw data in SAS program 3.Manually entering raw data in SAS program 4.Manually entering raw data using Table Editor

Creating SAS Data Sets Using the import data menu option 1.File  Import Data 2.Standard data source  select the file format 3.Specify file location or Browse to select file 4.Create name for the new SAS data set and specify location

Creating SAS Data Sets Compatible file formats Microsoft Excel Spreadsheets Microsoft Access Databases Comma Separate Files (.csv) Tab Delimited Files (.txt) dBASE Files (.dbf) JMP data sets SPSS Files Lotus Spreadsheets Stata Files Paradox Files

Creating SAS Data Sets Example Data Sets Excel File – State_SAT_data.xls Extracted from 1997 Digest of Education Statistics, an annual publication of the U.S. Department of Education Contains variables that show the relationship between public school expenditure and SAT performance Variables: –State (state) –Current expenditure per pupil (expend) –Average pupil to teacher ratio (PT_ratio) –Estimated annual salary of teachers (salary) –Percentage of eligible students taking the SAT (students) –Average verbal SAT score (verbal) –Average math SAT Score (math) –Average total score (total)

Creating SAS Data Sets Example Data Sets Cont. Text file – State_region_data.txt Contains region assignments for each state 1 = New England 2 = Middle Atlantic 3 = East North Central 4 = West North Central 5 = South Atlantic 6 = East South Central 7 = West South Central 8 = Mountain 9 = Pacific

Creating SAS Data Sets Import State_SAT_data.xls  Assign as work.state_sat_data.sas7bdat Import State_region_data.txt  Assign as work.state_region_data.sas7bdat

Introduction to the SAS Environment Questions/Comments

Working With SAS Data Sets 1.Data Set Information 2.Data Set Manipulation 3.Data Set Processing 4.Combining Data Sets A.Concatenating/Appending B.Merging 5.Saving Data Sets

Data Set Information Proc Contents Output contains a table of contents of the specified data set Data Set Information Data set name Number of observations Number of Variables Variable Information Type (numeric or character) Length Syntax: PROC CONTENTS DATA=input_data_set; RUN;

Data Set Information Assignment Obtain Data Set Information for work.state_sat_data and work.state_region_data

Data Set Information Solution proc contents data=state_sat_data; run; proc contents data=state_region_data; run;

Data Set Manipulation Create a new SAS data set using an existing SAS data set as input Specify name of the new SAS data set after the DATA statement Use SET statement to identify SAS data set being read Syntax: DATA output_data_set; SET input_data_set; ; RUN; By default the SET statement reads all observations and variables from the input data set into the output data set.

Data Set Manipulation Assignment Statements Evaluate an expression Assign resulting value to a variable General Form:variable = expression; Example:miles_per_hour = distance/time; SAS Functions Perform arithmetic functions, compute simple statistics, manipulate dates, etc. General Form:variable=function_name(argument1, argument2,…); Example: Time_worked = sum(Day1,Day2, Day3, Day4, Day5);

Data Set Manipulation Selecting Variables Use DROP and KEEP to determine which variables are written to new SAS data set. 2 Ways DROP and KEEP as statements –Form:DROP Variable1 Variable2; KEEP Variable3 Variable4 Variable5; DROP and KEEP options in SET statement –Form:SET input_data_set (KEEP=Var1);

Data Set Manipulation Conditional Processing Uses IF-THEN-ELSE logic General Form:IF THEN ; ELSE IF THEN ; ELSE ; is a true/false statement, such as: Day1=Day2, Day1 > Day2, Day1 < Day2 Day1+Day2=10 Sum(day1,day2)=10 Day1=5 and Day2=5

Data Set Manipulation Conditional Processing SymbolicMnemonicExample =EQIF region=‘Spain’; ~= or ^=NEIF region ne ‘Spain’; >GTIF rainfall > 20; <LTIF rainfall lt 20; >=GEIF rainfall ge 20; <=LEIF rainfall <= 20; &ANDIF rainfall ge 20 & temp < 90; | or !ORIF rainfall ge 20 OR temp < 90; IS NOT MISSING IF region IS NOT MISSING; BETWEEN ANDIF region BETWEEN ‘Plain’ AND ‘Spain’; CONTAINSIF region CONTAINS ‘ain’; INIF region IN (‘Rain’, ‘Spain’, ‘Plain’);

Data Set Manipulation Conditional Processing cont. If is true, is processed ELSE IF and ELSE are only processed if is false Only one statement specified using this form Use DO and END statements to execute group of statements General Form:IF THEN DO; ; END; ELSE DO; ; END;

Data Set Manipulation Subsetting Rows (Observations) We will look at two ways Using IF statement Using WHERE option in SET statement IF statement Only writes observations to the new data set in which an expression is true; General Form: IF ; Example: IF career = ‘Teacher’; IF sex ne ‘M’; In the second example, only observations where sex is not equal to ‘M’ will be written to the output data set

Data Set Manipulation Subsetting Rows (Observations) cont. Where Option in SET statement Use option to only read rows from the input data set in which the expression is true General Form:SET input_data_set (where=( )); Example:SET vacation (where=(destination=‘Bermuda’)); Only observations where the destination equals ‘Bermuda’ will be read from the input data set Comparison Resulting output data set is equivalent IF statement – all rows read from the input data set Where option – only rows where expression is true are read from input data set Difference in processing time when working with big data sets

Data Set Manipulation Assignments 1.Create new dataset work.state_SAT_data2 from work.state_SAT_data Assign new variable  upper_ind If total > 1000 then upper_ind=1 Otherwise upper_ind=0 2.Create new dataset work.south from work.state_region_data Specify work.south contains only records from regions 5, 6, or 7 Specify work.south only contains the state variable

Data Set Manipulation Solutions 1. data state_sat_data2; set state_sat_data; if total>1000 then upper_ind=1; else upper_ind=0; run;

Data Set Manipulation Solutions 2. data south; set state_region_data; if region=5 or region=6 or region=7; keep state; run; OR data south; set state_region_data(where=(region=5 or region=6 or region=7)); keep state; run;

Data Set Manipulation PROC SORT sorts data according to specified variables General Form:PROC SORT DATA=input_data_set ; BY Variable1 Variable2; RUN; Sorts data according to Variable1 and then Variable2; By default, SAS sorts data in ascending order Number low to high A to Z Use DESCENDING statement for numbers high to low and letters Z to A BY City DESCENDING Population; SAS sorts data first by city A to Z and then Population high to low

Data Set Manipulation Some Options NODUPKEY Eliminates observations that have the same values for the BY variables OUT=output_data_set By default, PROC SORT replaces the input data set with the sorted data set Using this option, PROC SORT creates a newly sorted data set and the input data set remains unchanged

Data Set Processing DATA steps read in data from existing data sets or raw data files one row at a time, like a loop DATA step reads data from the input data set in the following way: 1. Read in current row from input data set to Program Data Vector (PDV) 2.Process SAS statements 3.PDV to output data set 4.Set current row to the next row in the input data set 5.Iterate to Step 1 One row at a time is processed Thus we cannot simply add the value of a variable in one row to the value in another row

Data Set Processing Data Set Processing – Example Consider the following submitted code: data state_sat_data2; set state_sat_data; if total>1000 then upper_ind=1; else upper_ind=0; run;

Data Set Processing Data Set Processing – Example Execution of the Data Step data state_sat_data2; Current  set state_sat_data; if total>1000 then upper_ind=1; else upper_ind=0; run; PDV State_sat_data2 StateExpendPT_ratioSalaryStudentsVerbalMathTotalUpper_ind Alabama StateExpendPT_ratioSalaryStudentsVerbalMathTotalUpper_ind

Data Set Processing Data Set Processing – Example Execution of the Data Step data state_sat_data2; set state_sat_data; Current  if total>1000 then upper_ind=1; else upper_ind=0; run; PDV State_sat_data2 StateExpendPT_ratioSalaryStudentsVerbalMathTotalUpper_ind Alabama StateExpendPT_ratioSalaryStudentsVerbalMathTotalUpper_ind

Data Set Processing Data Set Processing – Example Execution of the Data Step data state_sat_data2; set state_sat_data; if total>1000 then upper_ind=1; else upper_ind=0; Current  run; PDV State_sat_data2 StateExpendPT_ratioSalaryStudentsVerbalMathTotalUpper_ind Alabama StateExpendPT_ratioSalaryStudentsVerbalMathTotalUpper_ind Alabama

Data Set Processing Data Set Processing – Example Execution of the Data Step Current  data state_sat_data2; set state_sat_data; if total>1000 then upper_ind=1; else upper_ind=0; run; PDV State_sat_data2 StateExpendPT_ratioSalaryStudentsVerbalMathTotalUpper_ind Alabama StateExpendPT_ratioSalaryStudentsVerbalMathTotalUpper_ind Alabama

Data Set Processing Data Set Processing – Example Execution of the Data Step data state_sat_data2; Current  set state_sat_data; if total>1000 then upper_ind=1; else upper_ind=0; run; PDV State_sat_data2 StateExpendPT_ratioSalaryStudentsVerbalMathTotalUpper_ind Alaska StateExpendPT_ratioSalaryStudentsVerbalMathTotalUpper_ind Alabama

Data Set Processing Data Set Processing – Example Execution of the Data Step data state_sat_data2; set state_sat_data; if total>1000 then upper_ind=1; Current  else upper_ind=0; run; PDV State_sat_data2 StateExpendPT_ratioSalaryStudentsVerbalMathTotalUpper_ind Alaska StateExpendPT_ratioSalaryStudentsVerbalMathTotalUpper_ind Alabama

Data Set Processing Data Set Processing – Example Execution of the Data Step data state_sat_data2; set state_sat_data; if total>1000 then upper_ind=1; else upper_ind=0; Current  run; PDV State_sat_data2 StateExpendPT_ratioSalaryStudentsVerbalMathTotalUpper_ind Alaska StateExpendPT_ratioSalaryStudentsVerbalMathTotalUpper_ind Alabama Alaska

Combining Data Sets Concatenating (or Appending) Stacks each data set upon the other If one data set does not have a variable that the other datasets do, the variable in the new data set is set to missing for the observations from that data set. General Form:DATA output_data_set; SET data1 data2; run; PROC APPEND may also be used

Combining Data Sets Merging Data Sets One-to-One Match Merge A single record in a data set corresponds to a single record in all other data sets Example: Patient and Billing Information One-to-Many Match Merge Matching one observation from one data set to multiple observations in other data sets Example: County and State Information Note:Data must be sorted before merging can be done (PROC SORT)

Combining Data Sets One-to-One Match Merge Usually need at least one common variable between data sets – matching purposes For the example, a patient ID would be needed Do not need common variable if all data sets are in exactly the same order General Form:DATA output_data_set; MERGE input_data_set1 input_data_set2; By variable1 variable2; RUN;

Combining Data Sets One-to-One Match Merge Example: PerformanceGoals Code: DATA compare; MERGE performance goals; BY month; difference=sales-goal; RUN; MonthSales MonthGoal

Combining Data Sets One-to-One Match Merge Example cont.: Compare MonthSalesGoalDifference

Combining Data Sets One-to-Many Match Merge Requires at least one common variable in the data sets for matching purposes For the example, State information is in both the state and county files If two data sets have variables with the same name, the variables in the second data set will overwrite the variable in the first. General Form:DATA output_data_set; MERGE Data1 Data2 Data3; BY Variable1 Variable2; RUN:

Combining Data Sets One-to-Many Match Merge Example: VideosAdjustment Code: DATA prices; MERGE videos adjustment BY category; NewPrice=(1-adjustment)*sales; RUN; CategorySales Aerobics12.99 Aerobics13.99 Aerobics13.99 Step12.99 Step12.99 Weights15.99 CategoryAdjustment Aerobics.20 Step.30 Weights.25

Combining Data Sets One-to-One Many Merge Example cont.: Videos CategorySalesAdjustmentNewPrice Aerobics Aerobics Aerobics Step Step Weights

Combining Data Sets Assignment Create the dataset work.state_data Merge work.state_sat_data2 with work.state_region_data by the state variable

Combining Data Sets Solution proc sort data=state_sat_data2; by state; run; proc sort data=state_region_data; by state; run; data state_data; merge state_sat_data2 state_region_data; by state; run;

Saving Data Sets Save as SAS dataset (.sas7bdat) LIBNAME libref “destination folder”; DATA libref.filename; SET current_name; optional commands; RUN; Other Formats 1.File  Export Data 2.Specify SAS data set 3.Standard data source  select the file format 4.Specify File Folder and Filename

Working With SAS Data Sets Questions/Comments

Attendee Questions If time permits