Search code examples
replacestatamean

Using egen functions with replace


I created a new variable from the mean of another variable using egen:

egen afd_lr2 = mean(afd_lire2w) if ost == 0

Now I would like to replace the values with the mean of another variable if ost == 1:

replace afd_lr2 = mean(afd_lireo) if ost ==1

This is not possible, as the mean function cannot be used with the replace command.

How can I achieve my goal?


Solution

  • This should work

    egen afd_lr2 = mean(cond(ost == 0, afd_lire2w, cond(ost == 1, afd_lireo, .))), by(ost) 
    

    Here is a test:

    clear
    input float(group y1 y2)
    1 42   .
    1 42   .
    2  . 999
    2  . 999
    end
    
    egen mean = mean(cond(group == 1, y1, cond(group == 2, y2, .))), by(group)
    
    tabdisp group, c(mean)
    
    ----------------------
        group |       mean
    ----------+-----------
            1 |         42
            2 |        999
    ----------------------
    

    The key is that the mean() function of egen feeds on an expression, which can be more complicated than a single variable name. That said, this is trickier than I would generally advise, as

    generate work = afd_lire2w if ost == 0
    replace work = afd_lireo if ost == 1
    egen mean = mean(work), by(ost)
    

    is easier to understand and should occur to a programmer any way.