Announcement

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

  • Assign value to a categorical variable between two limits

    Dear users, hope everyone is well.

    i want to assign value of 1 to the variable "dum" when the current year "survive" not equal dot and it become dot in the next period.

    Kindly note that i want 1 only in years (at firm level) after the year in which survive != .

    For example; for stkcd number 4, i want 1 in years 2009 to 2014.

    i tried the following command but it assign the value only in the next period (no matter the next period "survival" is equal dot or not).



    Code:
    bys stkcd: replace dum = 1 if survive != 0 & F.survive == 0
    is there any solution command for this?

    Code:
    * Example generated by -dataex-. To install: ssc install dataex
    clear
    input double stkcd float(year survive dum)
    1 1990        . .
    1 1991        . .
    1 1992        . .
    1 1993        . .
    1 1994        . .
    1 1995        . .
    1 1996        . .
    1 1997        . .
    1 1998        . .
    1 1999        . .
    1 2000        . .
    1 2001        . .
    1 2002        . .
    1 2003        . .
    1 2004        . .
    1 2005        . .
    1 2006        . .
    1 2007        . .
    1 2008        . .
    1 2009        . .
    1 2010        . .
    1 2011        . .
    1 2012        . .
    1 2013        . .
    1 2014        . .
    1 2015        . .
    1 2016        . .
    1 2017        . .
    1 2018        . .
    1 2019        . .
    1 2020        . .
    2 1991        . .
    2 1992        . .
    2 1993        . .
    2 1994        . .
    2 1995        . .
    2 1996        . .
    2 1997        . .
    2 1998        . .
    2 1999        . .
    2 2000        . .
    2 2001        . .
    2 2002        . .
    2 2003        . .
    2 2004        . .
    2 2005        . .
    2 2006        . .
    2 2007        . .
    2 2008        . .
    2 2009        . .
    2 2010        . .
    2 2011        . .
    2 2012        . .
    2 2013        . .
    2 2014        . .
    2 2015        . .
    2 2016        . .
    2 2017        . .
    2 2018        . .
    2 2019        . .
    2 2020        . .
    3 1991        . .
    3 1992        . .
    3 1993        . .
    3 1994        . .
    3 1995        . .
    3 1996        . .
    3 1997        . .
    3 1998        . .
    3 1999        . .
    3 2000        . .
    3 2001        . .
    4 1991        . .
    4 1992        . .
    4 1993        . .
    4 1994        . .
    4 1995        . .
    4 1996        . .
    4 1997        . .
    4 1998        . .
    4 1999        . .
    4 2000        . .
    4 2001        . .
    4 2002        . .
    4 2003        . .
    4 2004        . .
    4 2005        . .
    4 2006        . .
    4 2007   598120 0
    4 2008    64400 1
    4 2009        . .
    4 2010        . .
    4 2011        . .
    4 2012        . .
    4 2013        . .
    4 2014        . .
    4 2015  53908.8 0
    4 2016 65308.84 0
    4 2017     4000 0
    4 2018  7908000 1
    end
    format %ty year
    Last edited by Obaid Ur Rehman; 01 Dec 2021, 00:27.

  • #2
    Obaid:
    does the following code do the trick?
    Code:
    bysort stkcd (year): gen wanted=3 if survival[_n]!=0 & survival[_n+1]==0
    Kind regards,
    Carlo
    (Stata 19.0)

    Comment


    • #3
      Dear Carlo, i tried your mentioned code on data provided in #1, but the output is still not desirable (as mentioned in #1)

      I also make some changes in your code, such as change _n to _N, but still the problem exist.

      Comment


      • #4
        Is this what you want?

        Code:
        bys stkcd (year): replace dum = dum[_n-1] if mi(dum)

        Comment


        • #5
          thanks Fei Wang, the trick works perfectly.

          Comment

          Working...
          X