Post on 30-Mar-2018
Dates Saad Rais
Ministry of Health and Long-Term Care
Nov 20, 2015
SAS Dates are like sour grapes…
What do we use dates for?
Data query by period Measure duration
Time indicators Forecasting Modelling
Rates Time-trend
Merge by date Frequency tables
Before-after analysis Longitudinal data analysis
You will learn how to:
• Create a date variable
• Display a date in a certain way (formats)
• Read in a date from a text file
• Convert a string variable into a date variable
• Calculate duration between dates
• Parse dates into its components
Creating a date variable
• Date variables are numeric variables 0 = Jan 1 1960 1 = Jan 2 1960 -1 = Dec 31 1959
• What is the number for today’s date? – Use today () function %put %sysfunc(today ());
20411
• What is the number for my birthday (or any day)? – use MDY (month, day, year) function %put %sysfunc(mdy(2,28,1970));
3711
– use date in quotes and a ‘d’ %let sas_day = “19NOV2015"d;
%put %sysfunc(putn(&sas_day, 8.0));
20411
Displaying dates a certain way: formats
• Dates are stored as numeric values
0 = January 1, 1960
• Numeric values may be ‘externally’ represented by a format
• List of date formats can be found here: https://support.sas.com/documentation/cdl/en/etsug/60372/HTML/default/viewer.htm#etsug_intervals_sect010.htm
Some date formats
ID values Periodicity FORMAT Example
SAS date annual YEAR4. 1991
quarterly YYQC6. 1991:4
monthly MONYY7. OCT1991
weekly WEEKDATX23. Thursday, 17 Oct 1991
daily DATE9. 17OCT1991
SAS datetime hourly DATETIME10. 17OCT91:14
minutes DATETIME13. 17OCT91:14:45
seconds DATETIME16. 17OCT91:14:45:32
What SAS dates can look like
Sample anatomy of a date format
• DDMMYY = order of day month, year – Could be YYMMDD or something else
• x = separator (default = dash) – B = blank C = colon D = dash N = none P= period
S = slash
• w = character width including separator (default = 8)
DDMMYYxw.
Sample output of date formats
• Examples: %let d = 18788 /* = “10JUN2011"d*/;
%put %sysfunc(putn(&d,yymmdd.));
11-06-10
%put %sysfunc(putn(&d,yymmdds.));
11/06/10
%put %sysfunc(putn(&d,yymmdds10.));
2011/06/10
%put %sysfunc(putn(&d,ddmmyys10.));
10/06/2011
%put %sysfunc(putn(&d,yymmdd7.));
110610
Reading in dates from a flat file
• Step 1: Identify the date informat
Reading in dates from a flat file
• Step 2a: Decide on the format (point & click)
Reading in dates from a flat file
• Step 2b: Decide on the format (code)
Reading in dates from a flat file
• Step 3: Check that data was imported correctly
Converting string variable to date variable
Converting string variable to date variable
• Step 1: Know the string variable informat
• Step 2: Create a numeric variable out of the string variable using ‘input’ function and informat
• Step 3: Specify the date format for the numeric date variable
Converting string variable to date variable
data rpdb;
set rpdb.rpdb_personal_charcs
(obs = 10 keep = create_tmestmp);
create_tmestmp_new =
input(create_tmestmp, ymddttm20.);
create_tmestmp_new_formatted =
create_tmestmp_new;
format create_tmestmp_new_formatted
datetime19.;
run;
Converting string variable to date variable
Performing calculations with dates
• E.g., Age (integer or continuous)
• Methods:
– (Ref_date – DOB + 1) / 365.25
– YRDIF Function, using ‘AGE’ parameter value
– Formula that uses INTCK function (counting the number of completed months)
• For continuous age, add the proportion of the year completed (macro provided in appendix A)
Performing calculations with dates
%let start_dt = "29feb2000"d; /*DOB*/
%let end_dt = "28feb2050"d; /*50TH BDAY?*/
data age;
age1 = (&end_dt - &start_dt + 1)/365.25;
age2 = yrdif(&start_dt, &end_dt, 'AGE');
%age_continuous (agevar=age3, dob=&start_dt, eventdate=&end_dt);
age1_int = int(age1);
age2_int = int(age2);
age3_int = int(age3);
age4_int = int((intck('month',&start_dt,&end_dt)- (day(&end_dt)<day(&start_dt)))/12);
run;
Parsing dates into its components
• Datepart(datetime_var)
• Year(date_var)
• Qtr(date_var)
• Month(date_var)
• Day(date_var)
Parsing dates into its components
The End
For more information, contact:
Saad Rais Saad.rais@ontario.ca
Appendix A: Macro to calculate continuous age
%macro age_continuous (agevar=,dob=,eventdate=);
m_age_int = floor((intck('month',&dob.,&eventdate.)-
(day(&eventdate.)<day(&dob.)))/12);
m_prior_bday_correction = (month(&dob.)eq 2)*(day(&dob.)eq 29)*
(put(MDY(12,31,(year(&dob.)+m_age_int)),JULDAY.)eq "365");
m_prior_bday = MDY(month(&dob.+m_prior_bday_correction),
day (&dob.+m_prior_bday_correction),
year (&dob.)+m_age_int);
m_next_bday_correction = (month(&dob.)eq 2)*(day(&dob.)eq 29)*
(put(MDY(12,31,(year(&dob.)+m_age_int+1)),JULDAY.)eq "365");
m_next_bday = MDY(month(&dob.+m_next_bday_correction),
day (&dob.+m_next_bday_correction),
year (&dob.)+m_age_int+1);
&agevar. = m_age_int+((&eventdate.-m_prior_bday)/(m_next_bday-m_prior_bday));
drop m_age_int m_prior_bday_correction m_prior_bday m_next_bday_correction
m_next_bday;
%mend age_continuous;
Courtesy: http://www.wuss.org/proceedings09/09WUSSProceedings/papers/cod/COD-Adams.pdf