datetime - Convert date variable to numeric -
feels obvious question, stata hasn't yielded answers. stata users interested in converting non-date variable date variable, want opposite.
i have date variable date
, type long, format %tdccyynn
. i'm trying append dataset in same variable date
type long , format %12.0g
. accurately this, need convert date
in first dataset %tdccyynn
%12.0g
. when format %12.0g date
, date
values change incorrect ones.
let's say, in first dataset, have date=201204
. still want read 201204
, %12.0g
variable. there way this?
i +1 comments above nick , william , suggest read help datetime
. have been using stata few years , still visit file. stata's date/time functionality fantastic , benefit learning earlier rather later.
i convert other data stata date format. really. if need convert %td
date "integer yyyynn" date, pass through temporary file. if write %td
date plain text, keep displayed format , can read integer yyyynn date.
// data matches decsription clear set obs 1 generate date = date("20120401", "ymd") format date %tdccyynn list // write tempfile plain text tempfile plaintext outsheet using "`plaintext'" // read dates integers preserve tempfile statadata insheet using "`plaintext'", clear rename date dateinteger save "`statadata'" restore // merge original data merge 1:1 _n using "`statadata'" list describe
this yields following.
. list +---------------------------------+ | date datein~r _merge | |---------------------------------| 1. | 201204 201204 matched (3) | +---------------------------------+ . describe contains data obs: 1 vars: 3 size: 7 ----------------------------------------------------------------------------------------------------- storage display value variable name type format label variable label ----------------------------------------------------------------------------------------------------- date int %tdccyynn dateinteger long %12.0g _merge byte %23.0g _merge ----------------------------------------------------------------------------------------------------- sorted by: note: dataset has changed since last saved.
but suggest take advantage of stata's date/time functionality.
Comments
Post a Comment