HRP 223 2008 HRP 223 2008 Topic 3

  • Slides: 34
Download presentation
HRP 223 – 2008 HRP 223 2008 Topic 3 – Manipulating Data with SQL

HRP 223 – 2008 HRP 223 2008 Topic 3 – Manipulating Data with SQL and EG Copyright © 1999 -2008 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.

Planning an Analysis HRP 223 2008 § Think before you code. § First write

Planning an Analysis HRP 223 2008 § Think before you code. § First write out all the steps as notes in an EG project. § Connect the nodes to the tasks and you have a self documented program.

BMI and Diabetes HRP 223 2008

BMI and Diabetes HRP 223 2008

HRP 223 2008 You want to have well labeled nodes in the flowchart. Be

HRP 223 2008 You want to have well labeled nodes in the flowchart. Be sure to label your library icon with the same name as the library.

Spaghetti and Spiderwebs HRP 223 2008 § As I work on a project, I

Spaghetti and Spiderwebs HRP 223 2008 § As I work on a project, I have the options set to show me the logs (and sometimes the generated code). Once I check the log I delete the log node from the flow.

Be sure to check the log after you submit your code. If it has

Be sure to check the log after you submit your code. If it has only notes saying data or views were created and the number of seconds each task took, then delete the log node and move on to the next step. HRP 223 2008

BMI Analysis (take 2) HRP 223 2008 § Begin with the notes and do

BMI Analysis (take 2) HRP 223 2008 § Begin with the notes and do each task. § Check for a relationship between two continuous variables. – Do a scatter plot. – Look at Karl Pearson’s correlation coefficient. • Why do some people report R and others use Rsquared?

HRP 223 2008 A better solution would be to drop this bad record early

HRP 223 2008 A better solution would be to drop this bad record early on. Be careful of complete case analyses. If you have lots of variables, you do analyses on very different subsets of subjects.

HRP 223 2008

HRP 223 2008

HRP 223 2008

HRP 223 2008

Assignment 2 HRP 223 2008 § If you import the data using the import

Assignment 2 HRP 223 2008 § If you import the data using the import wizard, it looks like the two files are identical. § If you use import code, fix your registry, and specify mixed data in each column, you will notice that both columns come in as character variables! Even if you spend a ridiculous amount of time looking (10 -15 minutes), you probably will not spot the problems.

Compare Two Files HRP 223 2008 § Wouldn’t it be handy if SAS could

Compare Two Files HRP 223 2008 § Wouldn’t it be handy if SAS could compare two files and tell you what cells have actual character data? § If you import the data ignoring the possibility of characters, you will get data sets where the variables are numeric and the “cells” that originally had character data are set to missing. The MIXED line is missing.

Two Data Sets HRP 223 2008 § Labs has two character variables. § Labs.

Two Data Sets HRP 223 2008 § Labs has two character variables. § Labs. Not. Mixed has two numeric variables (one is formatted so it looks like a date).

Manual or Automatic Change HRP 223 2008 § You can manually change the format

Manual or Automatic Change HRP 223 2008 § You can manually change the format in this dataset or have it automatically make the change in a new dataset. If you make the manual change, the next time you import the data the format will reset to DDMMMYYYY.

Reformatted HRP 223 2008 § The formats are still not identical because the Excel

Reformatted HRP 223 2008 § The formats are still not identical because the Excel file has no leading zeros when the month or day is less than 10. You need to make the same format in Excel….

Custom Excel Formats HRP 223 2008 § You can write your own custom formats

Custom Excel Formats HRP 223 2008 § You can write your own custom formats in Excel. Tell it to make a format that shows two digits for month and date, and four digits for year.

Same Dates (sort of) HRP 223 2008 § Now the two dates display the

Same Dates (sort of) HRP 223 2008 § Now the two dates display the same way except one is stored as a character variable and the other is numeric.

Compare? HRP 223 2008 § There is a great comparison procedure in SAS but

Compare? HRP 223 2008 § There is a great comparison procedure in SAS but it wants the variables to be of the same type.

HRP 223 2008

HRP 223 2008

Try to compare the two files. HRP 223 2008 § After it imports, the

Try to compare the two files. HRP 223 2008 § After it imports, the numeric version is missing data. Convert it to be a character variable and then compare the two variables.

The put Function HRP 223 2008 § Recall functions in SAS take 1 or

The put Function HRP 223 2008 § Recall functions in SAS take 1 or more arguments inside parentheses and they return a value. – the. Sin = sin(1) § There is a function called put that takes a numeric variable and returns a character string.

put( variable. Name, format. ) HRP 223 2008

put( variable. Name, format. ) HRP 223 2008

Numeric and Character HRP 223 2008

Numeric and Character HRP 223 2008

Same Voodoo on Dates HRP 223 2008 § Here we want to put the

Same Voodoo on Dates HRP 223 2008 § Here we want to put the date as a character string after applying the MM/DD/YYYY format.

Converting this Numeric to Character HRP 223 2008 § In the case of the

Converting this Numeric to Character HRP 223 2008 § In the case of the assignment 2 data we don’t want to use the best. format because we have to think about the number of decimal places. The value is displayed in Excel and imported as a character string rounded to three decimal places. § We also want to compress out any blank spaces:

The Differences § Excel and SAS use different algorithms for rounding…. § You can

The Differences § Excel and SAS use different algorithms for rounding…. § You can also spot the bad date plus the lab value that is below the detectable limit of the assay. HRP 223 2008

Dealing with Problems HRP 223 2008 § Say you input a numeric variable and

Dealing with Problems HRP 223 2008 § Say you input a numeric variable and it has some bad values in it. This causes it to import as a character variable if you are paying attention (using mixed). You could use the input function and convert it to numeric. This, if not done carefully, will result in missing values. – new. Numeric = input(old. Character, 8. )

Not Many Values HRP 223 2008 § If your variable has only a few

Not Many Values HRP 223 2008 § If your variable has only a few values you can use the Describe > One Way Frequency menu to see all the values, then change the scores with the Filter and Query tool.

Filter and Query Recode § You can recode a problematic variable so that it

Filter and Query Recode § You can recode a problematic variable so that it will become a new numeric variable with the bad values recoded to different types of null. HRP 223 2008

Recode HRP 223 2008

Recode HRP 223 2008

After HRP 223 2008 § Once the data has been recoded you can calculate

After HRP 223 2008 § Once the data has been recoded you can calculate statistics on it. § Be sure to add in a format so the values will display nicely.

Distinct Records § There will be times when you want to get the distinct

Distinct Records § There will be times when you want to get the distinct IDs (or a distinct set of variables) from a dataset. Just check on the select distinct rows checkbox. HRP 223 2008

Dates HRP 223 2008 § Rather than dealing with the headaches of dates, a

Dates HRP 223 2008 § Rather than dealing with the headaches of dates, a lot of databases split the dates into three variables: month, day and year. If the dates are split, it allows you store incomplete dates. You can use the MDY function to combine the 3 parts into a date variable. § the. Date = mdy(the. Month, the. Day, the. Year)

Remember a Display Format § SAS does not apply a date format automatically. HRP

Remember a Display Format § SAS does not apply a date format automatically. HRP 223 2008