Announcement

Collapse
No announcement yet.
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • Recoding Missings in Elapsed Time Variable to 999

    Hi everybody,

    I am having a small coding problem that I cannot figure out.
    I have elapsed two variables into one, in order to display them in a time format.
    Now, I would like to recode some initially missing values to 999, to indicate that they are missing due to the questionaire's skip pattern.
    Naturally, since I have used a format command, Stata turns the 999 into a time format as well, though: 999 is displayed as 00:00.
    Is there any way in which the variable is generally being displayed in the HH:MM time format, but only in the case of 999 it is being displayed as it is?
    My code is the following:

    gen interviewtime = hms(A1A, A1B, 0)
    format interviewtime %tcHH:MM
    replace interviewtime=999 if Z1==0

    Any help would be very much appreciated!
    Best,
    Maja

  • #2
    Is there any way in which the variable is generally being displayed in the HH:MM time format, but only in the case of 999 it is being displayed as it is?
    Not that I know of.

    Now, I would like to recode some initially missing values to 999, to indicate that they are missing due to the questionaire's skip pattern.
    This is not a good idea. While some other statistical packages are designed to work with specified numbers like 999 to signal missing values, Stata is not. If you do this, you will have no end of errors and complications when you start to actually do calculations with this variable. What you should do is use one of Stata's extended missing values. See -help missing- for details and all the options. You can pick any one of them, let's say .s to represent missing due to skip pattern. You can even label the variable that way so that when you list or browse your data it shows up that way.

    Code:
    gen long interviewtime = hms(A1A, A1B, 0)
    format interviewtime %tcHH:MM
    replace interviewtime=.s if Z1==0
    label define interviewtime .s "Missing--skip pattern"
    label values interviewtime interviewtime
    That way, Stata will appropriately omit these from any calculations, but you can also see them displayed as "Missing--skip pattern".

    Added: Notice that I have modified your -gen- statement to make interviewtime a long variable, rather than the default float. This is unrelated to your question, but it is critical for dealing with times in Stata. The default float storage type does not have sufficient precision to store the times calculated by hms(); the numbers corresponding to times approaching midnight are too large to fit without losing some digits. Without specifying long you will find that the values of interviewtime are not accurate.
    Last edited by Clyde Schechter; 08 Jan 2017, 09:57.

    Comment


    • #3
      Hi Clyde,

      wow, this is the most perfect answer I could have hoped for!

      Thanks for your effort!
      Best,
      Maja

      Comment

      Working...
      X