Announcement

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

  • Combining Head and Spouse Data Using a Loop

    Hi All,

    Is it possible to use a loop that creates a single/combined variable for heads and spouses? Here is an example of several income variables as well as the respondents ID, the year, and their "position" (where 1 is a head and 0 is a spouse).

    Is it possible to create single income variables using a for loop? For example, instead of "hdivinc" and "sdivinc" I would just like "divinc" where divinc takes one the value of hdivinc if "position" == 1 and sdivinc if "position" == 0. If I have overlooked an answer to a similar question, please feel free to point me in the right direction.


    Many thanks in advance!
    Cora


    Code:
    * Example generated by -dataex-. For more info, type help dataex
    clear
    input float ID int year float(position hrentincf srentincf hassetbusincf sassetbusincf) long(hdivinc sdivinc) float(hchildsuprttranf schildsuprttranf hsupsectranf ssupsectranf sadctranf hadctranf halimonytranf salimonytranf)
     5003 2005 1     0     0      0    0     0     0    0    0   0    0 0 0 0 0
     5003 2007 1     0     0      0    0     0     0    0    0   0    0 0 0 0 0
     5003 2009 1     0     0      0    0     0     0    0    0   0    0 0 0 0 0
     5003 2011 1     0     0      0    0     0     0    0    0   0    0 0 0 0 0
     5003 2013 1     0     0      0    0     0     0    0    0   0    0 0 0 0 0
     5003 2015 1     0     0      0    0     0     0    0    0   0    0 0 0 0 0
     5003 2017 1     0     0      0    0     0     0    0    0   0    0 0 0 0 0
     5003 2019 1     0     0      0    0     0     0    0    0   0    0 0 0 0 0
     5004 2005 0     0     0      0    0     0     0    0    0   0 7800 0 0 0 0
     5004 2007 1     0     0      0    0     0     0    0    0 479    0 0 0 0 0
     5004 2009 1     0     0      0    0     0     0    0    0   0    0 0 0 0 0
     5004 2011 0     0     0   6000    0     0     0    0    0   0 8088 0 0 0 0
     5004 2013 0     0     0   4000    0     0     0    0    0   0 8136 0 0 0 0
     5004 2015 0     0     0      0    0     0     0    0    0   0 8652 0 0 0 0
     5004 2017 0     0     0      0    0     0     0    0    0   0 1932 0 0 0 0
     5004 2019 0     0     0    450    0     0     0    0    0   0 1920 0 0 0 0
     5177 2005 0     0     0      0    0     0     0 1716 3120   0    0 0 0 0 0
     5177 2007 0     0     0      0    0     0     0 1716 3380   0    0 0 0 0 0
     5177 2009 0     0     0      0    0     0     0 1716 3380   0    0 0 0 0 0
     5177 2011 0     0     0      0    0     0     0    0    0   0    0 0 0 0 0
     5177 2013 0     0     0      0    0     0     0    0    0   0    0 0 0 0 0
     5177 2015 0     0     0      0    0     0     0    0    0   0    0 0 0 0 0
     5177 2017 0     0     0      0    0     0     0    0    0   0    0 0 0 0 0
     5177 2019 0     0     0      0    0     0     0    0    0   0    0 0 0 0 0
     5181 2005 0     0     0      0    0     0     0    0    0   0    0 0 0 0 0
     5181 2007 0     0     0      0    0     0     0    0    0   0    0 0 0 0 0
     5181 2009 0     0     0      0    0     0     0    0    0   0    0 0 0 0 0
     5181 2011 1     0     0      0    0     0     0    0    0   0    0 0 0 0 0
     5181 2013 1     0     0      0    0     0     0 5520    0   0    0 0 0 0 0
     5181 2015 0     0     0      0    0     0     0    0 5520   0    0 0 0 0 0
     5181 2017 0     0     0      0    0     0     0    0 5980   0    0 0 0 0 0
     5181 2019 0     0     0      0    0     0     0    0 5980   0    0 0 0 0 0
     6006 2005 1 18000     0      0    0 32500 32500    0    0   0    0 0 0 0 0
     6006 2007 1 37000     0      0    0 68500     0    0    0   0    0 0 0 0 0
     6006 2009 1     0 37000      0    0     0     0    0    0   0    0 0 0 0 0
     6006 2011 1     0 39600      0    0     0     0    0    0   0    0 0 0 0 0
     6006 2013 1     0  9000      0    0   575   400    0    0   0    0 0 0 0 0
     6006 2015 1     0 25000      0    0   853   428    0    0   0    0 0 0 0 0
     6006 2017 1     0     0      0    0   400   400    0    0   0    0 0 0 0 0
     6006 2019 1     0     0      0    0   500   500    0    0   0    0 0 0 0 0
     6032 2005 1     0     0      0    0    50     0    0    0   0    0 0 0 0 0
     6032 2007 1     0     0      0    0   100     0    0    0   0    0 0 0 0 0
     6032 2009 1     0     0      0    0   100     0    0    0   0    0 0 0 0 0
     6032 2011 1  6000     0      0    0   150   150    0    0   0    0 0 0 0 0
     6032 2013 1 12000     0      0    0   575   400    0    0   0    0 0 0 0 0
     6032 2015 1  2500     0      0    0   250   250    0    0   0    0 0 0 0 0
     6032 2017 1     0     0      0    0   450   450    0    0   0    0 0 0 0 0
     6032 2019 1     0     0      0    0   250   250    0    0   0    0 0 0 0 0
     7035 2005 1     0     0      0    0     0     0    0    0   0    0 0 0 0 0
     7035 2007 1     0     0      0    0     0     0    0    0   0    0 0 0 0 0
     7035 2009 1     0     0      0    0     0     0    0    0   0    0 0 0 0 0
     7035 2011 1     0     0      0    0     0     0    0    0   0    0 0 0 0 0
     7035 2013 1     0     0      0    0     0     0    0    0   0    0 0 0 0 0
     7035 2015 1     0     0      0    0     0     0    0    0   0    0 0 0 0 0
     7035 2017 1     0     0      0    0     0     0    0    0   0    0 0 0 0 0
     7035 2019 1     0     0      0    0     0     0    0    0   0 9252 0 0 0 0
    10003 2005 1     0     0      0    0     0     0    0    0   0    0 0 0 0 0
    10003 2007 1     0     0      0    0     0     0    0    0   0    0 0 0 0 0
    10003 2009 1     0     0      0    0     0     0    0    0   0    0 0 0 0 0
    10003 2011 1  1200     0      0    0     0     0    0    0   0    0 0 0 0 0
    10003 2013 1     0     0      0    0     0     0    0    0   0    0 0 0 0 0
    10003 2015 1     0     0      0    0     0     0    0    0   0    0 0 0 0 0
    10003 2017 1  3600     0      0    0     0     0    0    0   0    0 0 0 0 0
    10003 2019 1     0     0      0    0     0     0    0    0   0    0 0 0 0 0
    10006 2005 1     0     0      0    0     0     0 1400    0   0    0 0 0 0 0
    10006 2007 1     0     0      0    0     0     0  900    0   0    0 0 0 0 0
    10006 2009 1     0     0      0    0     0     0    0    0   0    0 0 0 0 0
    10006 2011 1     0     0      0    0     0     0 3125    0   0    0 0 0 0 0
    10006 2013 1     0     0      0    0     0     0    0    0   0    0 0 0 0 0
    10006 2015 1     0     0      0    0     0     0    0    0   0    0 0 0 0 0
    10006 2017 1     0     0      0    0     0     0    0    0   0    0 0 0 0 0
    10006 2019 1     0     0      0    0     0     0    0    0   0    0 0 0 0 0
    10021 2005 1     0     0   5000    0     0     0    0    0   0    0 0 0 0 0
    10021 2007 1     0     0      0    0     0     0    0    0   0    0 0 0 0 0
    10021 2009 1     0     0      0    0     0     0    0    0   0    0 0 0 0 0
    10021 2011 1     0     0      0    0     0     0    0    0   0    0 0 0 0 0
    10021 2013 1     0     0      0    0     0     0    0    0   0 8760 0 0 0 0
    10021 2015 1     0     0      0    0     0     0    0    0   0 7812 0 0 0 0
    10021 2017 1     0     0      0    0     0     0    0    0   0 8808 0 0 0 0
    10021 2019 1     0     0      0    0     0     0    0    0   0    0 0 0 0 0
    10030 2005 0     0     0      0    0   500   300    0    0   0    0 0 0 0 0
    10030 2007 0     0     0      0    0    75    75    0    0   0    0 0 0 0 0
    10030 2009 0     0     0  -6182    0     0     0    0    0   0    0 0 0 0 0
    10030 2011 0     0     0    937 1875    63    63    0    0   0    0 0 0 0 0
    10030 2013 0     0     0    500 1000     0     0    0    0   0    0 0 0 0 0
    10030 2015 0     0     0      0    0     0     0    0    0   0    0 0 0 0 0
    10030 2017 0     0     0      0    0   250   250    0    0   0    0 0 0 0 0
    10030 2019 0     0     0      0    0   600   538    0    0   0    0 0 0 0 0
    10181 2005 1     0     0      0    0   500   300    0    0   0    0 0 0 0 0
    10181 2007 1     0     0      0    0    75    75    0    0   0    0 0 0 0 0
    10181 2009 1     0     0  -6182    0     0     0    0    0   0    0 0 0 0 0
    10181 2011 1     0     0    937 1875    63    63    0    0   0    0 0 0 0 0
    10181 2013 1     0     0    500 1000     0     0    0    0   0    0 0 0 0 0
    10181 2015 1     0     0      0    0     0     0    0    0   0    0 0 0 0 0
    10181 2017 1     0     0      0    0   250   250    0    0   0    0 0 0 0 0
    10181 2019 1     0     0      0    0   600   538    0    0   0    0 0 0 0 0
    13004 2005 0     0     0 -36250    0  1800  2700    0    0   0    0 0 0 0 0
    13004 2007 0     0     0      0    0  1107  1107    0    0   0    0 0 0 0 0
    13004 2009 0     0     0   2100    0     0     0    0    0   0    0 0 0 0 0
    13004 2011 1     0     0      0    0     0     0    0    0   0    0 0 0 0 0
    end

  • #2
    P.S., I would specifically like a look, as the real dataset is much larger with many more income components. It's taking quite some time to write the commands

    gen divinc = hdivinc if position == 1
    replace divinc = sdivinc if position == 0

    Thanks!

    Comment


    • #3
      This presumes that only your income variables begin with "h". Otherwise you will need another way to specify the overall list of income variables for either the head or the spouse.

      Code:
      foreach var of varlist h* {
          local stub = substr("`var'",2,.)
          local typ: type `var'
          gen `typ' `stub' = h`stub' if position == 1
          replace `stub' = s`stub' if position == 0
      }

      Comment


      • #4
        Hemanshu,

        Thanks so much - this worked!

        Comment

        Working...
        X