Announcement

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

  • Using Binary Monthly Variables to Adjust Income

    Hi all,

    I am trying to create a new variable for each income source that is a proportion of "yes" values by ID and year. For example, if ID 10030 responded "received" to "hdivincrep" for January, February, and March (months shown in last three letters of each variable), I would like the new variable to be valued "1." But, if they only received this income in January and February, the value would be ".67" and so on. Is this possible? Many thanks in advance.

    Best,
    Cora

    Code:
    * Example generated by -dataex-. For more info, type help dataex
    clear
    input double ID int year byte(hprofincrepjan hprofincrepfeb hprofincrepmar hdivincrepjan hdivincrepfeb hdivincrepmar sintincrepjan sintincrepfeb sintincrepmar sadctranrepjan sadctranrepfeb sadctranrepmar)
     4006 2001 0 0 0 0 0 0 0 0 0 0 0 0
     4006 2003 0 0 0 0 0 0 0 0 0 0 0 0
     4006 2005 0 0 0 0 0 0 0 0 0 0 0 0
     4031 2001 0 0 0 0 0 0 0 0 0 0 0 0
     4031 2003 0 0 0 0 0 0 0 0 0 0 0 0
     4031 2005 0 0 0 0 0 0 0 0 0 0 0 0
     5003 2001 0 0 0 0 0 0 0 0 0 0 0 0
     5003 2003 0 0 0 0 0 0 0 0 0 0 0 0
     5003 2005 0 0 0 0 0 0 0 0 0 0 0 0
     5004 2001 0 0 0 0 0 0 0 0 0 0 0 0
     5004 2003 0 0 0 0 0 0 0 0 0 0 0 0
     5004 2005 0 0 0 0 0 0 0 0 0 0 0 0
     5170 2001 0 0 0 0 0 0 0 0 0 0 0 0
     5170 2003 0 0 0 0 0 0 0 0 0 0 0 0
     5170 2005 0 0 0 0 0 0 0 0 0 0 0 0
     7035 2001 0 0 0 0 0 0 0 0 0 0 0 0
     7035 2003 0 0 0 0 0 0 0 0 0 0 0 0
     7035 2005 0 0 0 0 0 0 0 0 0 0 0 0
     7182 2001 0 0 0 0 0 0 0 0 0 0 0 0
     7182 2003 0 0 0 0 0 0 0 0 0 0 0 0
     7182 2005 0 0 0 0 0 0 0 0 0 0 0 0
    10006 2001 0 0 0 0 0 0 0 0 0 0 0 0
    10006 2003 0 0 0 0 0 0 0 0 0 0 0 0
    10006 2005 0 0 0 0 0 0 0 0 0 0 0 0
    10030 2001 0 0 0 0 0 0 0 0 0 0 0 0
    10030 2003 0 0 0 1 1 1 0 0 0 0 0 0
    10030 2005 0 0 0 0 0 0 1 1 1 0 0 0
    14003 2001 0 0 0 1 1 1 0 0 0 0 0 0
    14003 2003 0 0 0 0 0 1 0 0 0 0 0 0
    14003 2005 0 0 0 1 1 1 0 0 0 0 0 0
    14004 2001 0 0 0 0 0 0 0 0 0 0 0 0
    14004 2003 0 0 0 0 0 0 0 0 0 0 0 0
    14004 2005 0 0 0 0 0 0 0 0 0 0 0 0
    14172 2001 0 0 0 0 0 0 0 0 0 0 0 0
    14172 2003 0 0 0 0 0 0 0 0 0 0 0 0
    14172 2005 0 0 0 0 0 0 0 0 0 0 0 0
    14173 2001 0 0 0 1 1 1 0 0 0 0 0 0
    14173 2003 0 0 0 0 0 1 0 0 0 0 0 0
    14173 2005 0 0 0 1 1 1 0 0 0 0 0 0
    18003 2001 0 0 0 0 0 0 0 0 0 0 0 0
    18003 2003 0 0 0 0 0 0 0 0 0 0 0 0
    18003 2005 0 0 0 0 0 0 0 0 0 0 0 0
    18031 2001 0 0 0 0 0 0 0 0 0 0 0 0
    18031 2003 0 0 0 0 0 0 0 0 0 0 0 0
    18031 2005 0 0 0 0 0 0 0 0 0 0 0 0
    18185 2001 0 0 0 0 0 0 0 0 0 0 0 0
    18185 2003 0 0 0 0 0 0 0 0 0 0 0 0
    18185 2005 0 0 0 0 0 0 0 0 0 0 0 0
    22003 2001 0 0 0 0 0 0 0 0 0 0 0 0
    22003 2003 0 0 0 0 0 0 0 0 0 0 0 0
    22003 2005 0 0 0 0 0 0 0 0 0 0 0 0
    22004 2001 0 0 0 0 0 0 0 0 0 0 0 0
    22004 2003 0 0 0 0 0 0 0 0 0 0 0 0
    22004 2005 0 0 0 0 0 0 0 0 0 0 0 0
    22173 2001 0 0 0 0 0 0 0 0 0 0 0 0
    22173 2003 0 0 0 0 0 0 0 0 0 0 0 0
    22173 2005 0 0 0 0 0 0 0 0 0 0 0 0
    23031 2001 0 0 0 0 0 0 0 0 0 0 0 0
    23031 2003 0 0 0 0 0 0 0 0 0 0 0 0
    23031 2005 0 0 0 0 0 0 0 0 0 0 0 0
    39003 2001 0 0 0 0 0 0 0 0 0 0 0 0
    39003 2003 0 0 0 0 0 0 0 0 0 0 0 0
    39003 2005 0 0 0 0 0 0 0 0 0 0 0 0
    39171 2001 0 0 0 1 1 1 0 0 0 0 0 0
    39171 2003 0 0 0 1 1 1 0 0 0 0 0 0
    39171 2005 0 0 0 1 1 1 0 0 0 0 0 0
    40003 2001 0 0 0 0 0 0 0 0 0 0 0 0
    40003 2003 0 0 0 0 0 0 0 0 0 0 0 0
    40003 2005 0 0 0 0 0 0 0 0 0 0 0 0
    40004 2001 0 0 0 0 0 0 0 0 0 0 0 0
    40004 2003 0 0 0 0 0 0 0 0 0 0 0 0
    40004 2005 0 0 0 0 0 0 0 0 0 0 0 0
    40005 2001 0 0 0 0 0 0 0 0 0 0 0 0
    40005 2003 0 0 0 0 0 0 0 0 0 0 0 0
    40005 2005 0 0 0 0 0 0 0 0 0 0 0 0
    40031 2001 0 0 0 0 0 0 0 0 0 0 0 0
    40031 2003 0 0 0 0 0 0 0 0 0 0 0 0
    40031 2005 0 0 0 0 0 0 0 0 0 0 0 0
    40170 2001 0 0 0 0 0 0 0 0 0 0 0 0
    40170 2003 0 0 0 0 0 0 0 0 0 0 0 0
    40170 2005 0 0 0 0 0 0 0 0 0 0 0 0
    40181 2001 0 0 0 0 0 0 0 0 0 0 0 0
    40181 2003 0 0 0 0 0 0 0 0 0 0 0 0
    40181 2005 0 0 0 0 0 0 0 0 0 0 0 0
    41005 2001 0 0 0 0 0 0 0 0 0 0 0 0
    41005 2003 0 0 0 0 0 0 0 0 0 0 0 0
    41005 2005 0 0 0 0 0 0 0 0 0 0 0 0
    41007 2001 0 0 0 0 0 0 0 0 0 0 0 0
    41007 2003 0 0 0 0 0 0 0 0 0 0 0 0
    41007 2005 0 0 0 0 0 0 0 0 0 0 0 0
    41030 2001 0 0 0 0 0 0 0 0 0 0 0 0
    41030 2003 0 0 0 0 0 0 0 0 0 0 0 0
    41030 2005 0 0 0 0 0 0 0 0 0 0 0 0
    41177 2001 0 0 0 0 0 0 0 0 0 0 0 0
    41177 2003 0 0 0 0 0 0 0 0 0 0 0 0
    41177 2005 0 0 0 0 0 0 0 0 0 0 0 0
    41194 2001 0 0 0 0 0 0 0 0 0 0 0 0
    41194 2003 0 0 0 0 0 0 0 0 0 0 0 0
    41194 2005 0 0 0 0 0 0 0 0 0 0 0 0
    45003 2001 0 0 0 0 0 0 0 0 0 0 0 0
    end
    label values hprofincrepjan ER25933L
    label def ER25933L 0 "Inap.: did not receive income from professional practice or trade this month; did not receive income from professional practice or trade in 2004", modify
    label values hprofincrepfeb ER25934L
    label def ER25934L 0 "Inap.: did not receive income from professional practice or trade this month; did not receive income from professional practice or trade in 2004", modify
    label values hprofincrepmar ER25935L
    label def ER25935L 0 "Inap.: did not receive income from professional practice or trade this month; did not receive income from professional practice or trade in 2004", modify
    label values hdivincrepjan ER26005L
    label def ER26005L 0 "Inap.: did not receive income from dividends this month; did not receive income from dividends in 2004", modify
    label def ER26005L 1 "Was received this month", modify
    label values hdivincrepfeb ER26006L
    label def ER26006L 0 "Inap.: did not receive income from dividends this month; did not receive income from dividends in 2004", modify
    label def ER26006L 1 "Was received this month", modify
    label values hdivincrepmar ER26007L
    label def ER26007L 0 "Inap.: did not receive income from dividends this month; did not receive income from dividends in 2004", modify
    label def ER26007L 1 "Was received this month", modify
    label values sintincrepjan ER26355L
    label def ER26355L 0 `"Inap.: did not receive income from interest this month; no wife/"wife" in FU; did not receive any income in 2004; did not receive income from interest in 2004"', modify
    label def ER26355L 1 "Was received this month", modify
    label values sintincrepfeb ER26356L
    label def ER26356L 0 `"Inap.: did not receive income from interest this month; no wife/"wife" in FU; did not receive any income in 2004; did not receive income from interest in 2004"', modify
    label def ER26356L 1 "Was received this month", modify
    label values sintincrepmar ER26357L
    label def ER26357L 0 `"Inap.: did not receive income from interest this month; no wife/"wife" in FU; did not receive any income in 2004; did not receive income from interest in 2004"', modify
    label def ER26357L 1 "Was received this month", modify
    label values sadctranrepjan ER26404L
    label def ER26404L 0 `"Inap.: did not receive income from TANF/state program this month; no wife/"wife" in FU; did not receive any income in 2004; did not receive any transfer income in 2004; did not receive income from TANF/state program in 2004"', modify
    label values sadctranrepfeb ER26405L
    label def ER26405L 0 `"Inap.: did not receive income from TANF/state program this month; no wife/"wife" in FU; did not receive any income in 2004; did not receive any transfer income in 2004; did not receive income from TANF/state program in 2004"', modify
    label values sadctranrepmar ER26406L
    label def ER26406L 0 `"Inap.: did not receive income from TANF/state program this month; no wife/"wife" in FU; did not receive any income in 2004; did not receive any transfer income in 2004; did not receive income from TANF/state program in 2004"', modify

  • #2
    I'm not entirely sure I understand what you want, but I think it's this:
    Code:
    ds *jan
    local sources `r(varlist)'
    local sources: subinstr local sources "jan" "", all
    
    foreach s of local sources {
        egen new_var_for_`s' = rowmean(`s'*)
    }
    Note: The code assumes, but does not verify, that there is a variable for every source of income in January--this is true in the example data. That is, there are no February or March income sources in the full data set that have no counterpart for January.

    Comment

    Working...
    X