![]() However, if you are on a 12-hour clock without using correct notation, Stata will interpret it as the 24-hour clock (e.g., 2:34 will be read as 02:34). ![]() Note: Stata is smart enough to handle both inconsistent spacing and a twelve-hour clock with 12-hour notation (12:36pm). The format is "MDYhm" and the full command is generate double datetime = clock(datetimestr,"MDYhm") The original variable is "datetimestr" the target variable will just be called datetime. Since there is a time component, clock is used. While this might look more daunting, it is really just a combination of the previous two processes. The last example is an observation set combining date and time on a 12 hour clock with inconsistent spacing. The final command is generate double time= clock(timestr, "hm") Since I'm dealing with a time, I use clock not date as the function. I see from the table above that h=hour and m=minute so my format is "hm". The original variable is "timestr" and the new variable will just be "time". The next example is just a time, in this case using 24 hour time without seconds. After another example or two, we will change those number strings into something we can read too. If you were to browse your data now you would see the old string date variable, the not yet used other two string variable examples, and a new variable called "date" with long number strings. Thus the command is generate double date = clock (datestr, "DMY" Finally, 10jan2008 is the day the month and then the year, represented by DMY according to the table above. The original variable in this dataset is named "datestr." I'll simply call the target variable "date". Since this first column has no time component, date will be used. The first example in our sample dataset is date with the day then month then year (e.g., 10jan2008). The table below from Stata outlines what arguments can go in the section of the command and what they correspond to. The generic command takes the following form generate double = clock(, "" date takes the same basic syntax. The clock & date functions can look at strings like 10jan2009 or 14:35:00 or even 12/3/02 2:34pm and convert them into an all-number format. You should already be familiar with the generate command, so all that remains is to get to know the clock & date functiosn. Using the generate command and various clock functions, most any date or time format can be changed into a numeric version of itself. Once you have the date/time information in Stata in a string format, the first step is to generate a new variable with the data converted to numbers. Stata offers a variety of functions for converting the string dates to the above-listed formats. and %tg allows you to set your own origin time and increment. %tw uses the same origin but counts in weeks from 01jan1960, %tm is the equivalent in months, %tq in financial quarters, and %th in half years. %td counts from the same origin but in days since 01jan1960. %tC does the same thing except with leap milliseconds. %tc uses 01jan1960 base and counts in milliseconds, without counting leap seconds. Stata offers nine time formats which all count in slightly different ways. By putting everything into milliseconds, Stata can meaningfully subtract or add dates and times to calculate elapsed time between events. For times, Stata uses 00:00:00:00 as the origin of the day and once more proceeds to calculate milliseconds since then when given a time such as 1:26 p.m. Because computers thrive on complex calculations, Stata will do this math for you. This system even accounts for leap seconds. Thus, to Stata and other programs, every date following can be represented in terms of how far away from this origin date it is. Basically, computer time is zeroed to 01jan1960 00:00:00:00 o'clock. Moving from dates to Stata dates is a three step process.īefore the process though, a bit of the theory behind it. Therefore, Stata offers tools to turn these date strings into values that, while still displaying sensical data to humans, are encoded in a numeric format that Stata likes. While useful for a human eye, a computer can't glean much from data in this format. Often data is imported into Stata with string format dates such as or 9oct1986.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |