Announcement

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

  • Generating a dummy variable if a variable increases over time

    Dear all,

    I am trying to create a dummy variable that indicates whether a variable increases over time or not. I have a variable 'perrank' that indicates the place of an individual in a wealth distribution. For example, ID=101 in year = 2015 with perrank = 86, indicates that an individual in 2015 belonged to the top 14 percent. Now I want to calculate whether that person's place in the wealth distriution increases over time. I want to create a dummy that says 1 if increase over time and 0 if not. So if ID =101 in year = 2016 with perrank = 87, the dummy should have a 1, because the perrank variable increased. Preferably, I would be able to change the degree of change. For example, whether the increase is one percentile or two percentiles.

    I have seen this forum (https://www.statalist.org/forums/for...r-time-periods) but I could not write the code to solve my problem. It is probably something like ' bysort id (year), sort: gen changed = .............'. I have panel data. The ultimate goal is to calculate the probability of rising in the wealth distribution after receiving inheritance.

    Thank you in advance.

    Best,

    Timo

  • #2

    Code:
    bysort id (year) : gen decrease = sum(cond(_n == 1, 0, perrank < perrank[_n-1]))
    by id : replace decrease = decrease[_N]
    will count decreases. If staying the same is what you want to record too change < to <=

    Then decrease equal to 0 means always increasing or stable.

    Warning: not tested (you didn't give example data). Missings may need more care than this.

    Comment


    • #3
      Thank you for the response Nick. The code is, however, not giving the results I would like. Using 'tab' it shows that it adds the values:

      . tab decrease

      decrease | Freq. Percent Cum.
      ------------+-----------------------------------
      0 | 10,694 15.54 15.54
      1 | 12,905 18.76 34.30
      2 | 10,655 15.49 49.78
      3 | 6,639 9.65 59.43
      4 | 4,279 6.22 65.65
      5 | 3,304 4.80 70.45
      6 | 2,896 4.21 74.66
      7 | 2,787 4.05 78.71
      8 | 2,640 3.84 82.55
      9 | 2,856 4.15 86.70
      10 | 2,367 3.44 90.14
      11 | 2,070 3.01 93.15
      12 | 1,937 2.82 95.96
      13 | 1,041 1.51 97.48
      14 | 847 1.23 98.71
      15 | 537 0.78 99.49
      16 | 215 0.31 99.80
      17 | 82 0.12 99.92
      18 | 56 0.08 100.00
      ------------+-----------------------------------
      Total | 68,807 100.00

      Data browsing shows that the 'dummy' is already giving numbers as 2 or more in the first year. Could you take a second look at the code? I want to create a dummy that says 1 if percentile ranks over time increases with 1% for an individual. Thus, if perrank increases from 87 to 89 from the year 2010 to 2011, the dummy should say 1. If from 2011 to 2012 it increases again from 89 to 91 it should say another time 1.

      Here is example data. It is a unbalanced panel. I have added a column 'increase' to show what I mean. There is no missingness in the data. One problem I now recognize is that I am looking at changes over time. This has problems for the first year of each individual: should it be a zero or a '.'? Any advice?

      id year twealth_corrected perrank increase
      101 1993 0 0.230083 ./0
      101 1994 0 0.272502 1
      101 1995 0 0.213183 0
      102 1993 68849.09 0.76121 ./0
      102 1994 -15209 0.01407 0
      102 1995 52459.7 0.691973 1
      201 1993 117970.1 0.861559 ./0
      201 1994 58939.64 0.865301 1
      202 1993 4718.422 0.447323 ./0
      202 1994 4698.9 0.512932 1
      301 1993 -7513.7 0.019373 ./0
      301 1994 -3197.98 0.038899 1
      401 1993 106315.5 0.84458 ./0
      401 1994 23417.32 0.742189 0
      401 1995 165937 0.912987 1
      401 1996 95108 0.772455 0
      402 1993 0 0.230083 ./0
      402 1994 908 0.370991 0
      402 1995 908 0.30955 0
      402 1996 1035.12 0.291667 0
      501 1993 0 0.230083 ./0
      501 1994 0 0.272502 1
      501 1995 -22273.2 0.007179 0

      Thank you in advance.

      Comment


      • #4
        I did not receive a response yet, while the response is often fast. Is there anything unclear? I would love to clear things up

        Comment

        Working...
        X