Announcement

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

  • Recode time variable in xtset data

    I want to be able to take the time variable in an xtset data set (or else create a new variable) and have it be coded 1, 2, ..., T. This is easy to do if, say, time is coded as year, e.g. if the coding is 1976, 1977, ..., 1984, all I have to do is subtract 1975 from each value. But, there are so many different ways of coding time that I want something that will always work. Maybe use the group function of egen, but it would be nice if it could somehow take into account uneven intervals, e.g. 1975, 1976, 1978, 1979.. i.e. I don't want 1978 coded as 3, I would want it coded as 4. (This would be a situation where no case has data for 1977).

    I am working on a routine where the help file explicitly states that time needs to be coded 1, 2, 3, ..., T. But, everybody keeps on ignoring the warning so I want the program just to be able to recode time for the user.
    -------------------------------------------
    Richard Williams, Notre Dame Dept of Sociology
    StataNow Version: 19.5 MP (2 processor)

    EMAIL: [email protected]
    WWW: https://academicweb.nd.edu/~rwilliam/

  • #2
    Richard, it seems to me that starting with the stored results from xtset (r(tmin), r(tmax), r(tdelta)) would lead to an approach to your problem. I don't think it's too much to ask the user to xtset the data before invoking your routine.

    Comment


    • #3
      I've been doing things like

      gen newtime = time - r(tmin) + 1

      That works ok for something like year = 1976, 1977,..., 1984, but there are so many ways to code time I am not sure what would work universally. I probably have to work tdelta into it.
      -------------------------------------------
      Richard Williams, Notre Dame Dept of Sociology
      StataNow Version: 19.5 MP (2 processor)

      EMAIL: [email protected]
      WWW: https://academicweb.nd.edu/~rwilliam/

      Comment


      • #4
        Something like the following is what I had in mind.
        Code:
        generate int newtime = 1 + (time - r(tmin))/r(tdelta)
        Rounding the result of the division might be a good idea.
        Last edited by William Lisowski; 05 Jul 2015, 11:07.

        Comment


        • #5
          Would this work?

          gen newtime = (time - r(tmin))/r(tdelta) + 1

          I think it would work if, say, year = 1976, 1978, 1980, ..., 1984, with delta = 2. But I don't know if that will cover all scenarios. When xtsetting the data, in this case the user would specify delta(2).
          -------------------------------------------
          Richard Williams, Notre Dame Dept of Sociology
          StataNow Version: 19.5 MP (2 processor)

          EMAIL: [email protected]
          WWW: https://academicweb.nd.edu/~rwilliam/

          Comment

          Working...
          X