1
votes

I'm sorry if this is basic but I can't seem to sort this error out. I'm trying to tabout specific questions in a survey.

My code:

qui: ds Q3_9-Q3_11b, has(type numeric)

foreach i in `r(varlist)'  {
   qui: tabout `i' using "H:\Electricity1.xls", c(prop ci) f(3) svy append show(none) clab("`i'")
   }

The error is

Q3.10 invalid name

but why would this be an invalid name?

The variable name is Q3_10 and the label is Q3.10.

1

1 Answers

0
votes

Your example with concocted data works:

clear 
set more off

*----- example data -----

set obs 5

gen Q3_9 = 33
gen Q3_10 = "dog"
gen P4_5 = 56
gen Q3_11b = 24
gen weight = runiform()

svyset weight

qui: ds Q3_9-Q3_11b, has(type numeric)
display "`r(varlist)'"

*----- what you want -----

foreach i in `r(varlist)' { 
    qui: tabout `i' using "H:\Electricity1.xls", ///
        c(prop ci) f(3) svy append show(none) clab("`i'")
}

Preferable is

foreach i of varlist `r(varlist)' { 
    qui: tabout `i' using "H:\Electricity1.xls", ///
        c(prop ci) f(3) svy append show(none) clab(`i')
}

because you make explicit that your list contains variables. Additionally, Stata checks if the variables in the list exist, so any related error is caught before entering the loop.

(The clab() option works with quotes, but the help file indicates you don't need them.)

If you still have problems, run describe before the loop and edit your question to include Stata's output.