hrp 223 – 2008

Post on 30-Jan-2016

29 Views

Category:

Documents

0 Downloads

Preview:

Click to see full reader

DESCRIPTION

HRP 223 – 2008. Topic 3 – Manipulating Data with SQL and EG. Planning an Analysis. 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. - PowerPoint PPT Presentation

TRANSCRIPT

HRP223 2008

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.

HRP 223 – 2008

Topic 3 – Manipulating Data with SQL and EG

HRP223 2008

Planning an Analysis

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.

HRP223 2008

BMI and Diabetes

HRP223 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.

HRP223 2008

Spaghetti and Spiderwebs

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.

HRP223 2008

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.

HRP223 2008

BMI Analysis (take 2)

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 R-

squared?

HRP223 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.

HRP223 2008

HRP223 2008

HRP223 2008

Assignment 2

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.

HRP223 2008

Compare Two Files

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.

HRP223 2008

Two Data Sets

Labs has two character variables. LabsNotMixed has two numeric variables (one

is formatted so it looks like a date).

HRP223 2008

Manual or Automatic Change

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.

HRP223 2008

Reformatted

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….

HRP223 2008

Custom Excel Formats

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.

HRP223 2008

Same Dates (sort of)

Now the two dates display the same way except one is stored as a character variable and the other is numeric.

HRP223 2008

Compare?

There is a great comparison procedure in SAS but it wants the variables to be of the same type.

HRP223 2008

HRP223 2008

Try to compare the two files.

After it imports, the numeric version is missing data. Convert it to be a character variable and then compare the two variables.

HRP223 2008

The put Function Recall functions in SAS take 1 or more

arguments inside parentheses and they return a value. – theSin = sin(1)

There is a function called put that takes a numeric variable and returns a character string.

HRP223 2008

put( variableName, format. )

HRP223 2008

Numeric and Character

HRP223 2008

Same Voodoo on Dates

Here we want to put the date as a character string after applying the MM/DD/YYYY format.

HRP223 2008

Converting this Numeric to Character

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:

HRP223 2008

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.

HRP223 2008

Dealing with Problems

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.– newNumeric = input(oldCharacter, 8.)

HRP223 2008

Not Many Values 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.

HRP223 2008

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.

HRP223 2008

Recode

HRP223 2008

After

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.

HRP223 2008

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.

HRP223 2008

Dates

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.

theDate = mdy(theMonth, theDay, theYear)

HRP223 2008

Remember a Display Format

SAS does not apply a date format automatically.

top related