Dear Statalisters,
I want to build a table that records prevalence of a variable at a varying hypothetical cut-offs. I used Stata's auto data here. My variable of interest is Headroom. And I have designed the cut-offs in such a way that they address all possible values of Headroom. I want to see the results by all possible cases of rep78.
My code looks like this:
Code:
sysuse auto, clear
tempname relation
postfile `relation' rep78 cutoff headroom_preval N using relation, replace
forval rep = 1(1)5 {
foreach h of numlist 1.5(0.5)5.1 {
loc n = `h'*10 /*because decimals cannot be used as part of a variable name*/
gen preval`n' = 0
replace preval`n' = 1 if head > `h'
qui sum preval`n' if rep = `rep' /*for each rep case*/
loc prevalbyrep = r(mean)
loc Nbyrep = r(N)
post `relation' (`rep') (`h') (`prevalbyrep') (`Nbyrep')
}
}
postclose `relation'
Any suggestion on how to fix this are highly appreciated.
Using Stata 15.1
0 Response to Using decimal numlist in foreach loop
Post a Comment