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

# Re: st: Calculate weighted average across variables with externally given weights - controlling for missing values

 From Nick Cox To statalist@hsphsun2.harvard.edu Subject Re: st: Calculate weighted average across variables with externally given weights - controlling for missing values Date Mon, 3 Oct 2011 08:08:09 +0100

```To get Stata to treat values of -x- as 0 if they are missing you can

cond(missing(x), 0, x)

or use -mvdecode-.

Nick

On Mon, Oct 3, 2011 at 2:16 AM, Andreas Karpf <andreas.karpf@gmail.com> wrote:

> I have a couple of time series variables for different industrial
> sectors like manufacturing, services industry, communication industry
> etc.
>
> t  ; var_sect_1 ; var_sect_2 ; var_sect_3 ; var_sect_4;
> jan ; ; ; ; ;
> feb ; ; ; ; ;
> mar ; ; ; ; ;
> apr ; ; ; ; ;
>
> What I want to do (example january):
>
> weight_avg_january= var_sect_1[jan] *weight_sect_1 +
> var_sect_2[jan]*weight_sect_2 + var_sect_3[jan]*weight_sect_3 +
> var_sect_4[jan]*weight_sect_4/(weight_sect_1+weight_sect_2+weight_sect_3+weight_sect_4)
>
> if there is however a missing value for january sector 1 it should look like:
>
> weight_avg_january= var_sect_2[jan]*weight_sect_2 +
> var_sect_3[jan]*weight_sect_3 +
> var_sect_4[jan]*weight_sect_4/(weight_sect_2+weight_sect_3+weight_sect_4)
>
> these data relates to a kind of business monitor survey and i would
> like to calculate the aggregate indicator by using sectorial weights,
> this means weights
> which correspond to the contribution of each sector (services,
> manufacturing) to the gdp. i at first though i could do that by hand
> but than i realized that 1) if there is
> one missing value in e.g. sector 1 in january stata outputs a missing
> value for the weighted average for january. so it doesn't just ignore
> the mv but it refuses to calculate the datapoints which are there. 2)
> even if problem number one would be solved of course the denominator
> would not be correct because if the sector 1 data in january is
> missing also the weight in the denominator for sector 1 should be
> omitted.
> The weights i am referring to are from an external statistic.

*
*   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/
```