I'm working on a program in Stata that requires the weighted percentile (I'm inclined to have the user set up first svyset
instead of allowing them pass the weight variable in my command), and I need to extract the variable name used in svyset
for weighting.
Is there a specific helper command available for this purpose?
MWE:
* SETUP
clear, all
set obs 1000
svyset, srs
gen w1 = _n
* Generate weights
capture drop wt1
gen wt1 = 1
replace wt1 = 10 if w1>500.5
gen wt2 = 10
replace wt2 = 1 if w1>500.5
* Svyset with weights
svyset [w=wt1]
.
.
.
Then, when I use my program, I need to recover wt1
from svyset
. This can happen after svyset
or after a bunch of lines.
If you type
return list
after the last command in your MWE (many thanks!), you should see something like this:So the variable you seek is stored in
r(wvar)
.For uninteresting reasons I am using Stata 15 at this instant. I will check later that this is still true in Stata 18.