Presentation is loading. Please wait.

Presentation is loading. Please wait.

Working With Dates: Dates Come in Many Ways

Similar presentations


Presentation on theme: "Working With Dates: Dates Come in Many Ways"— Presentation transcript:

1 Working With Dates: Dates Come in Many Ways
10/18/04 18/10/04 10/18/2004 18OCT2004 101804 October 18, 2004 Need to know how to read-in dates and then work with them The rest of this lesson will discuss working with variables that are dates. Dates come in many different formats several of which are displayed here. These are all possible formats for the date: October 18th, Sometimes the month is first, sometimes it is second, sometimes the year is 4 digits, sometimes it is only 2. Sometimes the first 3 characters of the months are spelled out. Dates can have separators and sometimes not. Whatever the format we will want to know how to read them into SAS and how to work with them Fortunately, SAS makes reading and working with dates fairly easy.

2 What do you want to do with dates?
Display them Compare two dates: find the number of days between 2 dates ndays = date2 - date1; Will this work? Before we learn how to read in dates, let’s consider what you might want to do with dates once they are read in. This first thing we might want to do is simply to display them. For example, we might want to list for each patient the date they were enrolled, or the date they last came in for a visit. The second thing we will usually want to do with dates is to compare two dates, to see which date is earlier, or more generally to calculate the number of days between the two dates. Think about computing the age of a person at the time they entered a study if their birth date and their date enrolled are available. You would like to subtract the enrollment date from the birth date and somehow come up with the number of years between the dates. However, dates in their raw form do not subtract very well. Even if you could ignore the separators the result would not be what you want. SAS does something special when dates are read in that makes subtracting two dates be the number of days between them. Once you have that you can compute the number of years easily.

3 Jan 1, 1960 data dates; infile datalines; input @1 brthdate mmddyy10.;
03/03/1971 02/14/1956 01/01/1960 ; proc print; var brthdate; proc print; var brthdate; format brthdate mmddyy10.; Obs brthdate /03/1971 /14/1956 /01/1960 Let’s look at a simple program that illustrates how to read in dates and what SAS does with them. Here we have a short DATA step that reads in the birth dates for three persons. We use the INFORMAT mmddyy10. which reads in dates that take up 10 positions, having the pattern month/day/year with a slash separating the three portions of the date. There are similar INFORMATS for different date formats. We then do a PROC PRINT on the variable brthdate. Notice the values look nothing like dates, they are numbers – one positive, one negative, and one equal to zero. What is going on here? Well, when SAS reads in a date with a date informat SAS makes the variable numeric and assigns the value relative to the date of January 1, Dates before this date will be negative and dates after this date with be positive (and dates equal to this date will be assigned a value of zero). This explains our display for the three dates. Each date has the value of the number of days from January 1, 1960. To display dates properly, as a date, you need to apply a date FORMAT to the variable as shown here in the second PROC PRINT. SAS will then display the value as a date as displayed here. Jan 1, 1960

4 When you read in a variable with a date informat:
SAS makes the variable numeric SAS assigns the numeric value relative to January 1, 1960 This makes it easy to subtract two dates to get the number of days between the dates. dayselapsed = date2 – date1; Here is a summary: When SAS reads in a date with a date informat it make the variable numeric and assigns the value relative to January 1, You might ask, what good does this do? Well, this make it easy to subtract dates to find the number of days between the dates. You simply subtract the two date variables. The January 1, 1960 date is arbitrary, SAS could have used any date. An important thing to remember is that once the date is read-in with a date informat SAS looks at it as any numeric variable. Remember variables in SAS are either character or numeric, there is no “date” kind of variable. If you always want SAS to display a date variable in a date format, then assign a date format to the variable in the DATA step as shown here.

5 * Working with Dates run; libname t ‘/folders/myfolders/’; data age;
set t.tomhss (keep=ptid randdate brthdate); agedays = randdate - brthdate ; ageyrs = (randdate - brthdate)/365.25; ageint = int( (randdate - brthdate)/365.25); yrrand = year(randdate); run; OK, let’s look at program 9 from the course notes. The DATA step reads in three date variables using the mmddyy10. informat. The variables are the patient’s randomization date, birth date, and 12-month visit date. We will illustrate how to compute ages and some other things involving dates. The first new variable we compute is variable agedays which is the simple difference between the randomization and birth date. This will contain the number of days between the two dates. If we divide this difference by we can get age in years (variable ageyrs). Lastly, since we usually report ages as “completed years of life” we use the integer function INT to remove the decimal portion of the years (variable ageint). SAS also has a function that can compute age, the function YRDIF. You provide the first and second date along with a method to compute years Here we use the “Actual” method. You can look up this function under the SAS help to get details on the different options for calculating years. If you want to compute the age as of a certain date you can use the MDY function which creates a temporary variable with that date from which you can subtract the birth date. Here we compute the patient’s age as of February 28, 1992, the end of the study. There is also the TODAY function if you would like to compute the age as of the date you run the program. Next, suppose you want to know if a patient came in for their 1-year visit within +- one month of their one year anniversary from randomization. We first compute the number of days between the 1-year visit date and the randomization date (variable daysv12); then we use IF-THEN-ELSE statement to determine if the patient came in within a month of the expected date. ABS in the absolute value function. If the two dates differ by less than 31 days we say the visit was in the window (window12 = 1) ; if it is outside this number of days we say the visit was outside the window (window12 = 2). If the visit was missed then we set window12 to missing. Lastly, we illustrate the YEAR function. This function takes a date and extracts the year from it; so the variable yrrand will contain the year of enrollment. There are similar functions for month and day of the week.

6 run; proc contents data=age varnum; Variables in Creation Order
# Variable Type Len Format Label 1 ptid Char Patient ID 2 randdate Num MMDDYY Randomization Date 3 brthdate Num MMDDYY Birth Date 4 date Num MMDDYY Visit Date at 12 Months 5 agedays Num 6 ageyrs Num 7 ageint Num 8 agetoday Num 9 ageendst Num 10 daysv Num 11 window12 Num 12 yrrand Num OK, let’s look at program 9 from the course notes. The DATA step reads in three date variables using the mmddyy10. informat. The variables are the patient’s randomization date, birth date, and 12-month visit date. We will illustrate how to compute ages and some other things involving dates. The first new variable we compute is variable agedays which is the simple difference between the randomization and birth date. This will contain the number of days between the two dates. If we divide this difference by we can get age in years (variable ageyrs). Lastly, since we usually report ages as “completed years of life” we use the integer function INT to remove the decimal portion of the years (variable ageint). SAS also has a function that can compute age, the function YRDIF. You provide the first and second date along with a method to compute years Here we use the “Actual” method. You can look up this function under the SAS help to get details on the different options for calculating years. If you want to compute the age as of a certain date you can use the MDY function which creates a temporary variable with that date from which you can subtract the birth date. Here we compute the patient’s age as of February 28, 1992, the end of the study. There is also the TODAY function if you would like to compute the age as of the date you run the program. Next, suppose you want to know if a patient came in for their 1-year visit within +- one month of their one year anniversary from randomization. We first compute the number of days between the 1-year visit date and the randomization date (variable daysv12); then we use IF-THEN-ELSE statement to determine if the patient came in within a month of the expected date. ABS in the absolute value function. If the two dates differ by less than 31 days we say the visit was in the window (window12 = 1) ; if it is outside this number of days we say the visit was outside the window (window12 = 2). If the visit was missed then we set window12 to missing. Lastly, we illustrate the YEAR function. This function takes a date and extracts the year from it; so the variable yrrand will contain the year of enrollment. There are similar functions for month and day of the week.

7 proc print data=age (obs=5);
var brthdate randdate agedays ageyrs ageint ; title 'Printing Dates Without a Date Format'; format brthdate randdate;* Remove formats; run; var brthdate randdate agedays ageyrs ageint ; format brthdate mmddyy10. randdate mmddyy10.; title 'Printing Dates With a Date Format'; Now that we have defined a bunch of new variables we will use PROC PRINT to display some of the date and age variables we created; first without using date formats and in the second procedure with a format for the two date variables. We restrict the output to 10 observations.

8 Printing Dates Without a Date Format
Obs brthdate randdate agedays ageyrs ageyrsX ageint All before 1960 In the first display note that the variable brthdate displays as a negative integer value – these are all dates before The variable randdate has values of about 10 thousand; there are the number of days from January 1, The age variables all display correctly because these are not dates, but various times between dates. Note how the integer age in years variable truncates the fractional years from the age in years variable.

9 Printing Dates With a Date Format
Obs brthdate randdate /26/ /10/1987 /01/ /13/1988 /31/ /21/1987 /27/ /10/1987 /02/ /23/1988 This slide displays partial results for the proc print when the two date variables have a date format applied. We see that the variables display as dates, here in the month-day-year format with a four digit year. One note – you can display the dates in any format, not just the format used to read-in the variable. Section 9.3 of the LSB lists several of the available date formats.

10 TITLE 'Frequency Distribution of Year Randomized'; RUN;
PROC FREQ DATA=age; TABLES yrrand ; TITLE 'Frequency Distribution of Year Randomized'; RUN; The FREQ Procedure Cumulative Cumulative yrrand Frequency Percent Frequency Percent Here we run a PROC PRINT to display variables related to whether the patient came in within the visit window. We also run a PROC FREQ to display the frequency distribution of the year of randomization.

11 Topics Reading SAS datasets Sub-setting SAS datasets
Merging SAS datasets Welcome to Lesson 7. In this lesson we will look at working with SAS data sets. This includes creating and using permanent data sets, sub-setting datasets, and merging data sets. These topics are illustrated in programs in the course notes and discussed in the indicated sections of the LSB.

12 Working With SAS Data Sets
Reading SAS dataset SET Statement Merging SAS datasets MERGE Statement There are two key SAS statements when working with SAS data sets. The first is the SET statement which reads or brings-in a SAS dataset. The second is the MERGE statements which brings in multiple SAS datasets and merges them into one SAS dataset. All of these actions will take place in the DATA step. That is where new datasets are created, whether from raw data or from an existing SAS dataset. Done within a DATA step

13 SET STATEMENT data new; set old (keep = varlist); where = condition;
Reads SAS data set Replaces INFILE and INPUT statements used when reading in raw data KEEP brings in selected variables (columns) Where brings in selected observations (rows) data new; set old (keep = varlist); where = condition; run; The SET statement is a simple but important statement that reads a SAS dataset. The syntax is the keyword SET followed by the name of the dataset to read. An optional KEEP= option in parenthesis can be used to restrict the variables brought in. If there is no KEEP statement then all variables are brought in. The WHERE statement limits the observations brought in, based on one or more variables in the dataset brought in. So as the KEEP statement makes the new dataset skinnier, the WHERE statement make the new dataset shorter. These 4 lines of SAS code here create a new dataset called new that contain selected observations from old and the variables from old included in varlist. If there was no KEEP or WHERE statement then the dataset new would be identical to old The SET statement replaces both the INFILE and INPUT statements when reading in raw data. You can see that it is much easier to read a SAS dataset. . This creates a new data set called new that has the variables in varlist and selected observations from old.

14 libname t ‘/folders/myfolders/’; data tdata;
set t.tomhs (keep=ptid clinic randdate group sex sbp12); run; * Making a new dataset containing only men; data men; set tdata; * reads the existing dataset; where sex = 1; * This does the selection; * Add one new variable; if group in(1,2,3,4,5) then active = 1; else if group in(6) then active = 2; * Similar code to make dataset containing only women; * Then have 3 new work datasets available; We will illustrate working with SAS datasets in program 10. We first create a dataset called tdata reading in six variables from the raw file tomhs.data. The RUN statement completes the DATA step. We then create a “subset” data set called men which will contain the rows from tdata for which sex = 1. We do this by using the SET statement followed by the WHERE statement. We do not have a KEEP option so all variables are brought in. We then add a new variable called active based on the variable group. We add a KEEP statement to limit the variables written to the data set men. The new dataset will have 6 variables and as many rows are there are men on the dataset. There is no need to have the variable sex on the data set since we know the data set contains all men. Note: Even if we did not want to limit the new data set to men we would still need a data step to add the variable active.

15 PROGRAM 10 - Merging SAS Datasets
data clinic; infile datalines; input id $ sbp ; datalines; C B B D A B B B A … more data ; data lab; infile datalines; input id $ glucose; datalines; C B D A B B B A D … more data ; Frequently you will have multiple SAS datasets that you want to merge together. For example, laboratory data may be in one dataset and clinical data in another dataset. To perform analyses across the datasets (for example, relating laboratory variables and clinical variables) you first need to merge them together. Program 11 illustrates how to merge datasets using a simple example where all the data is within the program. In this example data comes from two sources: the first is data collected at the clinical center; the second source of data comes from the laboratory. In the first DATA step we create the dataset clinic reading in two variables, the patient ID and systolic BP. The second DATA step creates the dataset lab, reading in the patient ID and glucose. Note that the variable name for patient ID is identical in both datasets (variable id). When you merge datasets you need to have a common variable to link the data together. This is typically an ID of some sort. .

16 * Creating merged dataset; proc sort data= clinic; by id;
proc sort data= lab; by id; data study; merge clinic lab; by id ; run; Note: The BY statement is very important! Before we can merge datasets we need to be sure each dataset is sorted by the variable you want to merge by, in this case the variable id (think of trying to put two stacks of exams together by student ID – it will be much easier and faster if the piles are sorted by student ID). To sort a dataset you use PROC SORT. The syntax is PROC SORT followed by the dataset to be sorted. You follow this by the keyword BY followed by the variable you want the dataset sorted by. PROC SORT sorts the data by the variable in BY and writes the sorted dataset (by default) back to the same dataset. After the two PROC SORTS shown here the datasets baseline and follow-up will be sorted by patient ID. We are then ready to create a new dataset that is the merged data of the two datasets. We do this in a DATA step. Instead of a SET statement we use a MERGE statement followed by the datasets to be merged. This is immediately followed by a BY statement giving the variable that links the data together. That is all you need! In just a couple of lines of code we have created a merged dataset called study. An important note here: Do not forget the BY statement. If you omit the BY statement then SAS does a one-to-one matching, taking the first row of clinic and merging it with the first row of lab. This is not what you want to do; the data from one patient may get merged with a different patient. (think of the example of merging the two exams together)!

17 Merged Dataset Obs id sbp glucose 1 A00869 110 99 2 A01088 117 93
Here is the result of the merged dataset , displaying the variables using PROC PRINT. The two observations in red had data only in the clinic dataset and the observation in blue had data only in the lab dataset. Note their data is missing for all variables from the missing dataset. That is what you would expect SAS to do. Here then is an important thing to remember. When merging datasets if an observation is not in a dataset then all variables from that dataset are set to missing. What if the same variable name is in both datasets and the subject has data in both datasets? Well, this could cause a problem. SAS will take the right most dataset value. If that is what you want then you are OK. In general, though, you want variables to have unique names across the datasets (except for the merge by variable).

18 Things to Remember When Merging Datasets
Need to have common variable name is each dataset to use as linking variable Variables in dataset with no match will be set to missing Rows matched that have same variable names will be assigned right-most dataset value Always remember the BY statement in the merge! Here is a summary of important points to remember when merging datasets. First, you need to have a common variable in each dataset to be merged. This is usually an ID of some sort. Second, if an ID is found in one dataset but not the other than variables in the dataset not found are set to missing. Third, if there are common variable names in both datasets then the values form the second dataset will be used. As mentioned, you usually want unique names across datasets, except for the linking variable. Lastly always remember the BY statement after the MERGE. Otherwise you will usually get the wrong results. If you are database inclined, you may want to explore PROC SQL to merge your data. It is more complicated to use, but it is more flexible, and if you have database experience you will be familiar with the syntax.

19 Temporary vs Permanent SAS Datasets
Temporary (or work) SAS dataset - After SAS session is over the dataset is deleted. DATA bp; * bp is deleted after SAS session; Permanent SAS dataset - After program is run the dataset is saved and is available for use in future programs. You need to tell SAS where to store/retrieve the dataset. Note: For PC SAS the work dataset is available until you end the SAS session. In the programs we have looked at so far the SAS data sets created have been temporary SAS datasets. They are temporary in that after your SAS session is over the datasets are deleted. These are also called “work” datasets. In the DATA statement here the dataset bp is temporary. It will be deleted after the SAS session is over. This is usually not a problem because we created the data set with a DATA step in a program. To recreate the data set we can just re-run the program. However, there are times when you would like to have the dataset available after your SAS session is over, without having to re-create it using a DATA step. To create a permanent data set you will need to tell SAS where to store the file containing the dataset. We will look at some reasons why you may want to create a permanent SAS dataset and the syntax for how to do that.

20 Reasons to Create Permanent SAS Datasets
Read raw data and compute calculated variables only once All variables have assigned names and labels. Data is ready to be analyzed. Dataset can be sent to other computers or users. Listed here are reasons for creating a permanent SAS dataset. One reason is that you will then need to read the raw data only once. A second reason is that you can create all your calculated variables once, storing them in the SAS data set. The calculated variables may involve complicated formula or logic. You don’t want to do that every time, especially if multiple programmers are using the data. You want to get all the variable defined up-front. This will reduce the chance of errors. With the SAS dataset all the variables have assigned names and is ready to go, i.e. be analyzed. The data-step that read the raw data and computed new variables is eliminated, once the dataset is created. Often times you may still need to create or recode additional variables in a DATA step, but this will usually be simple and straightforward. The last reason given here is that SAS data sets are a good way to send data to other users or computers. You simply sent the file to another user and everything is ready to go – all the variables have been defined. SAS will need to be installed on the new computer for the dataset to be accessed.

21 Creating a Permanent Dataset
LIBNAME mylib ‘/folders/myfolders/’; LIBNAME – assigns a directory (folder) reference name. In this example the folder ‘/folders/myfolders/ is assigned a reference name of mylib. You will tell SAS to write to the SAS dataset to this folder. To create a permanent SAS dataset you need to use a LIBNAME statement to tell SAS where to store the dataset. LIBNAME stands for library name. After the keyword LIBNAME follows what is called a library reference. This is a name you assign that points to a directory (i.e. a folder) on your system. After the library reference you indicate the directory that the library reference points to. In the example the library reference named mylib points to the ‘My SAS Datasets’ folder. Library names are similar to filename statements that we learned about in program 2, except the filename statements point to a file whereas libname statements point to a folder. You then use the library reference name in the DATA statement as shown here. The DATA statement here tells SAS to create a data set called sescore in the directory referenced by mylib, which points to the C:\My SAS Datasets directory. Note the form of specifying the dataset, the library reference (where to put the file), a period, followed by the name of the dataset. One note – temporary datasets have a library reference called WORK. In this case you do not need to specify the WORK library, i.e. if no library reference is given, SAS assumes the WORK directory, which SAS assigns to some place on your computer. As stated before these files are then deleted by SAS when your SAS session is ended.

22 wtd12 = wt12 - wtbl; drop se12_1-se12_10 ;
libname mylib ‘/folders/myfolders/'; data sescore; * Work dataset created; set mylib.tomhss (keep = ptid clinic randdate group educ wtbl wt12 sbpbl sbp12 se12_1-se12_10); wtd12 = wt12 - wtbl; sbpd12 = sbp12 - sbpbl; sescr12 = mean (of se12_1 - se12_20) ; label wtd12 = 'Weight Change at Baseline'; label sbpd12 = 'Systolic BP Change at 12 Months'; label sescr12 = 'Side Effect Score at 12 Months'; format randdate date9. ; drop se12_1-se12_10 ; run; proc copy in=work out=mylib; select sescore; In program 12 we will create a permanent SAS data set called sescore that will be stored in the ‘C:\SAS_Files’ folder. We first define a library reference called mylib that points to this folder. We could have called it anything, we would just need to be consistent with the name in the DATA statement. The two words in red here need to be the same. We then read-in several variables from tomhs.data, including 20 side-effect items at baseline and the same 20 side-effects at the 12-month visit. We use array type notation as a shortcut in reading in these variables.

23 Using PROC COPY to copy work dataset to permanent dataset
Make a work dataset first – then when you know that is working correctly copy the work dataset to a permanent dataset. proc copy in=work out=mylib; select sescore; run; Instead of using a two-part name to create a permanent dataset you can use the copy procedure to copy the work dataset to a permanent library or folder on your computer. You use the IN option to specify the WORK library and then use the OUT option to specify the library (folder) you want the dataset stored. This is usually the way I make permanent datasets, that way my program always has work datasets and then I use PROC COPY to create the permanent dataset when I need to.

24 27 * Place dataset in mylib folder;
proc copy in = work out=mylib; select sescore; run; NOTE: Copying WORK.SESCORE to MYLIB.SESCORE (memtype=DATA). NOTE: There were 100 observations read from the data set WORK.SESCORE. NOTE: The data set MYLIB.SESCORE has 100 observations and 14 variables.

25 Reading Permanent SAS Dataset
libname t ‘/folders/myfolders/' ; * Tells SAS where to find the SAS dataset; proc means data=t.sescore ; title 'Means of All Numeric Variables on SAS Permanent Dataset'; run; What if dataset was moved to a different folder? Just need to change LIBNAME So you have created a permanent dataset called sescore. Now you or another user want to use it. (or maybe the dataset was sent to you and you have stored in on your computer and now want to use it. Well, just as you needed to tell SAS where to store it when you created the dataset, you will need to tell SAS where it is stored on your computer in order to read it. For this you will need to use (again) the LIBNAME statement. The LIBNAME statement is used for both reading and writing SAS datasets. Now the library reference that was used to create the dataset does not matter (in fact, you may not even know this information). You do need to know where on your computer the file is located. From program 12 we know the dataset sescore is located under the ‘C:\SAS_Files folder. In program 13 we use the LIBNAME statement to assign a library reference to this folder. We will use the name class. Now we can start running SAS procedures immediately without a DATA step. We need to refer to the dataset on the PROC statement with the library reference, a period, then the dataset name. The PROC MEANS statement here tells SAS to go out and find a dataset called sescore in the directory referenced by class and run a PROC MEANS on this dataset. We then run a PROC CORR on the dataset. You way think that using library references are an odd way to tell SAS where the data set is located. Perhaps, that is true. However, what if the dataset you have been using gets moved to a different folder (perhaps by the data administrator). If that happens, you just need to change the LIBNAME statement and everything will work.


Download ppt "Working With Dates: Dates Come in Many Ways"

Similar presentations


Ads by Google