Hello,
I am a PhD student and I have not been using Stata that long (about a year) so my apologies if the answer to this question is obvious. I have been looking for an answer for why this is happening for several days but have not had any success on my own.
Recently, my faculty advisor recommended that I look at some national data from the National Survey on Drug Use and Health (NSDUH) as part of my dissertation work. He is out of the country and this is quite time sensitive. After completing my analyses, I was going through the data documentation regarding estimate suppression for potentially unreliable estimates. They include code for SUDAAN, Stata and SAS. I tried the suggested Stata code (see below) and continually get the error message 'counter invalid name' (see bolded part of code where things are going wrong for me).
Here is a link to the NSDUH documentation (see page 71): https://www.samhsa.gov/data/sites/de...erence2015.pdf
Here is the code provided producing the error message 'counter' invalid name:
use using “.\\dataname.dta”, clear /*Ensure all variables are lower case*/
rename *, lower /*ID Nesting variables (VESTR and VEREP) and weight variable (ANALWT - standard single-year, person-level analysis weight*/
svyset verep [pw=analwt], strata(vestr) dof(750)
gen total_out=.
gen setotal=.
gen mean_out=.
gen semean=.
gen nsum=.
gen wsum=.
gen deffmean=.
/* Estimated means of past month alcohol use by year and gender*/
/*Year variable, where 2013=1 & 2014=2*/
/*Gender variable, where male=1 & female=2*/
svy: mean alcmon, over(year irsex)
matrix M=e(b) /*Store mean estimates in matrix M*/
matrix S=e(V) /*Store variances in matrix S*/
matrix N=e(_N) /*Store sample size in matrix N*/
matrix W=e(_N_subp) /*Store weighted sample size in matrix W*/
estat effects, deff srssubpop /*Obtain design effect*/
matrix D=e(deff) /*Store design effect in matrix D*/
/*Extract values stored in the M, S, N, W, and D matrices defined above to the mean_out, semean, nsum, wsum, and deffmean variables. The loop ensures that the appropriate values are extracted for each value of year and gender.*/
local counter=1
forvalues i=1/2 { /*number of years*/
forvalues j=1/2 { /* number of gender categories*/
replace mean_out=(M[1,`counter’]) if year==`i’ & irsex==`j’
replace semean=(sqrt(S[`counter’,`counter’])) if year==`i’ & irsex==`j’
replace nsum=(N[1,`counter’]) if year==`i’ & irsex==`j’
replace wsum=(W[1,`counter’]) if year==`i’ & irsex==`j’
replace deffmean=(D[1,`counter’]) if year==`i’ & irsex==`j’
local counter=`counter’+1
}
}
Thank you in advance for any help or suggestions you can provide.
Related Posts with forvalues with counter
Manually estimating standard error of the random effect, after the mi estimate: mixed command, with remlHello, I am using mi estimate: mixed, with reml. In Stata 16.1 Following estimation of the model, I …
Missing values after appending in loopHello, I am appending 78 dta files using a loop. When I do this, some observations of the district v…
regexr - Regular Expression in StataHi everyone! I am working on some regular expression problem. The data I have is something like this…
diff command (rcs and bootstrap option) - results replication issue*Hi, I am new user to Stata forum. I have been working with a two year repeated cross-sectional data…
forest plot not plottingI think it might be related to the estimates store not properly working (in bold). I'd appreciate an…
Subscribe to:
Post Comments (Atom)
0 Response to forvalues with counter
Post a Comment