SAS Programming: Working With Variables. Data Step Manipulations New variables should be created during a Data step Existing variables should be manipulated.

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.
16b. Accessing Data: Means in SAS ®. 1 Prerequisites Recommended modules to complete before viewing this module  1. Introduction to the NLTS2 Training.
Knowing Understanding the Basics Writing your own code part 2 SAS Lab.
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.
Lecture 10 Non Parametric Testing STAT 3120 Statistical Methods I.
I OWA S TATE U NIVERSITY Department of Animal Science Working with Your Data (Chapter 2 in the Little SAS Book) Animal Science 500 Lecture No. 4 September.
Quick Data Summaries in SAS Start by bringing in data –Use permanent data set for these examples Proc Tabulate –Produces summaries very quickly and easily.
15b. Accessing Data: Frequencies in SAS ®. 1 Prerequisites Recommended modules to complete before viewing this module  1. Introduction to the NLTS2 Training.
Introduction to SQL Session 1 Retrieving Data From a Single Table.
1 SAS SAS is a statistics software package developed by SAS Institute Inc. in U.S.A. SAS products include SAS/STAT, SAS/IML, SAS/OR, etc. The most.
Into to SAS ®. 2 List the components of a SAS program. Open an existing SAS program and run it. Objectives.
Welcome to SAS…Session..!. What is SAS..! A Complete programming language with report formatting with statistical and mathematical capabilities.
Introduction to SAS Essentials Mastering SAS for Data Analytics
Chapter 14: Generating Data with Do Loops OBJECTIVES Understand iterative DO loops. Construct a DO loop to perform repetitive calculations Use DO loops.
Lecture 5 Sorting, Printing, and Summarizing Your Data.
Chapter 9 Producing Descriptive Statistics PROC MEANS; Summarize descriptive statistics for continuous numeric variables. PROC FREQ; Summarize frequency.
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.
Niraj J. Pandya, Element Technologies Inc., NJ.  Summarize all possible combinations of class level variables even if few categories are altogether missing.
18b. PROC SURVEY Procedures in SAS ®. 1 Prerequisites Recommended modules to complete before viewing this module  1. Introduction to the NLTS2 Training.
Introduction to SAS. What is SAS? SAS originally stood for “Statistical Analysis System”. SAS is a computer software system that provides all the tools.
Key Data Management Tasks in Stata
1 Experimental Statistics - week 2 Review: 2-sample t-tests paired t-tests Thursday: Meet in 15 Clements!! Bring Cody and Smith book.
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 Macro: Some Tips for Debugging Stat St. Paul’s Hospital April 2, 2007.
TRANSFORMING SAS DATA SETS Creating new SAS data sets with the SET statement – how the DATA step works Creating and transforming variables – assignment.
EPIB 698C Lecture 2 Notes Instructor: Raul Cruz 2/14/11 1.
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.
1 Filling in the blanks with PROC FREQ Bill Klein Ryerson University.
Lesson 6 - Topics Reading SAS datasets Subsetting SAS datasets Merging SAS datasets.
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.
11/16/2015Slide 1 We will use a two-sample test of proportions to test whether or not there are group differences in the proportions of cases that have.
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.
Lecture 3 Topic - Descriptive Procedures Programs 3-4 LSB 4:1-4.4; 4:9:4:11; 8:1-8:5; 5:1-5.2.
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.
Chapter 4 concerns various SAS procedures (PROCs). Every PROC operates on: –the most recently created dataset –all the observations –all the appropriate.
1 EPIB 698C Lecture 4 Raul Cruz-Cano Summer 2012.
Summer SAS Workshop Lecture 3. Summer SAS Workshop Website
Lesson 8 - Topics Creating SAS datasets from procedures Using ODS and data steps to make reports Using PROC RANK Programs in course notes LSB 4:11;5:3.
Controlling Input and Output
Time Series Data Processes by Tai Yu April 15, 2013.
SAS for Data Management and Analysis
SW388R6 Data Analysis and Computers I Slide 1 Comparing Central Tendency and Variability across Groups Impact of Missing Data on Group Comparisons Sample.
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.
FORMAT statements can be used to change the look of your output –if FORMAT is in the DATA step, then the formats are permanent and stored with the dataset.
Multiple Imputation using SAS Don Miller 812 Oswald Tower
HRP Copyright © Leland Stanford Junior University. All rights reserved. Warning: This presentation is protected by copyright law and.
17b.Accessing Data: Manipulating Variables in SAS ®
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.
Chapter 17 Supplement: Alternatives to IF-THEN/ELSE Processing STAT 541 ©Spring 2012 Imelda Go, John Grego, Jennifer Lasecki and the University of South.
Chapter 8: Using Basic Statistical Procedures “33⅓% of the mice used in the experiment were cured by the test drug; 33⅓% of the test population were unaffected.
Using Data from the National Survey of Children with Special Health Care Needs Centers for Disease Control and Prevention National Center for Health Statistics.
Chapter 6: Modifying and Combining Data Sets  The SET statement is a powerful statement in the DATA step DATA newdatasetname; SET olddatasetname;.. run;
1 Checking Data with the PRINT and FREQ Procedures.
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.
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 16 & 17 By Tasha Chapman, Oregon Health Authority.
SAS ® 101 Based on Learning SAS by Example: A Programmer’s Guide Chapters 5 & 6 By Ravi Mandal.
SAS ® 101 Based on Learning SAS by Example: A Programmer’s Guide Chapters 3 & 4 By Tasha Chapman, Oregon Health Authority.
Session 1 Retrieving Data From a Single Table
Applied Business Forecasting and Regression Analysis
Quick Data Summaries in SAS
Chapter 14 Sorting and Merging.
Producing Descriptive Statistics
Presentation transcript:

SAS Programming: Working With Variables

Data Step Manipulations New variables should be created during a Data step Existing variables should be manipulated during a data step

Missing Values in SAS SAS uses a period (.) to represent missing values in a SAS data set Different SAS procedures and functions treat missing values differently - always be careful when your SAS data set contains missing values

Working With Numeric Variables SAS uses the standard arithmetic operators +, -, *, /, ** (exponentiation) Note on Missing Values: Arithmetic operators propagate missing values. SAS has many built-in numeric functions round(variable,value): Rounds variable to nearest unit given by value. sum(variable1, variable2, …): Adds any number of variables and ignores missing values

Acting on Selected Observations Working with selected observations - subsets of a SAS data set - is easy in SAS First, you must decide on a selection process. What is the distinguishing characteristic of the observations you want to work with?

Selecting Observations: IF-THEN Statements The IF-THEN statement is the most common way to select observations. Format: IF condition THEN action; condition is one or more comparisons. For any observation, condition is either true or false. If condition is true, SAS performs the action.

IF-THEN Statement: Example Suppose INC is a variable representing annual household income and you want to create a dummy variable, DUM, based on income that takes value 1 when income is less than $10,000. IF INC<10000 THEN DUM=1; IF INC >=10000 THEN DUM=0;

Using OBS in condition In a SAS data set, each record has an observation number which is the number stored in the variable OBS OBS can be used in a condition, but you must refer to the observation number using the variable _n_ Example: set the first 10 observations of INC equal to zero IF _n_ <= 10 THEN INC=0;

Comparison Operators There are 6 comparison operators Can use either the symbol or mnemonic SymbolMnemonicMeaning =EQEqual to ^=NENot equal to >GTGreater than <LTLess than >=GEGreater than or equal to <=LELess than or equal to

Multiple Comparisons Can make more than one comparison in condition by using AND/OR AND / &: All parts must be true for condition to be true Or / |: At least one part must be true for condition to be true Be careful when using AND/OR Can use parentheses in condition

Selecting Observations for New SAS Data Sets Can use IF-THEN statements to create new SAS data sets Either delete or keep selected observations based on condition

Deleting Observations Format for IF-THEN: IF condition THEN DELETE; Example: Removing missing observations. Suppose the variable INC is missing for some households and you want to drop these observations IF INC=. THEN DELETE;

Keeping Selected Observations A more straightforward way to create new SAS data sets is to keep only those observations that meet some condition. Format: IF condition;

Example The file salary.dat contains data for 93 employees of a Chicago bank. The file contains the following variables: Y: Salary X: Years of education E: Months of previous work experience T: Number of months after 1/1/69 that the individual was hired First 61 observations are females, last 32 males

Example: Create Dummy for Males *Program to create dummy variables and; *new SAS data sets ; data salary; infile ‘s:\mysas\salary.dat; input y x e t; IF _n_ >61 THEN G=1; IF _n_ <= 60 THEN G=0; run;

Example: Create Data Set for Males *Make a new SAS data set composed of only; *records for males ; data males; *New SAS data set; set=salary; *Created from salary; IF G=1; run;

Example: Create Data Set for Females *Make a new SAS data set composed of only; *records for females ; data females; *New SAS data set; set=salary; *Created from salary; IF G=0; run;

Describing Data: Sample Statistics Format: PROC UNIVARIATE ; VAR variable-list; BY variable-list; FREQ variable; WEIGHT variable;

Selected Options DATA=SAS-data-set; Specify Data Set If omitted, uses most recent SAS data set FREQGenerate Frequency Table NOPRINTSuppress Printed Output

VAR Statement List of variables to calculate sample statistics for. If no variables are specified, sample statistics are generated for all numeric variables

WEIGHT Statement Specifies a numeric variable in the SAS data set whose values are used to weight each observation

BY Statement Can be used to obtain separate analyses on observations in groups defined by some value of a variable. Example: Suppose SEX=1 if individual is male, SEX=0 if individual is female; EARN=annual earnings. PROC UNIVARIATE; *Generates statistics; VAR EARN; *on earnings for men; BY SEX; *and women; RUN;

BY Statements and Sorting Before using a BY statement, the SAS data set must be sorted on the variable specified SAS puts the observations in order, based on the values of the variables specified in the BY statement. Use PROC SORT

PROC SORT FORMAT: PROC SORT ; BY variables; Sort Order: ascending. For descending, put DESCENDING on BY line

Describing Data: Frequencies FORMAT: PROC FREQ ; BY variables; TABLES requests ; WEIGHT variable;

One-Way Frequency Table SEX=1 (Male) SEX=0(Female) EDUCATION=1(Less than High School), =2(High School),=3(Some College),=4(College grad.) EARN=Annual Earnings PROC FREQ; TABLES EDUCATION; RUN; PROC FREQ; TABLES EDUCATION; BY SEX; RUN;