I have date and time for admission to hospital and date of discharge. Some dates are apparently registered with error on the year. I want to correct these. I create variables for the year, month and day, I correct the year variable and then transform back. Some of these tranfsforms does not work
Here is my data
lengtofstay is generated with gen lengthofstay=datumut-dofc(ankomstdatumtid)
Year/mån/dag-utkoll was created by:
gen yearutkoll=year(datumutkoll)
gen månadutkoll=month(datumutkoll)
gen dagutkoll=day(datumutkoll)
The yearutkoll is wrong with 1 and 2 years, as supposed by the lengthofstay-variable.
So I correct the year-variable with
replace yearutkoll=yearutkoll-1 if lengthofstay>364&lengthofstay<730
replace yearutkoll=yearutkoll-2 if lengthofstay>729&lengthofstay<.
I get this
I backtransform the date
. replace datumutkoll=date(string(yearutkoll)+string(månadut koll)+string(dagutkoll),"YMD") if lengthofstay>360&lengthofstay<.
(4 real changes made, 2 to missing)
Why the two missing?
Here is my data
lengtofstay is generated with gen lengthofstay=datumut-dofc(ankomstdatumtid)
ankomstdatumtid | datumutkoll | yearutkoll | månadutkoll | dagutkoll | lengthofstay |
2010-12-14 12:12 | 14dec2011 | 2011 | 12 | 14 | 365 |
2010-01-11 08:35 | 13jan2011 | 2011 | 1 | 13 | 367 |
2011-06-13 08:15 | 13jun2013 | 2013 | 6 | 13 | 731 |
2011-02-19 12:51 | 21feb2013 | 2013 | 2 | 21 | 733 |
Year/mån/dag-utkoll was created by:
gen yearutkoll=year(datumutkoll)
gen månadutkoll=month(datumutkoll)
gen dagutkoll=day(datumutkoll)
The yearutkoll is wrong with 1 and 2 years, as supposed by the lengthofstay-variable.
So I correct the year-variable with
replace yearutkoll=yearutkoll-1 if lengthofstay>364&lengthofstay<730
replace yearutkoll=yearutkoll-2 if lengthofstay>729&lengthofstay<.
I get this
ankomstdatumtid | datumutkoll | yearutkoll | månadutkoll | dagutkoll | lengthofstay |
2010-12-14 12:12 | 14dec2011 | 2010 | 12 | 14 | 365 |
2010-01-11 08:35 | 13jan2011 | 2010 | 1 | 13 | 367 |
2011-06-13 08:15 | 13jun2013 | 2011 | 6 | 13 | 731 |
2011-02-19 12:51 | 21feb2013 | 2011 | 2 | 21 | 733 |
I backtransform the date
. replace datumutkoll=date(string(yearutkoll)+string(månadut koll)+string(dagutkoll),"YMD") if lengthofstay>360&lengthofstay<.
(4 real changes made, 2 to missing)
ankomstdatumtid | datumutkoll | yearutkoll | månadutkoll | dagutkoll | lengthofstay |
2010-12-14 12:12 | 14dec2010 | 2010 | 12 | 14 | 365 |
2010-01-11 08:35 | 13jan2010 | 2010 | 1 | 13 | 367 |
2011-06-13 08:15 | 2011 | 6 | 13 | 731 | |
2011-02-19 12:51 | 2011 | 2 | 21 | 733 |
Why the two missing?
Comment