I am trying to write an ado routine that creates a column that marks extreme values of a variable. But I cannot get it to work. It only works for a single variable.
Ideally, I would run the ado file on a list of variables, and give it arguments for the upper and lower cut-off percentile.
Any help is welcome!
Martien
Explanation:
My main variable should have values of -3 to +3, so -99 and 287 are clearly extreme value. These extreme values should be marked. See table below an attachment.
I want to rely on percentiles and use the sum,detail command. The result is the ado file that I show below, which unfortunately gives the error message ("varname is not a numeric variable and cannot be trimmed") if I want to apply it to 2 variables or more.
See the code for the ADO below:
program define trimmer
foreach var of local 0 {
capture confirm numeric variable `var'
if _rc==0 {
qui sum `var',detail
gen to_use_`var'= inrange(`var', r(p1), r(p99)) if `var'!=.
}
else di "`varname' is not a numeric variable and cannot be trimmed."
}
end
Example data, where the marker is to_use_Variable :
Variable; to_use_Variable
-99; 1
..
-2.8; 0
-1.3; 0
-1.9; 0
0.5; 0
2.0; 0
..
287; 1
Ideally, I would run the ado file on a list of variables, and give it arguments for the upper and lower cut-off percentile.
Any help is welcome!
Martien
Explanation:
My main variable should have values of -3 to +3, so -99 and 287 are clearly extreme value. These extreme values should be marked. See table below an attachment.
I want to rely on percentiles and use the sum,detail command. The result is the ado file that I show below, which unfortunately gives the error message ("varname is not a numeric variable and cannot be trimmed") if I want to apply it to 2 variables or more.
See the code for the ADO below:
program define trimmer
foreach var of local 0 {
capture confirm numeric variable `var'
if _rc==0 {
qui sum `var',detail
gen to_use_`var'= inrange(`var', r(p1), r(p99)) if `var'!=.
}
else di "`varname' is not a numeric variable and cannot be trimmed."
}
end
Example data, where the marker is to_use_Variable :
Variable; to_use_Variable
-99; 1
..
-2.8; 0
-1.3; 0
-1.9; 0
0.5; 0
2.0; 0
..
287; 1
Comment