![]() Thus at best Stata can only map a monthly date back to the first day of that month, and at worst a monthly date (here 660) could not be understood as anything you want. I can't follow why you thought that conversion to a monthly date or use of a monthly date display format was needed or helpful, as at best you'd lose the information on day of the month. The advantage of tostring would only be if you had several such variables you wished to convert at once, as tostring would loop over such variables for you. You could have run tostring with a format argument, which would have been equivalent to the code above. Will just put integers like 20118 in string form, so "20118", but there is no way that Stata can understand that alone to be a daily date. Turning to your code, tostring date, gen(dt) The principles needed here are better illustrated using the underlying function strofreal(). I have not used tostring here but as its original author I have no bias against it. gen WANTED = strofreal(date, "%tdCCYYNNDD") The default display format will work fine. Want such dates as variables held as strings? This is the previous solution, but leave off the real(). gen long also = 10000 * year(date) + 100 * month(date) + day(date) You need to be explicit about a suitable storage type and (as just mentioned) need to set an appropriate format, but it can be got to work. gen wanted = real(strofreal(date, "%tdCCYYNNDD"))Ī display format such as %8.0f is needed to see such values directly.Īnother method is to generate a large integer directly. Number of observations (_N) was 0, now 1. ![]() Want such dates as variables held as integers? If you want the dates to be held as integers like 20150130 then you could convert it to string using the display format above, and then to a real value. That instructs Stata to change the display format, but the numbers stored remain precisely as they were. Want to change display format? If you wish merely to see your dates in a display format exemplified by 20150130 then consulting help datetime display formats shows that the display format is as tested here with display, which can be abbreviated all the way down to di. Note that finding out what Stata is doing and thinking can be illuminated by running display with simple, single examples. You would need (e.g.) to run describe date to find out which type is being used in your case, but nothing to come in this answer depends on knowing that type. All would work, although int is least demanding of memory. display mdy(1, 30, 2015)Īnd a glance at help data types shows that your variable date could be stored as an int, float, long or double. There could well be yet other meanings.Ī date displayed as 30jan2015 is stored as an integer, namely. png or jpg or whatever) data layout (as in wide or long layout, structure or format) variable or storage type and (here) display format. Confusions about conversion often seem to hinge on a misunderstanding about what format means, as format is an overloaded word in computing, referring variously to file format (as in graphics file format. The display format is, and is only, an instruction to Stata on how to display such integers. If a date variable has a display format %td it must be numeric and stored as some kind of integer.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |