version: 2.0.0
help asgen July 29, 2020

Title

asgen - Generates a weighted average mean using an existing variable as a weight

Syntax

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

Description

asgen creates a new variable from an existing variable or an
expression. The new variable contains a 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 weight(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:

asgen wmX = X * 100, w(Y)

where wmX is a new variable to be created and shall contain
the weighted average; X and Y are existing variables in the data set.

Options

weight(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

. webuse grunfeld

. asgen WM_kstock = kstock, w(mvalue)

Example 2: Weighted average mean using an expression

. asgen WM_kstock = kstock / invest, w(mvalue)

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

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 circumstances where we want to use [if] without encountering
missing values in the new variable. The use of expression can come very handy
in such a situation. To proceed with an example, let us use astile
[can be downloaded from ssc by clicking here install
astile] to make five groups of firms based on mvalue and five groups based
kstock.

. 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

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

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

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

The difference between WM_invest and WM_invest_IF is that
the former spreads the results within years, and the latter 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.

ONLINE HELP

You may like to visit www.FinTechProfessor.com.
On this website, I regularly post blog posts and examples related to my programs.
I also maintain users' forums for these programs where users can post comments / questions / answers.
The link to the asgen's forum
is here

Author

::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: *
* *
* Dr. Attaullah Shah *
* Institute of Management Sciences, Peshawar, Pakistan *
* Email: attaullah.shah@imsciences.edu.pk *
* www.OpenDoors.Pk *
* www.FinTechProfessor.com *
*:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::*

Also see

asm : for momentum portfolios

asdoc : Easily create publication-quality tables from Stata

astile : for creating fastest quantile groups

asgen : for weighted average mean

asrol : for rolling-window statistics

asreg : for rolling-window, by-group, and Fama and MacBeth regressions

ascol : for converting asset returns and prices from daily to a weekly, monthly, quarterly, and yearly frequency

searchfor : for searching text in data sets