Retrieving the Weight Variable Name Following svyset

34 views Asked by At

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.

1

There are 1 answers

2
Nick Cox On BEST ANSWER

If you type return list after the last command in your MWE (many thanks!), you should see something like this:

. return list 

scalars:
             r(stages) =  1

macros:
           r(settings) : "_n [pweight= wt1], singleunit(missing) vce(linearized)"
                r(vce) : "linearized"
         r(singleunit) : "missing"
               r(wexp) : "= wt1"
               r(wvar) : "wt1"
              r(wtype) : "pweight"

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.