07.02.2014 Views

Introduction to Stata 8 - (GRIPS

Introduction to Stata 8 - (GRIPS

Introduction to Stata 8 - (GRIPS

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

15.3. Dates. Danish CPR numbers<br />

Date variables [U] 15.5.2<br />

Dates are numeric variables; the internal value is the number of days since 1 Jan 1960; dates<br />

before that are negative.<br />

Date formats [U] 15.5.3; [U] 27.2.3<br />

Format specifications start with %d. Specifying %d only is equivalent <strong>to</strong> %dDlCY<br />

displaying a date as 28sep2000. To display this date as 28.09.2000 specify the format<br />

%dD.N.CY (D for day, N for numeric month, C for century, Y for two-digit year). Example:<br />

format bdate %dD.N.CY<br />

Reading date variables [U] 27.2.1<br />

A date may be input as three variables: day, month, year and next transformed <strong>to</strong> a date<br />

variable:<br />

infix bd 1-2 bm 3-4 by 5-8 using c:\dokumenter\p1\datefile.txt<br />

generate bdate = mdy(bm,bd,by) // sequence must be m d y<br />

format bdate %dD.N.CY<br />

Another option is <strong>to</strong> enter the date as a string (sbdate) and translate it <strong>to</strong> a date variable:<br />

infix str10 sbdate 1-10 using c:\dokumenter\p1\datefile.txt<br />

generate bdate = date(sbdate,"dmy") // "dmy" defines sequence<br />

format bdate %dD.N.CY<br />

The date function 'understands' most input formats: 17jan2001, 17/1/2001,<br />

17.1.2001, 17 01 2001, but not 17012001. However <strong>to</strong>date, a user-written function,<br />

handles this situation; find and download it by: findit <strong>to</strong>date.<br />

In general: enter 4-digit years <strong>to</strong> avoid ambiguity on the century.<br />

Calculations with dates [U] 27.2.4<br />

To express the length of a time interval in years you must:<br />

generate opage = (opdate-bdate)/365.25<br />

You may extract day, month and year from a date variable (bdate):<br />

generate bday = day(bdate)<br />

gen bmonth = month(bdate)<br />

gen byear = year(bdate)<br />

On Danish CPR numbers: extracting key information<br />

Sometimes you get date information as a CPR number in an ASCII file. You can read the the<br />

CPR number as one string variable and the date information from the same columns:<br />

infix str10 cprstr 1-10 bday 1-2 bmon 3-4 byear 5-6 ///<br />

control 7-10 using c:\dokumenter\p1\datefile.txt<br />

generate bdate = mdy(bmon,bday,byear)<br />

57

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!