Announcement

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

  • Identify observations appeared in the previous year

    Dear All, I find this question here. The data set is
    Code:
    * Example generated by -dataex-. To install: ssc install dataex
    clear
    input byte stkcd str5 year str8 BusinessInstitutionID byte want
    4 "2011"  "10515981" .
    4 "2012 " "10515981" 1
    4 "2013 " "10515981" 1
    4 "2013 " "10513314" .
    4 "2014 " "10402864" .
    4 "2014"  "10515981" .
    4 "2014 " "10513314" 1
    4 "2015 " "10516931" .
    4 "2015"  "10514921" .
    4 "2015"  "10515981" 1
    4 "2015"  "10513314" 1
    4 "2016"  "10516535" .
    4 "2016"  "10519103" .
    4 "2016"  "10516931" 1
    4 "2016"  "10402864" .
    4 "2016"  "10515981" 1
    4 "2017 " "10520158" .
    4 "2017"  "10519103" 1
    4 "2017"  "10402864" 1
    4 "2017"  "10513235" .
    4 "2017 " "10520422" .
    end
    For each stkcd and each year, I'd like to identify whether `BusinessInstitutionID' has appeared in the previous year. If yes, return 1, otherwise missing data (zero is also fine). The variable `want' is the desired outcome. Thanks.
    Ho-Chuan (River) Huang
    Stata 19.0, MP(4)

  • #2
    Code:
    destring year, replace
    bys stkcd BusinessInstitutionID (year): gen wanted = (year[_n-1] == (year - 1))
    list , sepby(BusinessInstitutionID)
    ---------------------------------
    Maarten L. Buis
    University of Konstanz
    Department of history and sociology
    box 40
    78457 Konstanz
    Germany
    http://www.maartenbuis.nl
    ---------------------------------

    Comment


    • #3
      Dear Maarten, Thanks a lot for this helpful suggestion.
      Ho-Chuan (River) Huang
      Stata 19.0, MP(4)

      Comment


      • #4
        An important detail is implicit here. Maarten Buis created the indicator as 1 if true and 0 if false. That's much more useful than a variable that is 1 if true and missing if false.

        Comment


        • #5
          Dear Nick, I agree with your point.

          Ho-Chuan (River) Huang
          Stata 19.0, MP(4)

          Comment

          Working...
          X