Announcement

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

  • How to access varname or varlist in programming?

    I am trying to run a cluster bootstrap to calculate the standard error for several variables in Stata. Here is my code. It appears that Stata can not access the variable or the weight related to the variable. The exact error message is "can not find site_weight" I will appreciate any tip on solving the problem.

    capture program drop bootstrap_se
    program bootstrap_se, rclass
    syntax varname [if] [in]
    local weight_var site_`varname'_weight // Construct weight variable name dynamically
    reghdfe `varname' treatment [pweight=`weight_var'], absorb(cohort_site week_type) noconstant
    return scalar effect=_b[treatment]
    end

    bootstrap r(effect), reps(100) cluster(cohort_site) idcluster(new_cohort_site) seed(1234) : bootstrap_se avg_labor_cost


    I also tried

    capture program drop bootstrap_se
    program bootstrap_se, rclass
    syntax varlist [if] [in]
    reghdfe `varlist'[1] treatment [pweight=`varlist'[2], absorb(cohort_site week_type) noconstant
    return scalar effect=_b[treatment]
    end

    bootstrap r(effect), reps(100) cluster(cohort_site) idcluster(new_cohort_site) seed(1234) : bootstrap_se avg_labor_cost site_avg_labor_cost_weight

  • #2
    Try substituting `varlist' for `varname':
    Code:
    syntax varname [if] [in]  // <= but not here
    local weight_var site_`varlist'_weight
    reghdfe `varlist' treatment [pweight=`weight_var'], absorb(cohort_site week_type) noconstant

    Comment


    • #3
      Thanks a lot. It works!

      Comment

      Working...
      X