******************LAB ASSIGNMENT - Day 3************* * Created by: Meghan Warren (Oakes added day 4 for 4-day course) * Date: January 12, 2005 * answer key for Lab Assignment 3 *****************************************************;
*The data file - clinical - was copied from the CD to the c:\temp directory; libname a 'c:\temp';
/*set up a libname to tell SAS where to look for the data*/
proc contents data = a.clinical; run; proc print data = a.clinical; run;
*1. Collapse CLINICAL on PATNUM and calculate mean and max CHOL, SBP, DBP;
proc means data = a.clinical; class patnum; var chol sbp dbp; output out = collapse mean = mean_chol mean_sbp mean_dbp max = max_chol max_sbp max_dbp; run;
/*collapsing on patnum*/ /*Creating an output data set with the mean var in it*/ /*Naming the means variables*/ /*Naming the max variables*/
*Calcualte the ratio of mean SBP to DBP; data clin_1; set collapse;
/*Name new (Collapsed) data set Clin_1*/ /*Reading in the collapsed data set from above*/
ratio = mean_SBP/mean_DBP; run;
*2. Collapse CLINICAL on PATNUM and Calculate the proportion of ROUTINE visits; data visits; set a.clinical; if routine = 'Y' then rvisit = 1; else rvisit = 0;
/*Creating a variable for routing visit*/ /*I made these numeric so it is easier to do the calculation*/
run; proc means data = visits; class patnum; var rvisit; output out = clin_2 mean=mean_rvisit; run;
/*collapse on patnum*/ /*Creating the output data set of the collapsed data*/
*3. Merge clin_1 and clin_2 (see 4. below);
* 4. Tabulate the proportion of "successful" merges; *You must always sort your data on your merging variable before you do your merge;
proc sort data = clin_1; by patnum; run; proc sort data = clin_2; by patnum; run;
data merged; merge clin_1 (in=a) clin_2 (in=b); by patnum; clin1file = a;
/*Naming the variables to signify if the observation is in the data set*/
clin2file = b; run; title 'Lab 3 Assignment'; title2 'Checking for proportion of successful merges'; *the proc freq will show if the observation is in each data set; *this will create a cross-tabulation table;
proc freq data = merged; tables clin1file*clin2file; run;
*6. Format the value of RATIO (in data formed by merging clin_1 and clin_2) to have only two decimal places;
title2 'Showing the ratio with only 2 decimal places'; proc print data = merged; var patnum ratio; format ratio 8.2; run; *this can also be done in the data step, where you create a new data file and format the ratio within the data step;
*Like this:; data formatting; set merged; format ratio 8.2; run; proc print data = formatting; run;
libname a 'c:\temp'; proc contents data = a.clinical; run;
*6.) Create a new dataset from the CLINICAL data set, name it CLIN_TEMP. Make three new variables in CLIN_TEMP based on the DATE variable. Format the three new data variables so they look like the following:
data clin_temp; set a.clinical; date1 = date; date2 = date; date3 = date; date4 = date;
/*I added this one in to show you what the date looks like when it is not formatted*/
format date1 mmddyyb10. date2 mmddyyd10. date3 mmddyy8.; run; title 'Lab Assignment #4'; title2 'Showing the dates with 3 different formats'; proc print data = clin_temp; var patnum date date1 date2 date3; run;
*7.) What is the SAS date for the subject ID = 02 who visited the clinic on 02/19/90?;
title2 'Getting the SAS date for ID = 2 who visited the clinic 2/19/90'; proc print data = clin_temp; var patnum date date4; /*since the date4 created above has no format with it, it will show the SAS date*/ where patnum = 2; where also date = '19FEB90'd; run;
*8.) Create/generate three new variables from the CLINICAL data set, with names and specifications as follows: VAR Description --------------------------DAY Day of clincial visit data MONTH Month of clinical visit data YEAR Year of clinical visit data; *This requires the use of functions; data fxn; set a.clinical; day = day(date); month = month(date); year = year(date);
*9.) Create/generate a new variable named DATE4 that propoerly combines the three "date" variables MONTH, DAY, and YEAR. Format the new variable so it looks like 06SEP2003;
date4 = mdy(month, day, year); format date4 date9.; run; title2 'Using functions to break apart and then re-combine date variable'; proc print data = fxn; var patnum date day month year date4; run;
*10.) Somehow get the posted EXCEL data into SAS format. Do a proc contents to confirm this; * I used the import wizard under the File on the toolbar to generate this code and import the data from excel;
PROC IMPORT OUT= WORK.exceldata DATAFILE= "C:\Temp\clinical.xls" DBMS=EXCEL2000 REPLACE; GETNAMES=YES; RUN; title2 'Checking the data imported from Excel'; proc contents data = exceldata; run;
Lab 3 Answer Key.pdf
*this will create a cross-tabulation table;. proc freq data ... to have only two decimal places;. title2 'Showing the ratio ... Lab 3 Answer Key.pdf. Lab 3 Answer Key.