130 likes | 233 Views
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!. Think about what you want … consider if it is the best method to go about it. Date Functions & Expressions.
E N D
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!
Think about what you want … consider if it is the best method to go about it BMTRY 789 Introduction to SAS Programming
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 date Age = ((TODAY() – DOB) / 365.25); • Use a date as a constant EarthDay05 = ’22APR2005’D; BMTRY 789 Introduction to SAS Programming
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) Or AGE = INT (YRDIF(ADMIT, DOB, ‘Actual’)); LEN_STAY = DISCHRG – ADMIT +1; (Calculates the length of stay including the admission day and the discharge day) BMTRY 789 Introduction to SAS Programming
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; BMTRY 789 Introduction to SAS Programming
Longitudinal Data • Longitudinal data are collected on a group of subjects over time. (Also known as Repeated Measures or Vertical File, etc.) BMTRY 789 Introduction to SAS Programming
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. BMTRY 789 Introduction to SAS Programming
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) BMTRY 789 Introduction to SAS Programming
Example (SAS example to be run class) 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; Data Patients; Input PTID VisDate CD4Cell; Format VisDate MMDDYY8.; Cards; 001 01/02/85 125 001 07/25/85 210 001 09/03/84 220 002 12/12/92 180 002 10/19/90 223 002 06/14/91 190 003 07/23/92 150 003 06/01/93 125 ; Run; BMTRY 789 Introduction to SAS Programming
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. BMTRY 789 Introduction to SAS Programming
Example (SAS example to be run class) 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; Data Patients; Input PTID VisDate CD4Cell; Format VisDate MMDDYY8.; Cards; 001 01/02/85 125 001 07/25/85 210 001 09/03/84 220 002 12/12/92 180 002 10/19/90 223 002 06/14/91 190 003 07/23/92 150 003 06/01/93 125 ; Run; BMTRY 789 Introduction to SAS Programming
The Basics of Proc Format (Temporary) Libname Felix ‘C:\SASDATA’; Proc Format; Value $groupFMT “TREAT”=“Treatment Group” “CONTROL”=“Control Group”; Run; Data Felix.Ex4a Input Group$ X Y Z; Format Group $groupFMT.; Datalines; CONTROL 12 17 19 TREAT 23 25 29 CONTROL 19 18 16 TREAT 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; BMTRY 789 Introduction to SAS Programming
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 19 TREAT 23 25 29 CONTROL 19 18 16 TREAT 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; BMTRY 789 Introduction to SAS Programming