Announcement

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

  • For Each - Creating a New Variable if Another is Missing

    Hi All,

    I'm struggling to do something that I think can be done in a loop, as opposed to brute force. I have a dataset that has imputed and reported income values. I would like to ideally create a third variable that equals the imputed variable unless the reported variable equals 0 or "." The taxable income variables end in "inc" while the transfer variables end in "tran." Reported values additionally end in "rep." Additionally, head variables begin with "h" and spouse begin with "s." Thus, for example, spouses reported dividend income is represented by "sdivincrep."

    Currently, my code looks like many lines of:
    gen hdivincf = hdivinc
    replace hdivincf == 0 if hdivincrep == 0 | hdivincrep == .

    It would also be ok to just replace all of the imputed values with 0 if the reported value == 0 or == "." as opposed to creating the third variable.

    I've tried to write a loop, but have been unsuccessful.

    Code:
    * Example generated by -dataex-. For more info, type help dataex
    clear
    input float ID int year long(hrentincrep hdivincrep) float hadctranrep long(hrelhelptranrep srentincrep sdivincrep) float sadctranrep long(srelhelptranrep hrentinc hdivinc srentinc sdivinc) float(hadctran hrelhelptran sadctran srelhelptran position)
    4003 2005     0    0   0    0   0   0 0     0     0    0    0   0   0    0 0     0 1
    4003 2007     0    0   0    0   0   0 0     0     0    0    0   0   0    0 0     0 1
    4003 2009     0    0   0    0   0   0 0     0     0    0    0   0   0    0 0     0 1
    4003 2011     0    0   0    0   0   0 0     0     0    0    0   0   0    0 0     0 1
    4004 2005  6000  300   0    0   0   0 0     0  6000  300    0   0   0    0 0     0 1
    4004 2007  2000 1000   0    0   0   0 0     0 24000 1000    0   0   0    0 0     0 1
    4004 2009  1300    0   0    0   0   0 0     0  9100    0    0   0   0    0 0     0 1
    4004 2011 20000    0   0    0   0   0 0     0 20000    0    0   0   0    0 0     0 1
    4004 2013 25000    0   0    0   0   0 0     0 25000    0    0   0   0    0 0     0 1
    4006 2005     0    0   0    0   0   0 0     0     0    0    0   0   0    0 0     0 1
    4006 2007     0    0   0 5000   0   0 0     0     0    0    0   0   0 5000 0     0 1
    4006 2009     0    0   0  500   0   0 0     0     0    0    0   0   0  500 0     0 1
    4006 2011     0    0   0    0   0   0 0     0     0    0    0   0   0    0 0     0 1
    4006 2013     0    0   0    0   0   0 0     0     0    0    0   0   0    0 0     0 1
    4006 2015     0    0   0    0   0   0 0     0     0    0    0   0   0    0 0     0 1
    4006 2017     0    0   0    0   0   0 0     0     0    0    0   0   0    0 0     0 1
    4006 2019     0    0   0    0   0   0 0     0     0    0    0   0   0    0 0     0 1
    4007 2005     0    0   0    0   0   0 0     0     0    0    0   0   0    0 0     0 1
    4007 2017     0    0   0    0   0   0 0     0     0    0    0   0   0    0 0     0 1
    4007 2019     0    0   0    0   0   0 0     0     0    0    0   0   0    0 0     0 1
    4008 2005     0    0   0    0 400   0 0     0     0    0 4800   0   0    0 0     0 1
    4008 2009     0    0   0    0   0   0 0     0     0    0    0   0   0    0 0     0 1
    4008 2011     0    0   0    0   0   0 0     0     0    0    0   0   0    0 0     0 1
    4031 2005     0    0   0    0   0   0 0     0     0    0    0   0   0    0 0     0 0
    4031 2007     0    0 142  500   0   0 0     0     0    0    0   0 142  500 0     0 0
    4031 2009     0    0   0    0   0   0 0     0     0    0    0   0   0    0 0     0 0
    4031 2011     0    0   0  150   0   0 0     0     0    0    0   0   0  150 0     0 0
    4031 2013     0    0   0    0   0   0 0     0     0    0    0   0   0    0 0     0 0
    4031 2015     0    0   0    0   0   0 0     0     0    0    0   0   0    0 0     0 1
    4031 2017     0    0   0    0   0   0 0     0     0    0    0   0   0    0 0     0 1
    4031 2019     0    0   0  500   0   0 0     0     0    0    0   0   0  500 0     0 1
    4033 2005     0    0   0    0   0   0 0     0     0    0    0   0   0    0 0     0 1
    4033 2007     0    0   0    0   0   0 0     0     0    0    0   0   0    0 0     0 1
    4033 2009     0    0   0    0   0   0 0     0     0    0    0   0   0    0 0     0 1
    4034 2005     0    0   0  300   0   0 0     0     0    0    0   0   0  300 0     0 1
    4034 2009     0    0   0    0   0   0 0     0     0    0    0   0   0    0 0     0 1
    4034 2013     0    0   0    0   0   0 0     0     0    0    0   0   0    0 0     0 1
    4034 2015     0    0   0    0   0   0 0     0     0    0    0   0   0    0 0     0 1
    4034 2017     0    0   0    0   0   0 0     0     0    0    0   0   0    0 0     0 1
    4034 2019     0    0   0    0   0   0 0     0     0    0    0   0   0    0 0     0 1
    4035 2005     0   80   0 3000   0   0 0     0     0   80    0   0   0 3000 0     0 1
    4035 2009     0    0   0    0   0   0 0     0     0    0    0   0   0    0 0     0 1
    4036 2005     0  200   0    0   0   0 0     0     0  200    0   0   0    0 0     0 1
    4036 2009     0    0   0    0   0 160 0     0     0  500    0 300   0    0 0     0 0
    4036 2013     0    0   0    0   0   0 0  1200     0    0    0   0   0    0 0  1200 0
    4036 2015     0    0   0    0   0   0 0     0     0    0    0   0   0    0 0     0 0
    4038 2009     0    0   0 1000   0   0 0     0     0    0    0   0   0 1000 0     0 1
    4038 2011     0    0   0   75   0   0 0     0     0    0    0   0   0  650 0     0 1
    4038 2013     0    0   0  150   0   0 0     0     0    0    0   0   0 1800 0     0 1
    4038 2017     0    0   0    0   0   0 0     0     0    0    0   0   0    0 0     0 0
    4038 2019     0    0   0    0   0   0 0     0     0    0    0   0   0    0 0     0 0
    4039 2011     0    0   0  400   0   0 0   100     0    0    0   0   0 4000 0  1200 0
    4039 2013     0    0   0 1000   0   0 0     0     0    0    0   0   0 1000 0     0 0
    4039 2015     0    0   0 1000   0   0 0     0     0    0    0   0   0 1000 0     0 0
    4039 2017     0    0   0 2500   0   0 0     0     0    0    0   0   0 2500 0     0 1
    4039 2019     0    0   0 5000   0   0 0     0     0    0    0   0   0 5000 0     0 1
    4040 2017     0    0   0    0   0   0 0     0     0    0    0   0   0    0 0     0 1
    4040 2019     0    0   0  500   0   0 0     0     0    0    0   0   0  500 0     0 1
    4041 2011     0    0   0    0   0   0 0     0     0    0    0   0   0    0 0     0 1
    4041 2013     0    0   0    0   0   0 0 18000     0    0    0   0   0    0 0 18000 0
    4041 2017     0    0   0 1000   0   0 0     0     0    0    0   0   0 1000 0     0 1
    4041 2019     0    0   0    0   0   0 0     0     0    0    0   0   0    0 0     0 0
    4173 2005     0    0   0    0   0   0 0     0     0    0    0   0   0    0 0     0 0
    4173 2007     0    0   0    0   0   0 0     0     0    0    0   0   0    0 0     0 0
    4173 2009     0    0   0    0   0   0 0     0     0    0    0   0   0    0 0     0 0
    4173 2011     0    0   0    0   0   0 0     0     0    0    0   0   0    0 0     0 0
    4179 2005     0    0   0    0 400   0 0     0     0    0 4800   0   0    0 0     0 0
    4180 2005     0    0   0    0   0   0 0     0     0    0    0   0   0    0 0     0 0
    4180 2007     0    0   0  500   0   0 0     0     0    0    0   0   0  500 0     0 1
    4180 2009     0    0   0    0   0   0 0     0     0    0    0   0   0    0 0     0 1
    4180 2011     0    0   0    0   0   0 0     0     0    0    0   0   0    0 0     0 0
    4180 2013     0    0   0    0   0   0 0     0     0    0    0   0   0    0 0     0 1
    4180 2015     0    0   0    0   0   0 0     0     0    0    0   0   0    0 0     0 0
    4180 2017     0    0   0    0   0   0 0     0     0    0    0   0   0    0 0     0 0
    4180 2019     0    0   0    0   0   0 0     0     0    0    0   0   0    0 0     0 0
    4195 2005     0    0   0    0   0   0 0     0     0    0    0   0   0    0 0     0 1
    4195 2007     0    0 142  500   0   0 0     0     0    0    0   0 142  500 0     0 1
    4195 2009     0    0   0    0   0   0 0     0     0    0    0   0   0    0 0     0 1
    4195 2011     0    0   0  150   0   0 0     0     0    0    0   0   0  150 0     0 1
    4195 2013     0    0   0    0   0   0 0     0     0    0    0   0   0    0 0     0 1
    4196 2005     0    0   0    0   0   0 0     0     0    0    0   0   0    0 0     0 1
    4196 2007     0    0   0    0   0   0 0     0     0    0    0   0   0    0 0     0 1
    4196 2009     0    0   0    0   0   0 0     0     0    0    0   0   0    0 0     0 1
    4196 2011     0    0   0 4000   0   0 0     0     0    0    0   0   0 4000 0     0 1
    4196 2013     0    0   0    0   0   0 0     0     0    0    0   0   0    0 0     0 1
    4197 2009     0    0   0    0   0 160 0     0     0  500    0 300   0    0 0     0 1
    4197 2013     0    0   0    0   0   0 0  1200     0    0    0   0   0    0 0  1200 1
    4197 2015     0    0   0    0   0   0 0     0     0    0    0   0   0    0 0     0 1
    4198 2009     0    0   0    0   0   0 0     0     0    0    0   0   0    0 0     0 0
    4199 2011     0    0   0    0   0   0 0     0     0    0    0   0   0    0 0     0 0
    4200 2013     0    0   0    0   0   0 0 18000     0    0    0   0   0    0 0 18000 1
    4201 2011     0    0   0  400   0   0 0   100     0    0    0   0   0 4000 0  1200 1
    4201 2013     0    0   0 1000   0   0 0     0     0    0    0   0   0 1000 0     0 1
    4201 2015     0    0   0 1000   0   0 0     0     0    0    0   0   0 1000 0     0 1
    4202 2011     0    0   0    0   0   0 0     0     0    0    0   0   0    0 0     0 1
    4204 2015     0    0   0    0   0   0 0     0     0    0    0   0   0    0 0     0 1
    4204 2017     0    0   0    0   0   0 0     0     0    0    0   0   0    0 0     0 1
    4204 2019     0    0   0    0   0   0 0     0     0    0    0   0   0    0 0     0 1
    4205 2015     0    0   0    0   0   0 0     0     0    0    0   0   0    0 0     0 1
    4206 2017     0    0   0    0   0   0 0     0     0    0    0   0   0    0 0     0 1
    end

    Many thanks in advance!
    Cora

  • #2
    Code:
    foreach var of varlist *inc *tran {
        gen `var'f = `var'
        replace `var'f = 0 if `var'rep == 0 | `var'rep == .
    }

    Comment


    • #3
      The code in #2 is fine. Some may be interested in this way to write the statements inside the loop:

      Code:
      gen `var'f = cond(`var'rep == 0 | `var'rep == ., 0, `var')

      Comment


      • #4
        Apologies for the slow reply, but thank you both so much!! These responses worked well for me. :-)

        Comment

        Working...
        X