Announcement

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

  • How to generate a new variable as a non-algebraic combination of values

    Hi, I have the following data and v6 has years and v7 has months.
    I want to overwrite v6 whose value should be like 6101, meaning year 1961 and January, 7703, meaning 1977 March, and so on.
    Could you help?


    Code:
    * Example generated by -dataex-. To install: ssc install dataex
    clear
    input float v6 int v7
       .  .
       .  .
       .  .
    1961  1
    1977  3
    1999  1
       .  .
       .  .
       .  .
    1983  5
    2000  3
    2000  6
    2001  4
       .  .
    1953  9
    1956 11
    1957  6
    1962  5
    1964  7
    1966  6
    1969  5
    1971  6
    1973  4
    1973  5
    1981  4
    1982  6
    1983 12
    1986  1
    1988  1
    1988  8
    1989  1
    1989 12
    1991  6
    1991  6
    1992  6
    1993 10
    1993 12
    1993  6
    1993  8
    1994 10
    1995 10
    1995  3
    1995  6
    1995  8
    1996 12
    1996  4
    1996  6
    1997 10
    1999  1
    1999  1
    1999 10
    1999  3
    1999  4
    2000  1
    2000 10
    2000 10
    2000 12
    2000  4
    2001  4
    2001  5
    2001  7
    2002  1
    2002 12
    2002  3
    2002  5
    2002  8
       .  .
       .  .
       .  .
       .  .
       .  .
    1976  6
    1986 10
    1989  4
       .  .
       .  .
    1926 10
    1984  3
    1992 11
    1992  5
    1994  8
    1997  8
    2001  2
       .  .
       .  .
       .  .
       .  .
       .  .
       .  .
       .  .
       .  .
       .  .
       .  .
       .  .
       .  .
    1952 11
    1953  5
    1972  4
    1973 10
    1974  4
    end

  • #2
    Code:
    gen yy = mod(v6, 100)
    egen wanted = concat(yy v7), format(%02.0f)
    will do this.

    All of that said, unless you have to do this so you can link this data set with some other data set that has coded its monthly dates in this way, this is a terrible idea. You cannot do arithmetic with this kind of representation of dates. For example, 7401 - 7312 is 89, even though Jan 74 is just one month after Dec 73. If you are going to do any calculations with dates, you must use Stata internal format date variables:

    Code:
    gen mdate = ym(v6, v7)
    format mdate %tm

    Comment


    • #3
      Clyde Schechter Thank you so much!

      Comment

      Working...
      X