*! version 1.0.2 26Jul2015
help genstackGregorio Impavido

Title

genstack -- Utility to generate stacked variables

Syntax

genstack varlist,
generate(stub)
[double]

generate(stub) The mandatory option is needed to
generate the stacked variables whose name starts with stub.

double forces genstack to generate variables with double rpecision.

Description

genstack generates variables that can be displayed in a graph twoway
bar chart.

STATA can only produce oneway stacked charts. These however, do not allow you to
layer other charts. Indeed, STATA is notoriously unfriendly in producing a twoway
stacked chart where you have the freedom of layering different chart styles.
genstack helps you prepare the data for such charts.

Version 1.0.1 of genstack now copies the labels of original variables to the
stacked variables for better identification.

Version 1.0.2 of genstack now uses only a stub in the option generate()
and checks that the the names of the stacked variables generated are permissible.

Examples

Assume you want to display k=4 variables (genstack can do this for an
unlimited k but typically, anything with k>4 produces rather confusing charts)
with positive and/or negative observations adding up to some total as stacked bars
and then superimpose the total as line and/or dots. For all positive and negative
observations separastely, genstack calculates for you the cumulative
contribution of each variable to the row total. It then generates the cumulative
variables in such an order so that they do not hide each other wahen layered
in a graph twoway.

The following is a basic example with k=4:

********* start code here
clear all
local k 4 // Number of vars in the chart}
set obs 20
gen n = _n
forval i = 1/`k' {
gen v`i' = invnormal(uniform())
label var v`i' "var `i'"
}
egen total = rowtotal(v*)
genstack v1 v2 v3 v4, generate(c_) double
graph twoway (bar c_v4 c_v3 c_v2 c_v1 n) (scatter total n) (line total n)
********* end code here

NB: the order of the variables in -graph twoway- must be inverse of the order
used in -genstack-. In this example: (v1 v2 v3 v4) -> (c_v4 c_v3 c_v2 c_v1).

Authors

Gregorio Impavido (gimpavido@imf.org)