Announcement

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

  • Panel Data tracking a variable increase from zero

    Dear Statalist,


    I have the following panel data list and I need to generate Var 2 based on
    Var 1 for IDs that increase their value from 0 in the previous year.
    Var 2 takes value 1 only if an ID increases its value from zero previous year to 1 ( or a higher positive number) in the current year and zero otherwise.

    Year ID Var1 Var2
    1990 1 0 0
    1990 2 0 0
    1990 3 2 0
    1990 4 2 0
    1990 5 1 0
    1990 6 . 0
    1991 1 1 1
    1991 2 0 0
    1991 3 2 0
    1991 4 3 0
    1991 5 1 0
    1991 6 0 0
    1992 1 2 0
    1992 2 2 1
    1992 3 4 0
    1992 4 2 0
    1992 5 1 0
    1992 6 1 1
    Appreciate your help.
    Last edited by krishantha Ainsworth; 07 Apr 2022, 09:04.

  • #2
    Code:
    xtset id year
    gen byte var2 = (var1 >= 1 & L1.var1 == 0)

    Comment


    • #3



      Thank you for your answer. However one issue is that say for a particular ID
      when it is initially not zero but then later changes to zero and then increases from there to a positive value, Var2
      takes 1 (see the ID 3 example below). Actually what I need is for a particular ID to recognize only when they
      increase from zero and all their previous values up to that point is zero (see ID 4 example).
      ID Var 1 Var 2
      1990 3 4 0
      1991 3 5 0
      1992 3 0 0
      1993 3 0 0
      1994 3 1 1
      1995 3 4 0
      ID Var 1 Var 2
      1990 4 0 0
      1991 4 0 0
      1992 4 0 0
      1993 4 0 0
      1994 4 1 1
      1995 4 2 0
      Last edited by krishantha Ainsworth; 07 Apr 2022, 23:39.

      Comment


      • #4
        So for that
        Code:
        * Example generated by -dataex-. For more info, type help dataex
        clear
        input int year byte(id var1)
        1990 3 4
        1991 3 5
        1992 3 0
        1993 3 0
        1994 3 1
        1995 3 4
        1990 4 0
        1991 4 0
        1992 4 0
        1993 4 0
        1994 4 1
        1995 4 2
        end
        
        by id (year), sort: gen non_zeroes = sum(var1 != 0)
        by id (year): gen byte var2 = (var1 > 0 & non_zeroes == 1 & non_zeroes[_n-1] == 0)
        In the future, when showing data examples, please use the -dataex- command to do so, as I have here. If you are running version 17, 16 or a fully updated version 15.1 or 14.2, -dataex- is already part of your official Stata installation. If not, run -ssc install dataex- to get it. Either way, run -help dataex- to read the simple instructions for using it. -dataex- will save you time; it is easier and quicker than typing out tables. It includes complete information about aspects of the data that are often critical to answering your question but cannot be seen from tabular displays or screenshots. It also makes it possible for those who want to help you to create a faithful representation of your example to try out their code, which in turn makes it more likely that their answer will actually work in your data.


        Comment


        • #5
          HiClyde,

          Would you mind mentioning how to modify this code to indicate var2 == 1 only if there are a minimum number of zeroes (or missing values) precede the increase. For example say I need minimum of two zeros (or missing values) precede to var2 == 1.

          Comment


          • #6
            HiClyde,

            I mean if a particular firm goes from 0 to some positive value in the next year we ignore them. Instead we only consider firms those increase value in the next year after minimum 2 consecutive zeros (i.e. 0, 0, positive value ; 0, 0, 0 , positive value etc.) . How may I modify the above code

            Code:
            * Example generated by -dataex-. To install: ssc install dataex
            clear
            input int year byte(id var1)
            1990 3 0
            1990 4 0
            1991 3 5
            1991 4 0
            1992 4 2
            1992 3 1
            1993 3 5
            1993 4 2
            1994 3 1
            1994 4 1
            1995 4 2
            1995 3 4
            end
            Here for example ID 3 increases its value in the very next year and ID 4 increases after two consecutive zeros. I want var 2 to take one only for ID 4 in 1992 and not for ID 3 in 1991 as ID 3 does not have two minimum preceding zeroes.

            Comment


            • #7
              Code:
              * Example generated by -dataex-. To install: ssc install dataex
              clear
              input int year byte(id var1)
              1990 3 0
              1990 4 0
              1991 3 5
              1991 4 0
              1992 4 2
              1992 3 1
              1993 3 5
              1993 4 2
              1994 3 1
              1994 4 1
              1995 4 2
              1995 3 4
              end 
              
              bysort id (year): gen var2 = var1 > 0 & var1[_n-2] == 0 & var1[_n-1] == 0 
              
              list, sepby(id)
              
                   +-------------------------+
                   | year   id   var1   var2 |
                   |-------------------------|
                1. | 1990    3      0      0 |
                2. | 1991    3      5      0 |
                3. | 1992    3      1      0 |
                4. | 1993    3      5      0 |
                5. | 1994    3      1      0 |
                6. | 1995    3      4      0 |
                   |-------------------------|
                7. | 1990    4      0      0 |
                8. | 1991    4      0      0 |
                9. | 1992    4      2      1 |
               10. | 1993    4      2      0 |
               11. | 1994    4      1      0 |
               12. | 1995    4      2      0 |
                   +-------------------------+

              Comment

              Working...
              X