Announcement

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

  • Creating dummy based on variable value in subset year

    Hi all,

    If I have data of 100 firms for 10 years with three variables; and I want to make a dummy variable equal to 1 for the firm if the value of variable x1 in year1 or year2 or year3 not equal to zero; how do I do it?

    Probably I need something like: by id, gen boom=1 if x12000 !=0 | x12001 !=0 | x12002 !=0, but obviously there is no variable x!2000 etc. Thank you for your help.


    Abdan

  • #2
    I'm guessing here as you don't give a data example, but maybe:
    Code:
    bysort id (year): gen wanted = x[1]!=0 | x[2]!=0 | x[3]!=0

    Comment


    • #3
      Thank you for your answer. The following is the sample data. The id is psid and one of the variable is ekspor. I want to make a dummy variable called preboom=1 if the value of ekspor equal to 2 in any of 2000-2002. I tried bysort psid (year): gen preboom = 1 if ekspor[1]=2 | ekspor[2]=2 | ekspor[3]=2; but it does not work. Grateful for your help.






      Code:
      * Example generated by -dataex-. To install: ssc install dataex
      clear
      input double psid int year byte ekspor
      1752 2000 2
      1752 2001 .
      1752 2002 .
      1752 2003 .
      1752 2004 .
      1752 2005 .
      1752 2006 .
      1752 2007 .
      1752 2008 .
      1752 2009 .
      1752 2010 .
      1752 2011 .
      1752 2012 .
      1752 2013 .
      1752 2014 .
      1758 2000 2
      1758 2001 .
      1758 2002 .
      1758 2003 .
      1758 2004 .
      end
      Last edited by Abdan Syakura; 01 Dec 2019, 08:08.

      Comment


      • #4
        The problem with your code is that you use = instead of ==
        Code:
        bysort psid (year): gen preboom = 1 if ekspor[1]==2 | ekspor[2]==2 | ekspor[3]==2
        This should work if 2000 is always the first year and you don't have missing years.

        Edit: Another possibility is:
        Code:
        bysort psid (year): gen preboom = ekspor[1]==2 | ekspor[2]==2 | ekspor[3]==2
        This creates 0's instead of missing values when the conditions are not met.
        Last edited by Wouter Wakker; 01 Dec 2019, 08:27.

        Comment


        • #5
          Thank you Wouter. I owe you a lot.

          Comment


          • #6
            Another approach that works regardless of what years are included for each psid. Not necessary for the problem as stated, but it might be handy for a later reader whose data is not as well balanced as in the current case.
            Code:
            bysort psid (year): egen preboom = max( inrange(year,2000,2002) & ekspor==2 )
            Here's how it works on test data that exercises various possibilities.
            Code:
               +--------------------------------+
              | psid   year   ekspor   preboom |
              |--------------------------------|
              | 1752   2000        2         1 |
              | 1752   2001        .         1 |
              | 1752   2002        .         1 |
              | 1752   2003        .         1 |
              | 1752   2004        .         1 |
              |--------------------------------|
              | 9996   1999        2         0 |
              | 9996   2000        .         0 |
              | 9996   2001        .         0 |
              | 9996   2002        .         0 |
              | 9996   2003        .         0 |
              |--------------------------------|
              | 9997   2000        .         0 |
              | 9997   2001        .         0 |
              | 9997   2002        .         0 |
              | 9997   2003        2         0 |
              | 9997   2004        .         0 |
              |--------------------------------|
              | 9998   2002        .         0 |
              | 9998   2003        2         0 |
              | 9998   2004        .         0 |
              |--------------------------------|
              | 9999   2000        .         0 |
              | 9999   2001        .         0 |
              | 9999   2002        .         0 |
              | 9999   2003        .         0 |
              | 9999   2004        .         0 |
              +--------------------------------+

            Comment

            Working...
            X