1 Database Theory and Normalization HRP223 – 2010 November 14 th, 2011 Copyright © 1999-2011 Leland Stanford Junior University. All rights reserved. Warning:

Slides:



Advertisements
Similar presentations
Database Management Systems and Enterprise Software
Advertisements

Query Methods (SQL). What is SQL A programming language for databases. SQL (structured Query Language) It allows you add, edit, delete and run queries.
Slide C.1 SAS MathematicalMarketing Appendix C: SAS Software Uses of SAS  CRM  datamining  data warehousing  linear programming  forecasting  econometrics.
Final Thoughts HRP 223 – 2013 December 4 th, 2013 Copyright © Leland Stanford Junior University. All rights reserved. Warning: This presentation.
Working with Data in Windows HRP223 – 2010 October 4 th, 2010 Copyright © Leland Stanford Junior University. All rights reserved. Warning: This.
1 SAS Formats and SAS Macro Language HRP223 – 2011 November 9 th, 2011 Copyright © Leland Stanford Junior University. All rights reserved. Warning:
Beginning Data Manipulation HRP Topic 4 Oct 19 th 2011.
1 Merging with SQL HRP223 – 2011 October 31, 2011 Copyright © Leland Stanford Junior University. All rights reserved. Warning: This presentation.
1 Lab 2 HRP223 – 2010 October 18, 2010 Copyright © Leland Stanford Junior University. All rights reserved. Warning: This presentation is protected.
1 Processing Grouped Data HRP223 – 2011 November 14 th, 2011 Copyright © Leland Stanford Junior University. All rights reserved. Warning: This.
1 Combining (with SQL) HRP223 – 2010 October 27, 2009 Copyright © Leland Stanford Junior University. All rights reserved. Warning: This presentation.
1 Creating and Tweaking Data HRP223 – 2010 October 24, 2011 Copyright © Leland Stanford Junior University. All rights reserved. Warning: This.
Final Thoughts. When you get data… Check for Viruses Lock the files – Right click> properties>click on read only Assume the data has not been cleaned.
1 Lab 1 HRP223 – 2011 Oct 10, 2011 Copyright © Leland Stanford Junior University. All rights reserved. Warning: This presentation is protected.
Week 6 - Programming I So far, we’ve looked at simple programming via “scripts” = programs of sequentially evaluated commands Today, extend features to:
Program Design and Development
Basic And Advanced SAS Programming
Loops - Monday, Week 4  What are loops?  While loops  Classic loop example - the for loop  Parts of a loop  Examples.
Adding Automated Functionality to Office Applications.
1 Windows and Beginning Data Manipulation HRP223 – 2013 Oct 9, 2012 Copyright © Leland Stanford Junior University. All rights reserved. Warning:
SAS for Categorical Data Copyright © 2004 Leland Stanford Junior University. All rights reserved. Warning: This presentation is protected by copyright.
Chapter 4 MATLAB Programming Combining Loops and Logic Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
MySQL + PHP.  Introduction Before you actually start building your database scripts, you must have a database to place information into and read it from.
1 Data Manipulation (with SQL) HRP223 – 2010 October 13, 2010 Copyright © Leland Stanford Junior University. All rights reserved. Warning: This.
HPR Copyright © Leland Stanford Junior University. All rights reserved. Warning: This presentation is protected by copyright law and.
Working with Data in Windows HRP223 – 2009 Sept 28 th, 2009 Copyright © Leland Stanford Junior University. All rights reserved. Warning: This.
CSC Intro. to Computing Lecture 13: PALGO. Announcements Midterm is in one week  Time to start reviewing  We will do more review in class Tuesday.
HRP Copyright © Leland Stanford Junior University. All rights reserved. Warning: This presentation is protected by copyright law and.
1 Lab 2 and Merging Data (with SQL) HRP223 – 2009 October 19, 2009 Copyright © Leland Stanford Junior University. All rights reserved. Warning:
Copyright © 2010 Certification Partners, LLC -- All Rights Reserved Perl Specialist.
Copyright © Leland Stanford Junior University. All rights reserved. Warning: This presentation is protected by copyright law and international.
1 Summary HRP223 – 2009 November 1 st, 2010 Copyright © Leland Stanford Junior University. All rights reserved. Warning: This presentation is.
What does C store? >>A = [1 2 3] >>B = [1 1] >>[C,D]=meshgrid(A,B) c) a) d) b)
1 Data Manipulation (with SQL) HRP223 – 2010 October 13, 2010 Copyright © Leland Stanford Junior University. All rights reserved. Warning: This.
Copyright © 2003 ProsoftTraining. All rights reserved. Perl Fundamentals.
Lecture 26: Reusable Methods: Enviable Sloth. Creating Function M-files User defined functions are stored as M- files To use them, they must be in the.
HRP Copyright © Leland Stanford Junior University. All rights reserved. Warning: This presentation is protected by copyright law and.
HRP Copyright © Leland Stanford Junior University. All rights reserved. Warning: This presentation is protected by copyright law and.
1 Lab 1 HRP223 – 2011 Oct 10, 2011 Copyright © Leland Stanford Junior University. All rights reserved. Warning: This presentation is protected.
HRP Copyright © Leland Stanford Junior University. All rights reserved. Warning: This presentation is protected by copyright law and.
1 Lab 1 HRP223 – 2009 October 5, 2009 Copyright © Leland Stanford Junior University. All rights reserved. Warning: This presentation is protected.
HRP Copyright © Leland Stanford Junior University. All rights reserved. Warning: This presentation is protected by copyright law and.
HRP 223 – 2007 lm.ppt - Linear Models Copyright © Leland Stanford Junior University. All rights reserved. Warning: This presentation is protected.
1 Data Manipulation (with SQL) HRP223 – 2009 October 12, 2009 Copyright © Leland Stanford Junior University. All rights reserved. Warning: This.
Announcements Assignment 2 Out Today Quiz today - so I need to shut up at 4:25 1.
Beginning Data Manipulation HRP Topic 4 Oct 14 th 2012 Copyright © Leland Stanford Junior University. All rights reserved. Warning: This.
HRP Copyright © Leland Stanford Junior University. All rights reserved. Warning: This presentation is protected by copyright law and.
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 5 & 6 By Ravi Mandal.
Copyright © Leland Stanford Junior University. All rights reserved. Warning: This presentation is protected by copyright law and international.
Trace Tables In today’s lesson we will look at:
This shows the user interface and the SQL Select for a situation with two criteria in an AND relationship.
Repeating code We could repeat code we need more than once: i = 1 print (i) i += 1 print (i) #… stop when i == 9 But each line means an extra line we might.
CS 240 – Lecture 11 Pseudocode.
Working with Data in Windows
Dead Man Visiting Farrokh Alemi, PhD Narrated by …
SAS Output Delivery System
Combining (with SQL) HRP223 – 2013 October 30, 2013
Coding Concepts (Basics)
Lab 3 and HRP259 Lab and Combining (with SQL)
Lab 2 and Merging Data (with SQL)
Combining (with SQL) HRP223 – 2012 November 05, 2011
Lab 2 HRP223 – 2010 October 18, 2010 Copyright © Leland Stanford Junior University. All rights reserved. Warning: This presentation is protected.
Lab 1 HRP223 – 2009 October 5, 2009 Copyright © Leland Stanford Junior University. All rights reserved. Warning: This presentation is protected.
A Bit About SAS/Macro Language Database Theory and Normalization
File Sharing and Processing Grouped Data
Data Manipulation (with SQL)
Processing Grouped Data
Presentation transcript:

1 Database Theory and Normalization HRP223 – 2010 November 14 th, 2011 Copyright © Leland Stanford Junior University. All rights reserved. Warning: This presentation is protected by copyright law and international treaties. Unauthorized reproduction of this presentation, or any portion of it, may result in severe civil and criminal penalties and will be prosecuted to maximum extent possible under the law.

2 Flat Files Some people try to store all their data in a single file. This causes lots of extra work because of holes in the tables and repeated information. Both problems can be fixed by a relational model. – Split the data into many tables. You need to use SQL to work with data split across multiple tables.

3 Not Normalized I frequently get data, from people who are not professional programmers, where the diagnosis data is organized “wide” across the page. Where the first diagnosis is in the first column, the second is in the second, etc. and the task is to find or fix a diagnosis.

4 Subsetting Based on 5 Variables

5

6 SQL vs. Datastep The GUI generates this code: Or you could write a little data step program:

7 Change All 9s to 999s? It is a lot of clicking.

8 Code The SQL is a bit complicated

9 As Data Step If it is more than 5 columns, things get unruly. Imagine doing this across 20 possible diagnoses. There is an easy solution in data step code. First, the SQL code can be done easily in a data step.

10 A List As you can see, there is a list of variables and you are doing the same things over and over. You want to make a list called dx and have the 1 st element refer to dx1, the 2 nd thing refer to dx2, etc. The concept of a named list of variables or an alias to a bunch of variables is instantiated as an array.

11 Arrays A major improvement….. Ummmm. You want to process the same one line over and over. You need to count from 1 to 5…. Sounds like a loop.

12 Change Lots of Things If you have an array, you can process wide files easily.

13 Restructuring with Arrays You can use similar code to restructure data so that you have only a couple of columns of data. Add a new column that is called dxNum and another called theDX. Those two columns plus the subject ID number can contain the same information without all the “holes”.

14 How does that work? Go through all five variables, one at a time. If the variable is not missing, you need to do three things: – Copy the diagnosis counter number into the dxNum variable. – Copy the diagnosis code number into the variable called theDx. – Write to the new data set.

15 Repeated Ifs This is a lot of typing and it obscures the fact that you are doing three things if a condition is true:

16 You have seen do statements in the context where you do stuff over and over. There is also a do end command for when you need to do a block of instructions if a condition is true. do end You need both do and end

17 Actual Code

18 Normalization Part 2 I got data where I needed to analyze age for people who have a particular diagnosis. The data was a not-normalized mess:

19 Normalization Part 2 The Wrong Way If your database is like this, you need code like this: data bad2; set bad; if (dob1 ne. and not missing(dx1)) then do; if code1= 22 then IsCase1=1; else Iscase1=0; end; if (dob2 ne. and not missing(dx2)) then do; if code2=22 then IsCase2=1; else Iscase2=0; end; if (dob3 ne. and not missing(dx3)) then do; if code3=22 then IsCase3=1; else Iscase3=0; end; if (dob4 ne. and not missing(dx4)) then do; if code4=22 then IsCase4=1; else Iscase4=0; end; if (dob5 ne. and not missing(dx5)) then do; if code5=22 then IsCase5=1; else Iscase5=0; end; run; You will end up with the same code repeated as many times as you have repetitions.

20 Normalization Part 2 The Right Way Instead, you should have a record in a table corresponding to each repetition. With code like this: data good2; set good; if code= 22 then isCase1=1; else isCase1=0; run;

21 Your first attempt could go something like this: data normal1 (keep = sid mid dob dx code); set bad; format dob dx mmddyy8.; if (dob1 ne. and dx1 ne. and code1 ne.) then do; mid = 1;dob = dob1; dx = dx1;code = code1; output; end; if (dob2 ne. and dx2 ne. and code2 ne.) then do; mid = 2; dob=dob2; dx=dx2; code=code2; output; end; if (dob3 ne. and dx3 ne. and code3 ne.) then do; mid=3; dob=dob3; dx=dx3; code=code3; output; end; if (dob4 ne. and dx4 ne. and code4 ne.) then do; mid=4; dob=dob4; dx=dx4; code=code4; output; end; if (dob5 ne. and dx5 ne. and code5 ne.) then do; mid=5; dob=dob5; dx=dx5; code=code5; output; end; run; But you end up with just as many blocks of code.

22 Setting up Aliases (Arrays) What you want is a way to repeat this code over the five sets of variables: if (dob1 ne. and dx1 ne. and code1 ne.) then do; mid = 1;dob = dob1; dx = dx1;code = code1; output; end; You need: – A dob alias (dob_a) to refer to dob1, dob2, dob3, dob4 and dob5 – A dx alias (dx_a) to refer to dx1, dx2, dx3, dx4 and dx5 – A code alias (code_a) to refer to code1, code2, code3, code4 and code5

23 Setting up Aliases (Arrays) data normal2a; set bad; array dob_a dob1-dob5; array dx_a dx1-dx5; array code_a code1-code5; if (dob1 ne. and dx1 ne. and code1 ne.) then do; mid = 1;dob = dob1; dx = dx1;code = code1; output; end; run; This sets up the arrays but they are not used in this program.

24 Setting up Aliases (Arrays) data normal2a; set bad; array dob_a dob1-dob5; array dx_a dx1-dx5; array code_a code1-code5; if (dob_a[1] ne. and dx_a[1] ne. and code_a[1] ne.) then do; mid = 1;dob = dob_a[1]; dx = dx_a[1];code = code_a[1]; output; end; run;

25 Setting up Aliases (Arrays) data normal2c (keep = sid mid dob dx code); set bad; array dob_a dob1-dob5; array dx_a dx1-dx5; array code_a code1-code5; do c = 1 to 5 by 1; if (dob_a[c] ne. and dx_a[c] ne. and code_a[c] ne.) then do; mid = c; dob = dob_a[c]; dx = dx_a[c]; code = code_a[c]; output; end; run;

26 Arrays You can tell SAS that a set of variables are related by putting them into an array statement. Arrays in SAS are not like arrays in other languages like BASIC or C. SAS arrays are only aliases to an existing set of variables. They are created using the array statement: array times_a [365] time1-time365; My notation for arrays An optional size of the array What the array refers to

27 Arrays (2) If your array references variables that do not exist, they will be created. Make sure to use the $ if you intend to create character variables. If you want to reference all numeric variables between theValue and thingy2, do it like this: array x theValue -- thingy2 _numeric_; -- means all values between and including the starting and ending variables - indicates the numeric sequence starting with the first variable and ending with the second