Notice: On April 23, 2014, Statalist moved from an email list to a forum, based at statalist.org.

# Re: st: Replace with returned results in svy loop

 From Kate Schneider To statalist@hsphsun2.harvard.edu Subject Re: st: Replace with returned results in svy loop Date Sun, 29 May 2011 16:40:24 -0700

```Also, we have amended our code based on your recommendations but are
getting a "type mismatch error"

Here is what we're trying:

foreach X of varlist \$fooditemall {
quietly count if `X'==1 & priceperkilo !=.
if `r(N)'>0 {
display "Variable is: " "`X'"
svy, subpop(`X'): mean priceperkilo
mat meanpriceperkilo = e(b)
scalar vmean = meanpriceperkilo[1,1]
}
quietly count if `X'==1 & priceperkilo ==.
if `r(N)'>0 {
replace priceperkilo = varmean if `X'==1 & priceperkilo ==.
}
}

Thanks so much for any advice!

On Sun, May 29, 2011 at 4:27 PM, Kate Schneider
<schneider.kate1@gmail.com> wrote:
> Thanks so much Richard,
>
> We are trying to monetize own consumption of food items, so the mean
> we are calculating is the average price reported by households who
> bought the food and we want to apply this to the missing data.
> Observations are missing where households ate food they produced
> themselves or did not report the price they paid for the item if they
> bought it.
>
> Is there a better way you recommend attributing these prices to the
> cases where we don't have a value? We were previously manually keeping
> track of the mean prices and then manually telling stata to replace
> (for example) price_eggs == \$1 if price_eggs==. We are trying to avoid
> manually going between excel and stata, thinking that it would be more
> valid to have stata do all of this for us.
>
> The other approach we were considering would be to create a new
> variable where every observation of eggs get the mean egg price value,
> and then when we calculate the total value of egg consumption to
> specify drawing from the reported price if not missing, and if missing
> use the new value. Does that sound like a more valid approach?
> Regardless when we use ereturn list, there isn't a scalar listed for
> our mean. We tried e(mean), but it returned an error. Do we have to
> tell stata to store e(mean) before our svy: mean calculation?
>
> The other thing we should specify is that our data is hierarchical
> with 2 unique identifiers, a household ID and an id for each of the 65
> different food items. We are trying to value total consumption per
> food item and also per household. We thought about collapsing food
> items to their mean price, but thought that probably wouldn't apply
> our survey weights (though perhaps stata is smarter than we are?).
>
> Thanks so much for your assistance, we are really grateful!
>
> Kate
>
> On Sun, May 29, 2011 at 5:11 PM, Richard Williams
> <richardwilliams.ndu@gmail.com> wrote:
>> If in doubt where a value is stored, type -ereturn list- after the
>> estimation command. After -mean-, e(b) has the mean of the variable. So, to
>> get the mean stored in a scalar, do something like
>>
>> webuse nhanes2f, clear
>> svy: mean houssiz
>> mat varmean = e(b)
>> scalar vmean = varmean[1,1]
>>
>> Having said that, substituting the mean for missing data is generally not
>> considered the best practice in the world. And, there may be other errors in
>> your code (e.g. the == in the replace command looks wrong) but I imagine
>> those will become obvious once you tidy other things up.
>>
>> At 03:11 PM 5/29/2011, Kate Schneider wrote:
>>>
>>> Hello,
>>> Sorr to bug everyone on a holiday weekend, but we're a graduate
>>> student research group at the University of Washington working with a
>>> large household survey dataset and have come up against some trouble.
>>>
>>> We're trying to replace missing values of a variable with the results
>>> of some other operation we've just performed (basically, a mean).
>>> Right now, we're having trouble with the replace component of the
>>> below operation. Does anyone know how torecall the previous command's
>>> results from the svy calculation and use that data to replace missing
>>> in a loop?  Is this possible with survey data?
>>>
>>> foreach X of varlist \$fooditemall {
>>> quietly count if `X'==1 & priceperkilo !=.
>>>  if `r(N)'>0 {
>>>  display "Variable is: " "`X'"
>>>  svy, subpop(`X'): mean priceperkilo
>>>  }
>>> quietly count if `X'==1 & priceperkilo ==.
>>>  if `r(N)'>0 {
>>>  replace priceperkilo == e(????) if `X'==1 & priceperkilo ==.
>>>  }
>>> }
>>>
>>> Kate
>>>
>>> *
>>> *   For searches and help try:
>>> *   http://www.stata.com/help.cgi?search
>>> *   http://www.stata.com/support/statalist/faq
>>> *   http://www.ats.ucla.edu/stat/stata/
>>
>> -------------------------------------------
>> Richard Williams, Notre Dame Dept of Sociology
>> OFFICE: (574)631-6668, (574)631-6463
>> HOME:   (574)289-5227
>> EMAIL:  Richard.A.Williams.5@ND.Edu
>> WWW:    http://www.nd.edu/~rwilliam
>>
>> *
>> *   For searches and help try:
>> *   http://www.stata.com/help.cgi?search
>> *   http://www.stata.com/support/statalist/faq
>> *   http://www.ats.ucla.edu/stat/stata/
>>
>

*
*   For searches and help try:
*   http://www.stata.com/help.cgi?search
*   http://www.stata.com/support/statalist/faq
*   http://www.ats.ucla.edu/stat/stata/
```