ClaireR
ClaireR

Reputation: 1

Converting dates from Stata to R

I am new to R and am working with a data set of 398 variables. There are many variables for different dates. The ages are missing for more than 400 of my subjects, but I have all the dates of birth and death so am hoping to calculate their ages. However, the first step is to get all the dates in the same format. I've been successful with several of them, but the date of death are some kind of Stata format. It says:

str(Data_10_8_17$dateofdeath)
 atomic [1:10381] 17431 16752 18753 19776 17774 ...
 - attr(*, "format.stata")= chr "%9.0g"

I've changed all the numbers FYI.

Upvotes: 0

Views: 1172

Answers (1)

Nick Cox
Nick Cox

Reputation: 37208

Only part of an answer but this is too long and won't look good as a comment.

Those data don't look that cryptic. If you study help datetime in Stata you learn that daily dates have their origin 0 = 1jan1960. On the guess that they are daily dates which have lost a date format, we can push it back again:

. mata : strofreal((17431, 16752, 18753, 19776, 17774), "%td")
               1           2           3           4           5
    +-------------------------------------------------------------+
  1 |  22sep2007   12nov2005   06may2011   22feb2014   30aug2008  |
    +-------------------------------------------------------------+

If those dates are acceptable, you now need someone competent in R to guide you on translation.

Note that the format %9.0g (the syntax is consciously reminiscent of that in C) is just a general numeric format and not exotic at all: it's the default numeric display format and does not bite for moderate integers. https://www.stata.com/help.cgi?format is accessible documentation on Stata display formats for all, including R users needing a translation.

Upvotes: 2

Related Questions