Announcement

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

  • long format data, creating a new variable (loop)

    Dear Statalist,
    I have data in long format with observation year: 2000 to 2019, n=125 000.
    I have a variable named “received disability benefit “ , the variable takes the value 1 the year the person received disability benefit, the other years the value will be missing. I would like to compute a new variable that takes the value of 1 if the person have received disability benefit at least one year during the observation period (2000-2019).
    Is it possible to compute such variable when the data are in long format?
    Many thanks,


    The data look like this, example of one observation:
    id year Received disability benefit Received disability benefit at least one year during the observation period
    20233 2000 . 1
    20233 2001 . 1
    20233 2002 . 1
    20233 2003 . 1
    20233 2004 . 1
    20233 2005 . 1
    20233 2006 . 1
    20233 2007 . 1
    20233 2008 . 1
    20233 2009 . 1
    20233 2010 . 1
    20233 2011 1 1
    20233 2012 1 1
    20233 2013 1 1
    20233 2014 1 1
    20233 2015 1 1
    20233 2016 1 1
    20233 2017 1 1
    20233 2018 1 1
    20233 2019 1 1
    Last edited by Sunniva; 09 Dec 2021, 05:08.

  • #2
    Code:
    egen want = max(received) ,by(id)

    Comment


    • #3
      Better to code indicators 1/0 instead of 1/missing. This is an FAQ: https://www.stata.com/support/faqs/d...ble-recording/

      Code:
      bys id: egen wanted= max(disability==1)

      Comment


      • #4
        Many thanks, the code worked.

        Comment

        Working...
        X