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.

Slides:



Advertisements
Similar presentations
All Possible Regressions and Statistics for Comparing Models
Advertisements

CSE 1301 Lecture 5B Conditionals & Boolean Expressions Figures from Lewis, “C# Software Solutions”, Addison Wesley Briana B. Morrison.
Introduction to C Programming
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.
Data Types in Java Data is the information that a program has to work with. Data is of different types. The type of a piece of data tells Java what can.
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.
1 Creating and Tweaking Data HRP223 – 2010 October 24, 2011 Copyright © Leland Stanford Junior University. All rights reserved. Warning: This.
Instructor: Craig Duckett CASE, ORDER BY, GROUP BY, HAVING, Subqueries
True or false A variable of type char can hold the value 301. ( F )
Computer Science 1620 Variables and Memory. Review Examples: write a program that calculates and displays the average of the numbers 45, 69, and 106.
5-1 Flow of Control Recitation-01/25/2008  CS 180  Department of Computer Science  Purdue University.
Introduction to Computers and Programming Lecture 4: Mathematical Operators New York University.
 2007 Pearson Education, Inc. All rights reserved Introduction to C Programming.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Java Software Solutions Foundations of Program Design Sixth Edition by Lewis.
Chapter 2 Data Types, Declarations, and Displays
Introduction to C Programming
1 Computer Applications in Epidemiology Dongmei Li Lecture 26 5/6/2009.
Microsoft Access 2010 Chapter 7 Using SQL.
Adding Automated Functionality to Office Applications.
Welcome to SAS…Session..!. What is SAS..! A Complete programming language with report formatting with statistical and mathematical capabilities.
I OWA S TATE U NIVERSITY Department of Animal Science Writing Flexible Codes with the SAS Macro Facility (Chapter in the 7 Little SAS Book) Animal Science.
Chapter Seven Advanced Shell Programming. 2 Lesson A Developing a Fully Featured Program.
CHAPTER 13 Creating a Workbook Part 2. Learning Objectives Work with cells and ranges Work with formulas and functions Preview and print a workbook 2.
SAS Efficiency Techniques and Methods By Kelley Weston Sr. Statistical Programmer Quintiles.
1 Conditions Logical Expressions Selection Control Structures Chapter 5.
TRANSFORMING SAS DATA SETS Creating new SAS data sets with the SET statement – how the DATA step works Creating and transforming variables – assignment.
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.
Flow of Control Part 1: Selection
Getting Started with MATLAB 1. Fundamentals of MATLAB 2. Different Windows of MATLAB 1.
1 Boolean Expressions to Make Comparisons Boolean expression –Represents only one of two states –Expression evaluates to either true or false Expressions.
Chapter 4 Introduction to Control Statements Section 1 - Additional Java Operators Section 2 - If Statements Section 3 - If-Else Statements Section 4.
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.
ISU Basic SAS commands Laboratory No. 1 Computer Techniques for Biological Research Animal Science 500 Ken Stalder, Professor Department of Animal Science.
What does C store? >>A = [1 2 3] >>B = [1 1] >>[C,D]=meshgrid(A,B) c) a) d) b)
 2008 Pearson Education, Inc. All rights reserved JavaScript: Control Statements I.
WRITING CONTROL STRUCTURES (CONDITIONAL CONTROL).
Chapter 4 concerns various SAS procedures (PROCs). Every PROC operates on: –the most recently created dataset –all the observations –all the appropriate.
More Perl Data Types Scalar: it may be a number, a character string, or a reference to another data type. -the sigil $ is used to denote a scalar(or reference)
Summer SAS Workshop Lecture 3. Summer SAS Workshop Website
Overview Excel is a spreadsheet, a grid made from columns and rows. It is a software program that can make number manipulation easy and somewhat painless.
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Fluency with Information Technology Third Edition by Lawrence Snyder Chapter.
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.
Chapter 17 Supplement: Alternatives to IF-THEN/ELSE Processing STAT 541 ©Spring 2012 Imelda Go, John Grego, Jennifer Lasecki and the University of South.
Programming in Java (COP 2250) Lecture 12 & 13 Chengyong Yang Fall, 2005.
An Introduction to Programming with C++ Sixth Edition Chapter 5 The Selection Structure.
Based on Learning SAS by Example: A Programmer’s Guide Chapters 1 & 2
4 - Conditional Control Structures CHAPTER 4. Introduction A Program is usually not limited to a linear sequence of instructions. In real life, a programme.
OPERATORS IN C CHAPTER 3. Expressions can be built up from literals, variables and operators. The operators define how the variables and literals in the.
FILES AND EXCEPTIONS Topics Introduction to File Input and Output Using Loops to Process Files Processing Records Exceptions.
Working Efficiently with Large SAS® Datasets Vishal Jain Senior Programmer.
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.
Instructor: Craig Duckett Lecture 09: Tuesday, April 25th, 2017
The Selection Structure
Chapter 3: Working With Your Data
JavaScript: Control Statements.
Creating a Workbook Part 2
Java Programming Control Structures Part 1
Spreadsheets, Modelling & Databases
Chapter 3: Selection Structures: Making Decisions
Boolean Expressions to Make Comparisons
Chapter 3: Selection Structures: Making Decisions
The Selection Structure
Using C++ Arithmetic Operators and Control Structures
DATA TYPES AND OPERATIONS
3.0 - Design A software design specifies how a program will accomplish its requirements A design includes one or more algorithms to accomplish its goal.
Presentation transcript:

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 9, 2010

I OWA S TATE U NIVERSITY Department of Animal Science Working with Your Data To this point we have identified 1. Many forms which data can stored and ultimately imported into SAS 1. Spreadsheets – Excel, Lotus, Quattro Pro, etc. 2. Databases – Access, SQL, others 3. Text files – from Word, WordPad, Notepad, others 4. Other fileformats 2. Many ways to import our data into SAS 1. Import wizard 2. Infile statement 3. Others Many options to use with the importing of the data, formatting the input data, etc.

I OWA S TATE U NIVERSITY Department of Animal Science Modifying your Data u Data step n read and modify data n create a new dataset n performs actions on rows u Proc step n use an existing dataset n produce an output/results n performs actions on columns

I OWA S TATE U NIVERSITY Department of Animal Science Modifying your Data u Creating and redefining variables is straightforward in a SAS data step n variable = expression; u Examples n Newvariable = constant; n Newvariable = oldvariable * constant; n Adjusted Backfat, growth rate, loin muscle area = predetermined equation

I OWA S TATE U NIVERSITY Department of Animal Science Arithmetic Operators OperationSymbolExampleResult +addition Num + Num Example: add two numbers together -subtraction Num - Num Example: 5 – 3 or can use two variables ending wt. – beginning wt. subtract the value of * multiplication (table note 1)(table note 1) 2*y Always have to have * cannot use 2(y) or 2y multiply 2 by the value of Y /division var/5 or can use variable weight gain / days on test divide the value of VAR by 5 ** can also use the ^ exponentiation a**2 or a^2 raise A to the second power

I OWA S TATE U NIVERSITY Department of Animal Science Comparison Operators u Comparison operators set up a comparison, operation, or calculation with two variables, constants, or expressions within the dataset being used. n If the comparison is true, the result is 1. n If the comparison is false, the result is 0. u Comparison operators can be expressed as symbols or with their mnemonic equivalents, which are shown in the following table:

I OWA S TATE U NIVERSITY Department of Animal Science Comparison Operators Symbol Mnemonic EquivalentDefinitionExample =EQequal toa=3 ^=NEnot equal to (table note 1)(table note 1)a ne 3 ¬=NEnot equal to ~=NEnot equal to >GTgreater thannum>5 <LTless thannum<8 >=GEgreater than or equal to (table note 2)(table note 2) sales>=300 <=LEless than or equal to (table note 3)(table note 3)sales<=100 INequal to one of a listnum in (3, 4, 5)

I OWA S TATE U NIVERSITY Department of Animal Science Logical (Boolean) Operators and Expressions SymbolMnemonic EquivalentExample &AND(a>b & c>d) |OR(a>b or c>d) !OR ¦ ¬NOTnot(a>b) ˆNOT ~ Logical operators, also called Boolean operators, are usually used in expressions to link sequences of comparisons.

I OWA S TATE U NIVERSITY Department of Animal Science Order of calculations u The order in which any of the functions follow standard mathematical rules of precedence. u To overcome this parentheses are used to override that order.

I OWA S TATE U NIVERSITY Department of Animal Science Modifying your Data u The DROP or KEEP statements n Used to decrease the number of variables n Usually not a concern with datasets normally encountered n Remember that the variables are dropped or retained (keep) within the SAS dataset unless you specify otherwise

I OWA S TATE U NIVERSITY Department of Animal Science Modifying your Data Data new2; set new; ADG = ((Finalwt. – Beginningwt) / DaysOnTest); Drop Beginningwt DaysOnTest; Proc Means; Run; Quit;

I OWA S TATE U NIVERSITY Department of Animal Science Using IF – THEN Statements u In cases where we want to assign some statement to some of your observations but not all. n For example adjustment factors for backfat, loin muscle area, growth rate that differing by sex of animal u Called condition – action statements u IF condition THEN action;

I OWA S TATE U NIVERSITY Department of Animal Science Using IF – THEN Statements u Example1 n If job='banker' then highsal=1; u IF condition AND condition THEN action; u Example2 u If job='banker' and age>65 then ret_banker=1; u If job eq ‘banker’ and age ge 65 tehn ret_banker=1;

I OWA S TATE U NIVERSITY Department of Animal Science Using IF – THEN Statements u Example1 n If job='banker' then highsal=1; u IF condition AND condition THEN action; u Example2 u If job='banker' and age>65 then ret_banker=1; u If job eq ‘banker’ and age ge 65 tehn ret_banker=1;

I OWA S TATE U NIVERSITY Department of Animal Science Using the IN Operator u Using the IN operator makes comparisons and works similarly to the If – Then statement but gives a bit more flexibility u IF Model IN (‘Corvette’, ‘Camaro’) Then make = ‘Cheverolet’; n Assumes you have a column or variable titled Model n Creates new variable or column titled Cheverolet

I OWA S TATE U NIVERSITY Department of Animal Science Using the IN Operator u Example using animal data. u IF SEX IN (‘gilt’, ‘barrow’) Then adjustedBF = BF + ((actualwt – 250) * (actualbf / (actualwt – constant1))); u IF SEX IN (‘boar”) Then adjustedBF = BF + ((actualwt – 250) * (actualbf / (actualwt – constant2)));

I OWA S TATE U NIVERSITY Department of Animal Science Using IF – THEN Statements u A single IF – THEN statement can have only one action u Using the key words DO and END then it is possible to execute more than 1 action u Example IF condition THEN DO; action; END;

I OWA S TATE U NIVERSITY Department of Animal Science Using IF – THEN Statements u A single IF – THEN statement can have only one action u Using the key words DO and END then it is possible to execute more than 1 action u Example IF Model = ‘Mustang” THEN DO; Make = ‘Ford’; Size = ‘Compact’; END;

I OWA S TATE U NIVERSITY Department of Animal Science Using IF – THEN Statements u The AND and OR keywords can be used to specify multiple conditions n IF condition AND condition THEN action; u Example l IF Model = ‘Mustang’ AND Year < 1975 THEN Status = ‘Classic’; both conditions must be met to reach the ‘Classic” status l IF Model = ‘Mustang’ OR Year < 1975 THEN Status = ‘Classic’; only one of the conditions must be met to reach the ‘Classic” status

I OWA S TATE U NIVERSITY Department of Animal Science Using IF – THEN /ELSE Statements u Using the IF - Then / Else statement is typically used to group observations u Basic form of statement n IF condition THEN action; ELSE IF condition THEN action; Advantages: When compared to regular IF – THEN statements 1. Computationally more efficient as it uses less computer time because once a condition is satisfied SAS skips the rest of the steps. 2. The ELSE statement is mutually exclusive thus preventing an observation from ending up in more than one group.

I OWA S TATE U NIVERSITY Department of Animal Science The DO – END statement The DO-END statement is useful if you want to make several changes or create new variables for a subgroup or under certain conditions. Note that the DO-loop continues until you end it using END; Example If sex = ’female’ then do; u AdjustedBF = equation; u AdjustedLMA = equation; u AdjustedDAYS = equation; u End;

I OWA S TATE U NIVERSITY Department of Animal Science The DO – END statement The DO-END statement is useful if you want to make several changes or create new variables for a subgroup or under certain conditions. Note that the DO-loop continues until you end it using END; u Else if sex EQ ’male’ then do; u... u end;

I OWA S TATE U NIVERSITY Department of Animal Science Using IF – THEN /ELSE Statements u Example: u IF CowBC. THEN AdjustedCowBC =.; ELSE IF CowBC ge 9 THEN AdjustedCowBC = 5; ELSE IF CowBC ge 7 and lt 9 THEN AdjustedCowBC = 4; ELSE IF CowBC ge 5 and lt 7 THEN AdjustedCowBC = 3; ELSE IF CowBC ge 3 and lt 5 THEN AdjustedCowBC = 2; ELSE IF CowBC ge 1 and lt 3 THEN AdjustedCowBC = 1; ELSE IF condition THEN action;

I OWA S TATE U NIVERSITY Department of Animal Science Using IF – THEN /ELSE Statements u When the condition is true, SAS assigns the stated value to AdjustedCowBC and then leaves the loop. The last ELSE is a trash-bin: anything that is not covered by the previous conditions is put to missing. u Look at what gets assigned to the last ELSE statement as it may identify an error in the data set or other problems u Make sure the condition part includes all possibilities, else you might get missings or hidden errors. u Examine the log where it will reveal the number of missing values created, but there is no indication for observations that were not covered by your programming!

I OWA S TATE U NIVERSITY Department of Animal Science Subsetting your data u Sometimes researchers or programmers want to look at only a portion of the data that is collected. u Can accomplish this using the IF statement u Example only interested in gilts in a dataset that includes data from boars and barrows. n IF sex = ‘barrow’ THEN delete; n IF sex = ‘boar” THEN delete;

I OWA S TATE U NIVERSITY Department of Animal Science Subsetting your data u Sometimes researchers or programmers want to look at only a portion of the data that is collected. u Another way to use the IF statement u Rather than making it an deletion statement, make an inclusionary statement; u Example only interested in gilts in a dataset that includes data from boars and barrows. n IF sex = ‘gilt’; this results in the program looking at data where sex = gilt

I OWA S TATE U NIVERSITY Department of Animal Science Subsetting your data What is the difference when looking at only gilts if the statements IF sex = ‘gilt’; this results in the program looking at data where sex = gilt; IF sex = ‘barrow’ THEN delete; IF sex = ‘boar” THEN delete; Dataset would include anything coded incorrectly in the dataset other than ‘barrow’ or ‘boar’ Using the inclusionary statement (IF sex =‘gilt”), requires that every line of data be examined

I OWA S TATE U NIVERSITY Department of Animal Science The RETAIN Statements u Use the RETAIN statement when you want to keep some or all of the variable from a previous DATA step. n The RETAIN variable list; can appear anywhere in the DATA step n You can specify an initial value instead of missing for variables as follows RETAIN variable list initial value;

I OWA S TATE U NIVERSITY Department of Animal Science The Sum Statements u The Sum statement is used when have a cumulative total for some variable. Example used in the book; RETAIN MaxRuns; MaxRuns = MAX (MaxRuns, Runs); RunsToDate + Runs; Might want to use something similar if you are totaling milk production across the days in lactation or pigs born alive across parity.

I OWA S TATE U NIVERSITY Department of Animal Science Using Arrays in SAS u An array is a temporary holding site for a collection of variables upon which the same operations will be performed. n Arrays provide convenient shortcuts in programming. n An array is a group of variables that is user defined. n The array is user defined in the DATA step. l All the variables in an array must either be characters or numeric CANNOT mix character and numeric variables in the same array.

I OWA S TATE U NIVERSITY Department of Animal Science Using Arrays in SAS u Array name (n) $ (may or may not be included) variable list. n The number (n) must match the number of variables in the list. n An array by itself does nothing n You create the array to perform some function that you want to perform on all array variables (book uses changing missing value from 9 to.

I OWA S TATE U NIVERSITY Department of Animal Science Using Shortcuts for Lists of Variable Names u Provides you a listing for inputting variables that have very similar names n Variable1, Variable2, Variable3, Variable4 and so forth n You could use an input statement that includes all of the names – INPUT Variable1 Variable2 Variable3 Variable4; n Alternatively you could write it n INPUT Variable1 – Variable4; and all variable will have been inputted.