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.

Slides:



Advertisements
Similar presentations
Java Programming, 3e Concepts and Techniques Chapter 3 Section 63 – Manipulating Data Using Methods – Day 2.
Advertisements

Pascal Programming Today Chapter 4 1 »Conditional statements allow the execution of one of a number of possible operations. »Conditional statements include:
Introduction to Unix – CS 21 Lecture 11. Lecture Overview Shell Programming Variable Discussion Command line parameters Arithmetic Discussion Control.
Professional Seminar Northwestern Polytechnic University By Dr. Michael M Cheng.
JavaScript Part for Repetition Statement for statement Cpecifies each of the items needed for counter-controlled repetition with a control variable.
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.
SAS Programming: Working With Variables. Data Step Manipulations New variables should be created during a Data step Existing variables should be manipulated.
I OWA S TATE U NIVERSITY Department of Animal Science Getting Started Using SAS Software Animal Science 500 Lecture No. 2.
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.
1 Creating and Tweaking Data HRP223 – 2010 October 24, 2011 Copyright © Leland Stanford Junior University. All rights reserved. Warning: This.
CS Lecture 03 Outline Sed and awk from previous lecture Writing simple bash script Assignment 1 discussion 1CS 311 Operating SystemsLecture 03.
The number of calories burned per hour by cycling, jogging and swimming are 200, 475 and 275 respectively. A person loses 1pound of weight for each 3500.
Flow of Control MINS298c Fall 1998 Chapter 9. Overview ABAP Programming Structures for: –Iteration –Decisions Control Flow –If … Then –Do & While loops.
CS241 PASCAL I - Control Structures1 PASCAL I - Control Structures Philip Fees CS241.
Shell Programming 1. Understanding Unix shell programming language: A. It has features of high-level languages. B. Convenient to do the programming. C.
Understanding SAS Data Step Processing Alan C. Elliott stattutorials.com.
Welcome to SAS…Session..!. What is SAS..! A Complete programming language with report formatting with statistical and mathematical capabilities.
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.
Computer Science Selection Structures.
BMTRY 789 Lecture 3: Categorical Data and Dates Readings – Chapter 3 & 4 Lab Problems 3.1, 3.2, 3.19, 4.1, 4.3, 4.5 Homework – HW 2 Book Problems Due 6/24!
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.
Programming Fundamentals. Today’s lecture Decisions If else …… Switch Conditional Operators Logical Operators.
SAS Efficiency Techniques and Methods By Kelley Weston Sr. Statistical Programmer Quintiles.
Input, Output, and Processing
EPIB 698C Lecture 2 Notes Instructor: Raul Cruz 2/14/11 1.
7-1 Chapter 7.  Basic Arithmetic Verbs  Options Available with Arithmetic Verbs  COMPUTE Statement  Signed Numbers in Arithmetic Operations  Intrinsic.
Flow of Control Part 1: Selection
Lesson 2 Topic - Reading in data Chapter 2 (Little SAS Book)
1 Boolean Expressions to Make Comparisons Boolean expression –Represents only one of two states –Expression evaluates to either true or false Expressions.
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 2 Input, Processing, and Output.
Lesson 6 - Topics Reading SAS datasets Subsetting SAS datasets Merging SAS datasets.
Shells. Variables MESSAGE="HELLO WORLD" echo $MESSAGE MESSAGE="HELLO WORLD $LOGNAME" echo $MESSAGE MESSAGE="HELLO WORLD $USER" echo $MESSAGE.
Chapter 22: Using Best Practices 1 STAT 541 ©Spring 2012 Imelda Go, John Grego, Jennifer Lasecki and the University of South Carolina.
Prof. Alfred J Bird, Ph.D., NBCT Office – McCormick 3rd floor 607 Office Hours – Tuesday and.
©Colin Jamison 2004 Shell scripting in Linux Colin Jamison.
Assignment statement: Assigns a value to a variable Variable must appear on the left side, value on the right side of the assignment operator Right side.
Chapter 4 concerns various SAS procedures (PROCs). Every PROC operates on: –the most recently created dataset –all the observations –all the appropriate.
Introduction to SAS Essentials Mastering SAS for Data Analytics Alan Elliott and Wayne Woodward SAS Essentials - Elliott & Woodward1.
 2008 Pearson Education, Inc. All rights reserved JavaScript: Introduction to Scripting.
COIT29222 Structured Programming 1 COIT29222-Structured Programming Lecture Week 02  Reading: Textbook(4 th Ed.), Chapter 2 Textbook (6 th Ed.), Chapters.
Section 3.9: RETAIN & sum statements –because all variables are set to missing at the start of each iteration of the DATA step, we need a statement to.
SAS for Data Management and Analysis
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.
1 EPIB 698C Lecture 3 Raul Cruz-Cano Fall Creating and Redefining Variables You can create and redefine variables with assignment statements as.
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.
Shell script – part 2 CS 302. Special shell variable $0.. $9  Positional parameters or command line arguments  For example, a script myscript take 2.
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.
Assigning Values 1. $ set One Two Three [Enter] $echo $1 $2 $3 [Enter] 2. $set `date` [Enter] $echo $1 $2 $3 [Enter] 3. $echo $1 $2 $3 $4 $5 $6 [Enter]
Visual Basic.NET Comprehensive Concepts and Techniques Chapter 5 Decision Making.
PHP Tutorial. What is PHP PHP is a server scripting language, and a powerful tool for making dynamic and interactive Web pages.
SAS ® 101 Based on Learning SAS by Example: A Programmer’s Guide Chapters 5 & 6 By Ravi Mandal.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Extended Prelude to Programming Concepts & Design, 3/e by Stewart Venit and.
How to Calculate Your Body Mass Index (BMI)
Agenda Bash Shell Scripting – Part II Logic statements Loop statements
Assignment statement:
Introduction to DATA Step Programming SAS Basics II
Introduction to DATA Step Programming: SAS Basics II
Computing in COBOL: The Arithmetic Verbs and Intrinsic Functions
Chapter 3: Selection Structures: Making Decisions
Boolean Expressions to Make Comparisons
Instructor: Raul Cruz-Cano 7/19/2012
Chapter 3: Selection Structures: Making Decisions
Matlab Basics.
The Selection Structure
Introduction to Bash Programming, part 3
Factoring if/else code
Presentation transcript:

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 or modifying existing variables is one of the main tasks in DATA step programming. The syntax is: variable = expression ; The variable on the left side can be a new or existing variable; if the variable is a new one, then SAS adds it to the dataset and if it’s an existing variable, SAS redefines it (and so replaces the old values with the new values) defined by the expression on the right.

SAS uses the usual arithmetic signs: +, -, *, /, ** – follows the usual rules for precedence (exponentiation first, then multiplication & division, then addition and subtraction) – follows the usual rules for parentheses (do what’s in parentheses first)… so when in doubt, use parentheses... Example: write a simple SAS program using the 5 arithmetic operations to create new variables… then write some programming statements to show how parentheses can affect those variables…

options ls=80; data test; input x y=x+10; z=x/y; w=x**2; t=x**.5; bd=mdy(1,15,1966); new=round(log10(150)); datalines; ; proc print; format bd mmddyy10.; run;

Many times new variables are created with the built-in functions of SAS... sections 3.2 & 3.3 have a sampling of those put in categories so you can see the variety... categories are: –Character, Date, Financial, Macro, Mathematical –Probability, Random Number, Simple Statistical –State & Zip Code Functions operate on their arguments and are in the form function(arg1, …, argn); so to create a new variable with a function use new_variable = function(argument_list) Functions can be nested within each other: newvar=sqrt(log10(X));

DATA contest; *INFILE 'c:\MyRawData\Pumpkin.dat'; INPUT Name $16. Age Type $1. +1 Date MMDDYY10. (Scr1 Scr2 Scr3 Scr4 Scr5) (4.1); AvgScore = MEAN(Scr1, Scr2, Scr3, Scr4, Scr5); DayEntered = DAY(Date); Type = UPCASE(Type); DATALINES; Alicia Grossman 13 c Matthew Lee 9 D Elizabeth Garcia 10 C Lori Newcombe 6 D Jose Martinez 7 d Brian Williams 11 C ; PROC PRINT DATA = contest; TITLE 'Pumpkin Carving Contest'; RUN;

Go over the functions in section 3.3 in detail - general statements on p. 80 and specific examples on p. 81. Note the variety of arguments for the various functions…

Another way to create new variables is with so-called IF-THEN statements - syntax is IF condition THEN action ; Most of the conditions are based on comparison operators... EQ, NE, GT, LT, GE, LE, IN (or use =, ~= or ^=, >, =, <=) You can also specify multiple conditions with logical operators... OR, AND, NOT (or use |, &, ~ or ^) You can also specify multiple actions using the DO-END loop... (see p also next slide)

In the conditional IF condition THEN action ; condition can also be compound, formed by using the logical connectors AND, OR, NOT or by using the IN statement... see the examples on pages 82 and 83 Can also use the following construction: IF condition THEN DO; action1; action2; END; this DO group can contain many SAS statements, but they are all treated as a unit, and executed one after the other until the END statement is reached

IF-THEN statements are often used to create new categorical variables from existing variables with many different values; or to convert numeric variables to character variables - do some examples… IF-THEN-ELSE statements can help with this task by ensuring that the conditions you are using to create your categories are mutually exclusive. Sometimes the final ELSE statement has no IF…THEN See example on page 85 - note how missing values are handled…

* Group observations by cost; DATA homeimprovements; *INFILE 'c:\MyRawData\Home.dat'; INPUT Owner $ 1-7 Description $ 9-33 Cost; IF Cost =. THEN CostGroup = 'missing'; ELSE IF Cost < 2000 THEN CostGroup = 'low'; ELSE IF Cost < THEN CostGroup = 'medium'; ELSE CostGroup = 'high'; DATALINES; Bob kitchen cabinet face-lift Shirley bathroom addition Silvia paint exterior. Al backyard gazebo Norm paint interior Kathy second floor addition PROC PRINT DATA = homeimprovements; TITLE 'Home Improvement Cost Groups'; RUN;

HW: to me by noon on Wednesday: Use the diabetes data to create the following new variables: –BMI (Body Mass Index = (weight (in kg)) divided by (height in meters) 2 ) –A character variable based on the BMI that puts the participants into groups: below 18.5 is underweight, between 18.5 and 24.9 is normal, 25.0 to 29.9 is overweight, and 30.0 and above is obese. –age classes - make classes by decade (teens, 20s, 30s, etc.)