Reputation: 11
I am trying to find outliers in a variable called nbrs
, generating an interquartile (iqr) range called nbrs_iqr
. I then want to loop (for practice with this looping concept) the values 1.5, 2, 5, and 10 in to multiply them by the iqr.
I keep getting a syntax error (invalid syntax r(198);) on the loop. I have seen something about not being able to do the forvalues loop when the values are not a range but have seen examples where it is a non-range without being explicit that that is permitted. I figured the spaces worked to separate the non-range values but I've thrown up my hands from there.
sum nbrs, detail
return list
gen nbrs_iqr = r(p75)-r(p25)
tab nbrs_iqr
forvalues i = 1.5 2 5 10 {
gen nbrs_out_`i'=`i'*nbrs_iqr
}
Upvotes: 1
Views: 1976
Reputation: 11112
help forvalues
is clear on the syntax you can use. Yours is not a valid range. You can work with foreach
, but notice that a .
in a variable name is not allowed.
One solution is to use strtoname()
:
clear
set more off
sysuse auto
keep price
sum price, detail
gen nbrs_iqr = r(p75)-r(p25)
foreach i of numlist 1.5 2 5 10 {
local newi = strtoname("`i'")
gen nbrs_out`newi' = `i' * nbrs_iqr
}
describe
My advice: familiarize yourself with help help
.
Upvotes: 3