Introduction to DATA Step Programming SAS Basics II

Slides:



Advertisements
Similar presentations
Statistical Methods Lynne Stokes Department of Statistical Science Lecture 7: Introduction to SAS Programming Language.
Advertisements

SAS Programming:File Merging and Manipulation. Reading External Files (review) data barf; * create the dataset BARF; infile ’s:\mysas\Table7.1'; * open.
Slide C.1 SAS MathematicalMarketing Appendix C: SAS Software Uses of SAS  CRM  datamining  data warehousing  linear programming  forecasting  econometrics.
Professional Seminar Northwestern Polytechnic University By Dr. Michael M Cheng.
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.
1 Creating and Tweaking Data HRP223 – 2010 October 24, 2011 Copyright © Leland Stanford Junior University. All rights reserved. Warning: This.
Biostatistical Methods II PubH 6415 Spring PubH 6415 – Biostatistics I Instructor: Susan Telke (office hours: lecture.
Week 6 - Programming I So far, we’ve looked at simple programming via “scripts” = programs of sequentially evaluated commands Today, extend features to:
Understanding SAS Data Step Processing Alan C. Elliott stattutorials.com.
Data Cleaning 101 Ron Cody, Ed.D Robert Wood Johnson Medical School Piscataway, NJ.
Welcome to SAS…Session..!. What is SAS..! A Complete programming language with report formatting with statistical and mathematical capabilities.
An Introduction to Textual Programming
SAS Workshop Lecture 1 Lecturer: Annie N. Simpson, MSc.
Introduction to SAS BIO 226 – Spring Outline Windows and common rules Getting the data –The PRINT and CONTENT Procedures Manipulating the data.
1 Experimental Statistics - week 4 Chapter 8: 1-factor ANOVA models Using SAS.
Introduction to SAS. What is SAS? SAS originally stood for “Statistical Analysis System”. SAS is a computer software system that provides all the tools.
Quantify the Example Data First, code and quantify the data (assign column locations & variable names) Use the sample data to create a data set from the.
SAS 介绍和举例 Presented by 经济实验教学中心 商务数据挖掘中心. Raw Data Read in Data Process Data (Create new variables) Output Data (Create SAS Dataset) Analyze Data Using.
BMTRY 789 Introduction to SAS Programming Lecturer: Annie N. Simpson, MSc.
SAS Efficiency Techniques and Methods By Kelley Weston Sr. Statistical Programmer Quintiles.
Vectors and Matrices In MATLAB a vector can be defined as row vector or as a column vector. A vector of length n can be visualized as matrix of size 1xn.
Lesson 2 Topic - Reading in data Chapter 2 (Little SAS Book)
Chapter 3 “Working With Your Data” concerns programming in the DATA step - putting lines of SAS code between a DATA and PROC statement… Creating new variables.
Lesson 6 - Topics Reading SAS datasets Subsetting SAS datasets Merging SAS datasets.
Chapter 22: Using Best Practices 1 STAT 541 ©Spring 2012 Imelda Go, John Grego, Jennifer Lasecki and the University of South Carolina.
Chapter 5 Reading and Manipulating SAS ® Data Sets and Creating Detailed Reports Xiaogang Su Department of Statistics University of Central Florida.
read and learn from example loop programs develop modular program
SAS Basics. Windows Program Editor Write/edit all your statements here. Log Watch this for any errors in program as it runs. Output Will automatically.
Introduction to SAS Essentials Mastering SAS for Data Analytics Alan Elliott and Wayne Woodward SAS Essentials - Elliott & Woodward1.
Chapter 1: Overview of SAS System Basic Concepts of SAS System.
Summer SAS Workshop Lecture 3. Summer SAS Workshop Website
Controlling Input and Output
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.
Chapter 2 Getting Data into SAS Directly enter data into SAS data sets –use the ViewTable window. You can define columns (variables) with the Column Attributes.
BMTRY 789 Lecture 6: Proc Sort, Random Number Generators, and Do Loops Readings – Chapters 5 & 6 Lab Problem - Brain Teaser Homework Due – HW 2 Homework.
Use the SET statement to: –create an exact copy of a SAS dataset –modify an existing SAS dataset by creating new variables, subsetting (using a subsetting.
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;
SAS Programming Training Instructor:Greg Grandits TA: Textbooks:The Little SAS Book, 5th Edition Applied Statistics and the SAS Programming Language, 5.
SAS ® 101 Based on Learning SAS by Example: A Programmer’s Guide Chapters 8, 13, & 24 By Tasha Chapman, Oregon Health Authority.
SAS ® 101 Based on Learning SAS by Example: A Programmer’s Guide Chapters 7 & 10 By Tasha Chapman, Oregon Health Authority.
Chapter 2 Writing Simple Programs
Input and Output: I/O Finish reading chapters 1 and 2 of the text
Chapter 6: Modifying and Combining Data Sets
Chapter 2: Getting Data into SAS
Chapter 3: Working With Your Data
LINDSEY BREWER CSSCR (CENTER FOR SOCIAL SCIENCE COMPUTATION AND RESEARCH) UNIVERSITY OF WASHINGTON September 17, 2009 Introduction to SPSS (Version 16)
What is Bash Shell Scripting?
Chapter 18: Modifying SAS Data Sets and Tracking Changes
Learning to Program in Python
Chapter 7: Macros in SAS Macros provide for more flexible programming in SAS Macros make SAS more “object-oriented”, like R Not a strong suit of text ©
SAS Essentials How SAS Thinks
Introduction to SAS A SAS program is a list of SAS statements executed in order Every SAS statement ends with a semicolon! SAS statements can be in caps.
Defining and Calling a Macro
Introduction to DATA Step Programming: SAS Basics II
How SAS Thinks: SAS Basics I
Combining Data Sets in the DATA step.
A look at Python Programming Language 2018.
Lab 2 and Merging Data (with SQL)
Lab 2 HRP223 – 2010 October 18, 2010 Copyright © Leland Stanford Junior University. All rights reserved. Warning: This presentation is protected.
Vectors and Matrices In MATLAB a vector can be defined as row vector or as a column vector. A vector of length n can be visualized as matrix of size 1xn.
Instructor: Raul Cruz-Cano 7/19/2012
Unit 3: Variables in Java
Matlab Basics.
Introduction to SAS Lecturer: Chu Bin Lin.
The Python interpreter
Hans Baumgartner Penn State University
Introduction to SAS Essentials Mastering SAS for Data Analytics
STAT 515 Statistical Methods I Lecture 1 August 22, 2019 Originally prepared by Brian Habing Department of Statistics University of South Carolina.
Presentation transcript:

Introduction to DATA Step Programming SAS Basics II Susan J. Slaughter, Avocet Solutions Let’s get this party started!

SAS Essentials Three presentations We’ll go fast There will be a test How SAS thinks DATA step PROC step We’ll go fast Slides are on my website There will be a test Do you have the handout? Copyright 2016 Susan Slaughter

DATA versus PROC steps Two basic parts of SAS programs DATA step PROC step Begin with DATA statement Begin with PROC statement Input and modify data Perform analysis or task Create SAS data set Produce report Flexibility of programming Like filling out a form Susan says: This is a simplification Copyright 2016 Susan Slaughter

DATA versus PROC steps A simple example DATA temps; Farenheit = 68; Celsius = (Farenheit – 32) * 0.5556; PROC PRINT DATA = temps; TITLE 'Temperature Conversions'; RUN; DATA step PROC step Copyright 2016 Susan Slaughter

DATA versus PROC steps Copyright 2016 Susan Slaughter

DATA step’s built-in loop DATA steps execute line-by-line and observation-by-observation DATA step output data set input data set observation 1 observation 2 observation 3 line 1 line 2 line 3 line 4 line 5 observation 1 observation 2 observation 3 Copyright 2016 Susan Slaughter

Reading data in a DATA step Reading raw data file INPUT statement Reading SAS data sets SET statement MERGE statement Copyright 2016 Susan Slaughter

Reading raw data INPUT statements read raw data External raw data files also called text, ASCII, sequential, flat file, CSV files Internal raw data also called instream data Three basic styles of INPUT statements List style Column style Formatted style Copyright 2016 Susan Slaughter

Reading raw data List style input Internal data * Read internal data; DATA students; INPUT ID $ Name $ Age Major $; DATALINES; 78374 Thomas 21 . 75638 Cathy . STA 78634 David 20 ENG ; * Print data set named students; PROC PRINT DATA = students; TITLE 'Students'; RUN; List style input Internal data Copyright 2016 Susan Slaughter

Reading raw data Be sure to check your SAS log! Copyright 2016 Susan Slaughter

Reading a single SAS data set SET statement General form: DATA new-data-set; SET old-data-set; Example: DATA class; SET class; If data set names are same, then new replaces old Copyright 2016 Susan Slaughter

Stacking SAS data sets SET statement Stacking = concatenating General form: DATA new-data-set; SET old-data-set-1 … old-data-set-n; Example: DATA new_stacked; SET class1 class2 class3; Order of observations in new data set depends on order in SET statement Copyright 2016 Susan Slaughter

Interleaving SAS data sets SET statement General form: DATA new-data-set; SET old-data-set-1 … old-data-set-n; BY variable-list; Example: DATA new_stacked; SET class1 class2 class3; BY ID; Data sets must be sorted by variables in BY statement Use PROC SORT Copyright 2016 Susan Slaughter

Merging SAS data sets 1-to-1 MERGE statement General form: DATA new-data-set; MERGE old-data-set-1 old-data-set-2; BY variable-list; Example: DATA new_merged; MERGE class1 demog; BY ID; Data sets must be sorted by values of BY variable If needed, use PROC SORT Copyright 2016 Susan Slaughter

Merging SAS data sets 1-to-many MERGE statement General form: DATA new-data-set; MERGE old-data-set-1 old-data-set-2; BY variable-list; Example: DATA new_merged; MERGE class1 demog; BY ID; Data sets must be sorted by values of BY variable If needed, use PROC SORT Copyright 2016 Susan Slaughter

Data for examples * Input student enrollment data; DATA SASUSER.students; INPUT ID $ Name $ AmtPaid Course $ New; DATALINES; 78374 Adam 350.00 597 1 75638 Michele 525.00 221 1 78634 Jacob 625.00 221 0 28746 . . 597 2 58743 Zina 250.00 435 0 45378 Amy 250.00 435 0 87463 Angela 525.00 221 1 46732 Trevor 450.00 597 0 23867 Michael 450.00 597 0 ; RUN; Copyright 2016 Susan Slaughter

Data for examples Copyright 2016 Susan Slaughter

Data for examples Copyright 2016 Susan Slaughter

Assignment statements Set one thing equal to another General form: variable-name = expression; Example Type of expression x = 5; numeric constant x = '5'; character constant x = y; a variable x = y + 1; addition x = y * z; multiplication Copyright 2016 Susan Slaughter

SAS functions Functions perform calculation or transformation SAS has hundreds of functions Types of functions Character Date and time Descriptive statistics Mathematical Probability Random number State and ZIP code Copyright 2016 Susan Slaughter

SAS functions Functions are often used in assignment statements General form: var = function-name(argument, argument,…); Example: PhoneNum = '(714) 637-9398'; AreaCode = SUBSTR(PhoneNum, 2, 3); Value of AreaCode is ‘714’ Copyright 2016 Susan Slaughter

SAS functions Examples: Test1 = 80; Test2 = .; Test3 = 100; AverageScore1 = Test1 + Test2 + Test3 / 3; AverageScore2 = MEAN(Test1, Test2, Test3); Value of AverageScore1 is missing Value of AverageScore2 is 90 Copyright 2016 Susan Slaughter

Assignment statements Example: * Student enrollment data; DATA SASUSER.students; SET SASUSER.students; Quarter = 'Fall'; FirstInitial = SUBSTR(Name, 1,1); RUN; Copyright 2016 Susan Slaughter

Subsetting IF Special form of IF statement General form: Example: IF condition; Example: IF Age >= 21; SAS will keep only observations with ages 21 or over. Copyright 2016 Susan Slaughter

Conditional logic: IF-THEN IF-THEN statements General form: IF condition THEN action; IF condition AND condition THEN action; IF condition OR condition THEN action; Examples: IF State = 'AZ' THEN Region = 'West'; IF State = 'CA' AND County = 'Yolo' THEN Area = 'Central'; Copyright 2016 Susan Slaughter

Conditional logic: IF-THEN/ELSE IF-THEN/ELSE statements General form: IF condition THEN action; ELSE IF condition THEN action; ELSE action; Copyright 2016 Susan Slaughter

Conditional logic: IF-THEN/ELSE IF-THEN/ELSE statements Bad example: IF State = 'CA' THEN Region = 'West'; IF State = 'MA' THEN Region = 'East'; IF State = 'TX' THEN Region = 'Gulf'; Better example: IF state = 'CA' THEN Region = 'West'; ELSE IF State = 'MA' THEN Region = 'East'; ELSE IF State = 'TX' THEN Region = 'Gulf'; ELSE Region = 'None'; Copyright 2016 Susan Slaughter

Conditional logic Example: * Student enrollment data; DATA SASUSER.students; SET SASUSER.students; IF Name NE ''; IF New = 1 THEN NewStudent = 'yes'; ELSE IF New = 0 THEN NewStudent = 'no'; ELSE NewStudent = '?'; RUN; Copyright 2016 Susan Slaughter

DO groups A single IF-THEN statement can have only one action For multiple actions, use DO/END statements General form: IF condition THEN DO; action; END; Example: IF State = 'HI' THEN DO; Region = 'West'; Capital = 'Honolulu'; Copyright 2016 Susan Slaughter

DO groups Example: * Student enrollment data; DATA SASUSER.students; SET SASUSER.students; IF Course = '221' THEN DO; CourseName = 'Introduction to SAS'; Instructor = 'Susan Slaughter'; END; RUN; Copyright 2016 Susan Slaughter

Pop quiz What statement do you use to read data from a raw data file? INPUT statement What statement do you use to concatenate two SAS data sets? SET statement What statement do you use to match observations from one data set with observations from another data set? MERGE statement Copyright 2016 Susan Slaughter

Pop quiz Write a statement that assigns the value of 10 to a variable named Score. Score = 10; Write a statement that assigns the value of Alaska to a variable named State. State = 'Alaska'; Copyright 2016 Susan Slaughter

Pop quiz What statement do you use to end a DO group? END statement What will this statement do? IF Name = 'Joe'; This subsetting IF statement will keep all observations where the value of the variable Name equals Joe. Copyright 2016 Susan Slaughter

Pop quiz Extra credit: Can an ELSE statement be used without an IF-THEN statement? No, SAS will give you an error if you use an ELSE without an IF-THEN statement. Copyright 2016 Susan Slaughter

Thank you! I hope you can stay for the next presentation. Susan Slaughter Avocet Solutions Can download slides from www.avocetsolutions.com Copyright 2016 Susan Slaughter