1 / 26

Working With Dates: Dates Come in Many Ways

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. What do you want to do with dates?. Display them Compare two dates: find the number of days between 2 dates

csandra
Télécharger la présentation

Working With Dates: Dates Come in Many Ways

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

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

  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?

  3. DATA dates; INFILE DATALINES; INPUT @1 brthdate mmddyy10.; * Use informat; DATALINES; 03/03/1971 02/14/1956 01/01/1960 ; PROCPRINT; VAR brthdate; PROCPRINT; VAR brthdate; FORMAT brthdate mmddyy10.; ------------------------------------------------------ Obs brthdate 1 4079 2 -1417 3 0 Obs brthdate 1 03/03/1971 2 02/14/1956 3 01/01/1960 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;

  5. * Program 8 ; DATA age; INFILE‘C:\SAS_Files\tomhs.data' ; INPUT @14 randdate mmddyy10. @34 brthdate mmddyy10. ; agedays = randdate - brthdate ; ageyrs = (randdate - brthdate)/365.25; ageint = INT( (randdate - brthdate)/365.25); yrrand = YEAR(randdate);

  6. PROCPRINTDATA=age (obs=5); VAR brthdate randdate agedays ageyrs ageint ; TITLE'Printing Dates Without a Date Format'; RUN; PROCPRINTDATA=age (obs=5); VAR brthdate randdate agedays ageyrs ageint ; FORMAT brthdate mmddyy10. randdate mmddyy10.; TITLE'Printing Dates With a Date Format'; RUN;

  7. Printing Dates Without a Date Format Obs brthdate randdate agedays ageyrs ageyrsX ageint 1 -8589 10175 18764 51.3730 51.3739 51 2 -6880 10239 17119 46.8693 46.8711 46 3 -12572 10002 22574 61.8042 61.8055 61 4 -9592 10175 19767 54.1191 54.1205 54 5 -12996 10280 23276 63.7262 63.7268 63 All before 1960

  8. Printing Dates With a Date Format Obs brthdate randdate 1 06/26/1936 11/10/1987 2 03/01/1941 01/13/1988 3 07/31/1925 05/21/1987 4 09/27/1933 11/10/1987 5 06/02/1924 02/23/1988

  9. PROCFREQDATA=age; TABLES yrrand ; TITLE'Frequency Distribution of Year Randomized'; RUN; The FREQ Procedure Cumulative Cumulative yrrand Frequency Percent Frequency Percent ----------------------------------------------------------- 1986 9 9.00 9 9.00 1987 65 65.00 74 74.00 1988 26 26.00 100 100.00

  10. Topics • Reading SAS datasets • Sub-setting SAS datasets • Merging SAS datasets

  11. Working With SAS Data Sets • Reading SAS dataset • SET Statement • Merging SAS datasets • MERGE Statement Done within a DATA step

  12. SET STATEMENT • 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; This creates a new data set called new that has the variables in varlist and selected observations from old.

  13. PROGRAM 9 Making SAS Datasets from Other SAS Datasets; DATA tdata; INFILE‘C:\SAS_Files\tomhs.data' ; INPUT @ 1 ptid $10. @ 12 clinic $1. @ 25 group 1. @ 30 sex 1. @ 123 sbp12 3. @ 14 randdate $10. ; RUN; * Making a new dataset containing only men; DATA men; SET tdata; * reads the existing dataset; WHERE sex = 1; This does the selection; ifgroup in(1,2,3,4,5)thenactive = 1;else ifgroup in(6)thenactive = 2; KEEP ptid clinic group sbp12 randdate active; RUN;

  14. PROGRAM 10 - Merging SAS Datasets DATA clinic; INFILE DATALINES; INPUT id $ sbp ; DATALINES; C03615 115 B00979 107 B00644 138 D01348 142 A01088 117 B01408 121 B00025 130 B00714 144 A01166 113 … more data ; DATA lab; INFILE DATALINES; INPUT id $ glucose; DATALINES; C03615 102 B00644 089 D01348 111 A01088 093 B01408 094 B00025 077 B00714 100 A01166 113 D00942 103 … more data ;

  15. * 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!

  16. Merged Dataset Obs id sbp glucose 1 A00869 110 99 2 A01088 117 93 3 A01166 113 113 4 B00025 130 77 5 B00644 138 89 6 B00714 144 100 7 B00867 114 98 8 B00979 107 . 18 D00942 . 103 20 D01809 129 .

  17. 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!

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

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

  20. Creating a Permanent Dataset LIBNAMEmylib‘C:\My SAS Datasets’; LIBNAME – assigns a directory (folder) reference name. In this example the directory ‘C:\My SAS Datasets’ is assigned a reference name of mylib. DATAmylib.sescore; Tells SAS to create a dataset called sescore in the directory referenced by mylib, which is ‘C:\My SAS Datasets’.

  21. PROGRAM 11 LIBNAMEmylib‘C:\SAS_Files'; DATAmylib.sescore; INFILE‘C:\SAS_Files\tomhs.data'LRECL =400; INPUT @ 1 ptid $10. @ 12 clinic $1. @ 14 randdate mmddyy10. @ 25 group 1. @ 49 educ 1. @ 85 wtbl 5.1 @ 97 wt12 5.1 @115 sbpbl 3.0 @123 sbp12 3.0 @236 (sebl_1-sebl_20) (1. +1) @276 (se12_1-se12_20) (1. +1) ;

  22. wtd12 = wt12 - wtbl; sbpd12 = sbp12 - sbpbl; sescrbl = MEAN (OF sebl_1 - sebl_20) ; sescr12 = MEAN (OF se12_1 - se12_20) ; sescrd12 = sescr12 - sescrbl ; LABEL educ = 'Highest Education Level'; LABEL wt12 = 'Weight (lbs) at 12 Months'; LABEL wtbl = 'Weight (lbs) at Baseline'; LABEL wtd12 = 'Weight Change at Baseline'; LABEL sbpbl = 'Systolic BP (mmHg) at Baseline'; LABEL sbp12 = 'Systolic BP (mmHg) at 12 Months'; LABEL sbpd12 = 'Systolic BP Change at 12 Months'; LABEL group = 'Treatment Group (1-6)'; LABEL sescrbl = 'Side Effect at Baseline'; LABEL sescr12 = 'Side Effect at 12 Months'; LABEL sescrd12 = 'Side Effect Change Score'; FORMAT randdate mmddyy10. ; DROP sebl_1-sebl_20 se12_1-se12_20 ;

  23. 60 LIBNAME mylib 'C:\SAS_Files'; NOTE: Libref MYLIB was successfully assigned as follows: Engine: V9 Physical Name: C:\SAS_Files DATA mylib.sescore; NOTE: The infile 'C:\SAS_Files\tomhs.data' is: File Name=C:\SAS_Files\tomhs.data, RECFM=V,LRECL=400 NOTE: 100 records were read from the infile 'C:\SAS_Files\tomhs.data'. NOTE: The data set MYLIB.SESCORE has 100 observations and 14 variables.

  24. 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. LIBNAMEmylib‘C:\SAS_Files'; DATA sescore; …. RUN; PROC COPY IN=work OUT=mylib; SELECT sescore; RUN;

  25. Reading Permanent SAS Dataset LIBNAMEclass‘C:\SAS_Files' ; * Tells SAS where to find the SAS dataset; PROCMEANSDATA=class.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

  26. *---------------------------------------------------------------**---------------------------------------------------------------* Often you will read the permanent SAS dataset in a DATA step to modify or add variables. Usually these will be put on a new work SAS dataset. *---------------------------------------------------------------*; LIBNAME class 'C:\SAS_Files' DATA rxdata; SET class.sescore; if group in(1,2,3,4,5) then rx = 1; else rx = 2; RUN; PROCMEANSDATA=rxdata NMEANMAXDEC=2FW=7; CLASS group; VAR sbpd12 wtd12 sescrd12; TITLE'Change in SBP, Weight, and Side Effect Score by Treatment'; RUN;

More Related