Announcement

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

  • Panel data, if one observation is true for a participant generate a variable that is true for all observation of that participant

    Dear Statlist,

    A simple question this morning I think but one I have been struggling to answer (although I have found one very clunky solution). I am using panel data with employment status and I have been trying to generate a dummy variable of 1 for all observation of participants who have at least one instance of job loss during the duration of the panel.

    The solution i have found is using the command below and just repeating it until there are no more changes. It's a solution but I would really appreciate a cleaner one.

    Code:
    gen jobloss_anypoint = 1 if jobloss == 1
    replace jobloss_anypoint = 1 if jobloss[_n - 1] == 1 & _nomem_encr == _nomem_encr[_n - 1]
    replace jobloss_anypoint = 1 if jobloss[_n + 1] == 1 & _nomem_encr == _nomem_encr[_n + 1]
    Code:
    * Example generated by -dataex-. For more info, type help dataex
    clear
    input double _nomem_encr float jobloss int _Year float jobloss_anypoint
    800009 0 2008 .
    800009 0 2009 .
    800009 0 2010 .
    800009 0 2011 .
    800009 0 2012 .
    800009 0 2013 .
    800009 0 2014 .
    800009 0 2015 .
    800009 0 2016 .
    800009 0 2017 .
    800009 0 2018 .
    800009 0 2019 .
    800015 0 2008 .
    800015 0 2009 .
    800015 0 2010 .
    800015 0 2011 .
    800015 0 2012 .
    800015 0 2013 .
    800015 0 2014 .
    800015 0 2015 .
    800015 0 2016 .
    800015 0 2017 .
    800015 0 2018 .
    800015 0 2019 .
    800042 0 2008 .
    800042 0 2009 .
    800042 0 2010 .
    800042 0 2011 .
    800042 0 2012 .
    800042 0 2013 .
    800042 0 2014 .
    800042 0 2015 .
    800042 0 2016 .
    800042 0 2017 .
    800042 0 2018 .
    800042 0 2019 .
    800057 0 2008 .
    800057 0 2009 .
    800057 0 2010 .
    800057 0 2011 .
    800057 0 2012 .
    800057 0 2013 .
    800057 0 2014 .
    800057 0 2015 .
    800057 0 2016 .
    800057 0 2017 .
    800057 0 2018 .
    800057 0 2019 .
    800073 0 2015 .
    800073 0 2016 .
    800073 0 2017 .
    800073 1 2018 1
    800073 0 2019 .
    800085 0 2008 .
    800085 0 2009 .
    800085 0 2010 .
    800085 0 2011 .
    800085 0 2012 .
    800085 0 2013 .
    800085 0 2014 .
    800085 0 2015 .
    800085 0 2016 .
    800085 0 2017 .
    800085 0 2018 .
    800085 0 2019 .
    800100 0 2008 .
    800100 0 2009 .
    800100 0 2010 .
    800100 0 2011 .
    800100 0 2012 .
    800100 0 2013 .
    800100 0 2014 .
    800100 0 2015 .
    800100 0 2016 .
    800100 1 2017 1
    800100 0 2018 .
    800100 0 2019 .
    800119 0 2008 .
    800119 0 2009 .
    800119 0 2010 .
    800119 0 2011 .
    800119 0 2012 .
    800119 0 2013 .
    800119 0 2014 .
    800119 0 2015 .
    800125 0 2008 .
    800125 0 2009 .
    800125 0 2010 .
    800125 0 2011 .
    800125 0 2012 .
    800125 0 2013 .
    800125 0 2014 .
    800131 0 2008 .
    800131 0 2009 .
    800131 0 2010 .
    800131 0 2011 .
    800131 0 2012 .
    800131 0 2013 .
    800131 0 2014 .
    800131 0 2015 .
    end
    format %ty _Year
    Thanks for the help.

    Kind Regards,

    Hugo

  • #2
    Hugo:
    you may want to try:
    Code:
    . bysort _nomem_encr: egen wanted=sum (jobloss)
    
    . replace jobloss_anypoint=wanted
    
    . drop wanted
    Kind regards,
    Carlo
    (StataNow 18.5)

    Comment


    • #3
      Hey Carlo,

      Thanks so much for the quick reply, if I can be cheeky and ask for a second (related) question? I have also been trying to differentiate between attributes prior to jobloss and post. In effect creating a further dummy variable which is 1 for individuals post a jobloss (regardless of employement status) and 0 prior? So in the example of data above individual 800100 would be assigned 0 up to 2016 and 1 from 2017 to 2019.

      Kind Regards,

      Hugo

      Comment


      • #4
        Actually Carlo, I think my original code does what I am asking for. Apologies.

        Comment


        • #5
          Hugo:
          as far as your last question is concerned, you may want to try:
          Code:
          . bysort _nomem_encr: gen wanted2=jobloss
          
          bysort _nomem_encr: replace wanted2 = wanted2[_n-1] if wanted2[_n-1]==1
          Kind regards,
          Carlo
          (StataNow 18.5)

          Comment


          • #6
            #1 #2 See also FAQ https://www.stata.com/support/faqs/d...ble-recording/

            Comment

            Working...
            X