Thanks to Kit Baum, I have shared a new program
Following are related details:

[bysort varlist]: asgen newvar = exp [if] [in] [, weights(varname) by(varlist)]

where

.
This will divide the variable kstock on the variable invest before finding the weighted average mean.

We use the [if] qualifier to perform calculation on a subset of the data. Using [if] condition, the resulting variable will have missing values where the condition is not true. There might be circumstance where we want to use [if] without encountering missing values in the new variable. With

Now, let's find weighted average mean in each year for a combination of firms that are in the first quantile of mvalue (size == 1) and third quantile of kstock (kstock5 == 3) using mvalue as a weighting variable

Off-course, we could have done that using [if] qualifier, but that will generate missing values where the condition is not true

The difference between WM_invest and WM_invest_IF is that the former spreads the results within years, and the later does not. The use of this trick is borrowed from the behavior of egen. Further details related to this trick can be read here in Nick Cox's column.

*

Comments for improvement of

**asgen**on**SSC**that computes weighted average mean. The program can be installed byCode:

ssc install asgen

**Syntax**[bysort varlist]: asgen newvar = exp [if] [in] [, weights(varname) by(varlist)]

**Description****asgen**creates a new variable from an existing variable or an expression. The new variable contains weighted average mean. The existing variable is supplied to**asgen**by the**=exp**. The weights are based on the values of an existing numeric variable, that is specified in the**(**__w__eights*varname*) option.**asgen**accepts both the [if] [in] qualifiers for performing the required calculations on a subset of data. Further, the use of**=exp**can come handy in many situations where we want to make changes on the fly before we find the weighted average mean. For example, we might want to multiply the value of 100 to a variable before we find its weighted average. In such a case, the expression will look like:Code:

asgen wmX = X * 100, w(Y)

where

*is a new variable to be created and shall contain the weighted average;***wmX***and***X***are existing variables in the data set. Options***Y****w****eight***(varname)*is an optional option. Therefore, without this option,**asgen**works like**egen**command and finds simple mean**Example 1: Weighted average mean for kstock using the variable mvalue as a weight**Code:

webuse grunfeld asgen WM_kstock = kstock, w(mvalue)

**Example 2: Weighted average mean using an expression**.

Code:

asgen WM_kstock = kstock / invest, w(mvalue)

**Example 3: Avoiding the use of [if] using an expression**We use the [if] qualifier to perform calculation on a subset of the data. Using [if] condition, the resulting variable will have missing values where the condition is not true. There might be circumstance where we want to use [if] without encountering missing values in the new variable. With

**asgen**, the use of an expression can come very handy in such a s situation. To proceed with an example, let us use**astile**[can be downloaded from ssc by:**ssc install astile**] to make five groups of firms based on the variable mvalue and five groups based on kstock.Code:

astile size5 = mvalue, nq(5) astile kstock5 = kstock, nq(5)

Now, let's find weighted average mean in each year for a combination of firms that are in the first quantile of mvalue (size == 1) and third quantile of kstock (kstock5 == 3) using mvalue as a weighting variable

Code:

bys year: asgen WM_invest = (kstock / (size5 ==1 & kstock5==3)), w(mvalue)

Code:

bys year: asgen WM_invest_IF = kstock if size5 ==1 & kstock5==3 , w(mvalue) sort id year

*

Comments for improvement of

**asgen**are greatly appreciated.
## Comment