BMTRY 789 Lecture 3: Categorical Data and Dates Readings – Chapter 3 & 4 Lab Problems 3.1, 3.2,...

13
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!

Transcript of BMTRY 789 Lecture 3: Categorical Data and Dates Readings – Chapter 3 & 4 Lab Problems 3.1, 3.2,...

Page 1: 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!

BMTRY 789 Lecture 3: Categorical Data and Dates

Readings – Chapter 3 & 4Lab Problems 3.1, 3.2, 3.19, 4.1, 4.3, 4.5Homework – HW 2 Book Problems Due 6/24!

Page 2: 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!

Summer 2009 BMTRY 789 Introduction to SAS Programming 2

Think about what you want … consider if it is the best method to go about it

Page 3: 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!

Summer 2009 BMTRY 789 Introduction to SAS Programming 3

Date Functions & Expressions SAS date functions perform a number of

handy operations (“The Little SAS Book” Readings pp. 80-81 (2nd Ed) pp. 90-91 (3rd Ed)

For example, the TODAY function returns a SAS date value equal to today’s dateAge = ((TODAY() – DOB) / 365.25);

Use a date as a constantEarthDay05 = ’22APR2005’D;

Page 4: 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!

Summer 2009 BMTRY 789 Introduction to SAS Programming 4

Why are SAS dates done this way? SAS can use the SAS date to calculate

many important things, just as it would any other numeric variable.

AGE = INT ((ADMIT – DOB) / 365.25);(Calculates patient age at admission)OrAGE = INT (YRDIF(ADMIT, DOB, ‘Actual’));

LEN_STAY = DISCHRG – ADMIT +1;(Calculates the length of stay including the admission day

and the discharge day)

Page 5: 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!

Summer 2009 BMTRY 789 Introduction to SAS Programming 5

Data librarycards;

INFILE ‘c:MyData\Dates.dat’ TRUNCOVER;

INPUT Name$ :11. +1 BirthDate: Date9. +1 IssueDate :MMDDYY10.;

ExpireDate = IssueDate + (365.25 *3);

ExpireQuarter = QTR(ExpireDate);

If IssueDate > ’01jan1999’D Then NewCard = ‘yes’;

Run;

Proc Print Data = librarycards;

FORMAT IssueDate MMDDYY8. ExpireDate WEEKDATE17.;

Title ‘SAS Dates without and with Formats’;

Run;

Page 6: 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!

Summer 2009 BMTRY 789 Introduction to SAS Programming 6

Longitudinal Data Longitudinal data are collected on

a group of subjects over time. (Also known as Repeated Measures or Vertical File, etc.)

Page 7: 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!

Summer 2009 BMTRY 789 Introduction to SAS Programming 7

The SET statement ‘SET’ reads observations from a SAS data set to a

new SAS data set. It brings a SAS data set, one observation at a time,

into the DATA step for processing.

Syntax: Example:DATA new-data-set; DATA Friday; SET old-data-set; SET sales;Run; If Day = ‘f’; *subsetting If;

Total = Popcorn + Soda; Run;

*You can use the same name for the ‘old’ and ‘new’ data sets if you want to overwrite the old with the new manipulation.

Page 8: 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!

Summer 2009 BMTRY 789 Introduction to SAS Programming 8

FIRST.byvar & LAST.byvar Automatic variable available only in special

circumstances. MUST SORT the data by the byvariable first!

The FIRST.variable and LAST.variable are available when you are using a BY statement in a DATA step.

The FIRST.variable will have a value of 1 when SAS is processing an observation with the first occurrence of a new value for that variable and a value of 0 for the other observations.

Similarly, if the LAST.variable is the last occurrence of the value it will equal 1 and 0 if it is not the last value. (logical variables)

Page 9: 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!

Summer 2009 BMTRY 789 Introduction to SAS Programming 9

Example (SAS example to be run class)

Data Patients;Input PTID VisDate CD4Cell;Format VisDate MMDDYY8.;Cards;

001 01/02/85 125001 07/25/85 210001 09/03/84 220002 12/12/92 180002 10/19/90 223002 06/14/91 190003 07/23/92 150003 06/01/93 125

;Run;

Proc Print Data = Patients;Title “Print out of Patients

Data”;Run;Proc Sort Data = Patients;

By PTID VisDate;Run;Data LastVisit;

Set Patients;By PTID;If LAST.PTID;

Run;Proc Print data = LastVisit;

Title “Print out of LastVisit Data”;Run;

Page 10: 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!

Summer 2009 BMTRY 789 Introduction to SAS Programming 10

LAG Function What do you do if you want to compute the change

between a single variable from visit to visit with your data in a longitudinal structure?

LAG Function = The value of the variable, the last time the LAG function executed (i.e. the previous value).

Lets look at our last example but assume that we want to look at the differences between our CD4 cell counts from the baseline (first visit) to the final visit.

Page 11: 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!

Summer 2009 BMTRY 789 Introduction to SAS Programming 11

Example (SAS example to be run class)

Data Patients;Input PTID VisDate CD4Cell;Format VisDate MMDDYY8.;Cards;

001 01/02/85 125001 07/25/85 210001 09/03/84 220002 12/12/92 180002 10/19/90 223002 06/14/91 190003 07/23/92 150003 06/01/93 125

;Run;

Proc Sort Data = Patients;By PTID VisDate;

Run;Data FirstLastCD4;

Set Patients;By PTID;If First.PTID or LAST.PTID;DiffCD4= CD4Cell – Lag

(CD4Cell);Run;

Proc Print data = FirstLastCD4; Title ‘Print out of FirstLastCD4 Data’;Run;

Page 12: 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!

Summer 2009 BMTRY 789 Introduction to SAS Programming 12

The Basics of Proc Format (Temporary)

Libname Felix ‘C:\SASDATA’;

Proc Format;Value $groupFMT “TREAT”=“Treatment Group”

“CONTROL”=“Control Group”;Run;

Data Felix.Ex4aInput Group$ X Y Z;Format Group $groupFMT.;

Datalines;CONTROL 12 17 19TREAT 23 25 29CONTROL 19 18 16TREAT 22 22 29;

Program to read a permanent SAS Data Set with Formats:

Libname C ‘C:\SASDATA’;

Proc Format;Value $groupFMT ‘TREAT’=‘Treatment Grp’

‘CONTROL’=‘Control Grp’;Run;

Proc Print Data=C.Ex4a;

Format group $groupFMT;

Run;

Page 13: 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!

Summer 2009 BMTRY 789 Introduction to SAS Programming 13

The Basics of Proc Format (Permanent)Libname Felix ‘C:\SASDATA’;Options FMTSEARCH = (Felix);***We will place the permanent SAS data sets and the formats in C:\SASDATA;Proc Format Library=Felix;

Value $groupFMT ‘TREAT’=‘Treatment Grp’ ‘CONTROL’=‘Control Grp’;

Run;Data Felix.Ex4a

Input Group$ X Y Z;Format Group $groupFMT.;

Datalines;CONTROL 12 17 19TREAT 23 25 29CONTROL 19 18 16TREAT 22 22 29;

Program to read a permanent SAS Data Set with Formats:

Libname C ‘C:\SASDATA’;

Options FMTSEARCH= ( C );

***Tell the program to look in C:\SASDATA for user defined formats;

Proc Print Data=C.Ex4a;

Run;