Thanks, Carlo. It's a good idea to begin programs with a -version #- statement, typically the version you are using, so that Stata knows what minimum version is needed to run the program and that it will run in future versions. There's nothing special her that requires version 17, so you can replace 17 with your version to get it to run (or simply omit the statement altogether for learning purposes, but not for serious work).
Note I also have a typo in the command and should be corrected with the text in red.
Note I also have a typo in the command and should be corrected with the text in red.
Code:
cap progam drop myprog
program define myprog, rclass
version 17
syntax varlist(max=1) [if] [in]
unab v : `varlist'
confirm numeric var `v'
marksample touse
tempname p10 p90 p10_mean p90_mean
qui summ `v' if `touse', det
scalar `p10' = r(p10)
scalar `p90' = r(p90)
return scalar p10 = `p10'
return scalar p90 = `p90'
qui mean `v' if `touse' & inrange(`v', ., `p10')
scalar `p10_mean' = r(table)["b",1]
return scalar p10_mean = `p10_mean'
qui mean `v' if `touse' & inrange(`v', `p90', .)
scalar `p90_mean' = r(table)["b",1]
return scalar p90_mean = `p90_mean'
end
Comment