Announcement

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

  • Problems generating a variable conditional on others in panel data

    Hello,

    I've managed to successfully generate a variable that describes the proportion of people working from home during the pandemic within their occupation, socworkfromhome, using the following command:
    bys b10soc3: egen socworkfromhome2016=mean(cw1_workfromhome)
    Where b10soc3 is the person's occupational code in 2016.
    Now, where I am stuck is that I want to generate a variable socworkfromhome2012 where if socworkfromhome2016=0.5 when b10soc3=111 and socworkfromhome2016=0.5 when b10soc=112 then, I want tsocworkfromhome2012 to equal 0.5 if b9soc3 (2012 occupational code)=111 and equal 0.25 if b9soc3=112 etc. However, I do not know how to do this.

    It feels like I'm missing something very simple (although I can't work out how to do it from any online resource) and I'm not sure I have explained myself well, so thank you for any help.
    If it helps, my data looks like this:
    Code:
    * Example generated by -dataex-. For more info, type help dataex
    clear
    input int(b10soc3 b9soc3) float(cw1_workfromhome socworkfromhome2016)
    544 523  0   .0909091
    416 411  .  .27380952
    415 415  0    .313253
    353 353  .    .365285
    813 112  0 .018181818
    119 112  0  .09036145
    125 245  1   .1737805
    247 343  0   .3703704
    113 113  1   .3995633
    823 123  0 .020833334
    415   0  1    .313253
    113 113  1   .3995633
    113 113  0   .3995633
    921 921  0  .07777778
    421 421  .   .2534722
    125 112  0   .1737805
    542 112  . .029411765
    244   0 .5      .6875
    354 353  1  .29672897
    413   .  0  .27857143
    end
    Last edited by Joseph Richardson; 15 Jun 2022, 09:52.

  • #2
    I managed to sort my problem with the following code (I had to learn about macros, which I didn't previously know to solve it):
    gen socworkfromhome2012_2=0 if b9soc3>0&b9soc3!=.
    levelsof b10soc3, local(b10soc3list)
    foreach l of local b10soc3list{
    egen a`l'=max(socworkfromhome2016*cond(b10soc3==`l', 1, 0))
    replace socworkfromhome2012=a`l' if b9soc3==`l'
    drop a`l'
    }
    Thus, feel free to ignore this thread.

    Comment

    Working...
    X