Announcement

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

  • Problem saving ineqdeco output as variable

    Hi,

    I am trying to save output from ineqdeco (from scc, updated) as a variable, but can't work it out. I get no error message, just the message "(0 real changes made)". The code I used is

    Code:
    gen ge1 = .
    qui ineqdeco Outcomm_f, by(year)
     foreach z of varlist year {
      replace ge1=r(ge1_`z') if year==`z'
     }
    I also tried to put the ineqdeco inside the loop.

    Code:
     foreach z of varlist year {
     qui ineqdeco Outcomm_f if year==`z'
      replace ge1=r(ge1_`z') if year==`z'
     }
    Then it looks like it works, and I get the message "(15,957 real changes made, 15,957 to missing)", but the variable still only contains missing values.

    Could anyone please tell me what I am doing wrong? I would really appreciate help on this, sine I'm completely stuck.

    Sincerely,
    Helena

  • #2
    No; that's a radical misunderstanding of -foreach- but it can easily be fixed.

    Your loops are each loops over a single item, the variable name -year-. So the first loop is equivalent to

    Code:
     
    gen ge1 = . 
    qui ineqdeco Outcomm_f, by(year) 
    replace ge1=r(ge1_year) if year==year
    That's legal but useless. The restriction -if year == year- is vacuous (it selects every observation) but there is no stored result with that name and you should get all missings.

    You're imagining that a loop mentioning a variable name means a loop over the distinct values of a variable name, but that's not how it works.

    The second loop asks for different output, but there's the same misconception.

    To loop over the distinct values, you have to make them explicit directly or indirectly.

    This works and is reproducible.

    Code:
    sysuse auto   
    ineqdeco mpg, by(rep78)   
    gen ge1 = .   
    quietly foreach r in `r(levels)' {     
        replace ge1 = r(ge1_`r') if rep78 == `r'  
    }   
    tabdisp rep78, c(ge1)
    So, at a guess, which we can't test because you give no example data, you need

    Code:
    gen ge1 = . 
    qui ineqdeco Outcomm_f, by(year)  
    foreach y in `r(levels)' {     
        replace ge1 = r(ge1_`y') if year==`y' 
    }
    Last edited by Nick Cox; 25 May 2018, 09:20.

    Comment


    • #3
      Thanks so much. it works. And thank you so much for taking your time to write a thorough explanation, it is much appreciated!

      Comment

      Working...
      X