Announcement

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

  • Multiple value for local macro

    I have code to estimate two groups difference.I want to assign specific year groups instead of sample1 and sample2. For example, let sample1 have years from 2006 to 2012 and sample2 have years from 2017 to 2021. When I write them as local variables,

    local sample1 2006 2008 2009 2010 2011 2012
    local sample2 2017 2018 2019 2020 2021
    local year sample
    global sample1 `sample1'
    global sample2 `sample2'

    I get 'invalid 2007' error in the following code.

    How do I calculate the difference between these two groups by looping them in my year variable. I have tried many commands inrange, egen etc. none of them worked and I kept getting different errors. I would appreciate any help.

    Code:
    foreach var of varlist `indepvars5'  {
        sum `var' [aw=dwt] if `sample1'
        local `var'`sample1' : di %12.2f  r(mean)
        sum `var' [aw=dwt] if `sample2'
        local `var'`sample2' : di %12.2f  r(mean)
        local `var'change (`var'`sample1' - `var'`sample2') / `var'`sample2' * 100
        di "`var'change"
    
        
        svy: mean `var', over(year) 
        lincom [`var']`sample1'- [`var']`sample2'
        
        *mat L = (r(estimate), r(se),r(estimate)/r(se) , 2*ttail(r(df),abs(r(estimate)/r(se))) 
        local x : di  %9.0g  2*ttail(r(df),abs(r(estimate)/r(se)))
        local y: di %12.3f `x'
        local p`var' "`y'"
    }
    
    local j
    foreach var of varlist `indepvars5'  {
        local j = `j' + 1
        qui sum `var'  if `sample2' [aw=dwt]
        local mean`sample2' : di %12.2f r(mean)
        qui sum `var'  if `sample1' [aw=dwt]
        local mean`sample1' : di %12.2f r(mean)
        local meandiff : di %12.1f  (`mean`sample1'' - `mean`sample2'') / `mean`sample2'' * 100
        
        svy: mean `var', over(year) 
        lincom [`var']`sample1'- [`var']`sample2'
        
        local x : di  %9.0g  2*ttail(r(df),abs(r(estimate)/r(se)))
        local y: di %12.3f `x'
        local all`var'`j' "`mean`sample2'' & `mean`sample1'' & `meandiff' & p < `y' "
        di "`all`var'`j''"
    }

  • #2
    First, I repeat a reminder of our request to use real names. https://www.statalist.org/forums/for...acro-variables If "Max Planck" is your real name, it's fine to explain that (and so avoid requests of this kind).

    Although you report differently, I can't see that Stata would get past the second line in your code, which becomes

    Code:
     
     sum <some variable name here> [aw=dwt] if 2006 2008 2009 2010 2011 2012
    and while if 2006 is legal (just unlikely to be what you want) the 2008 [sic] is not legal at all.

    There could be an error before. We can't see what is in the local macro indepvars5 and whether it really does consist of a list of names of variables.

    I can't be confident exactly what you want here but if inlist(year, 2006, 2008, 2009, 2010, 2011, 2012) or if inrange(year, 2006, 2012) would have the merit of being legal.

    Comment

    Working...
    X