Bookmark and Share

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

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: st: Create a row matrix with weighted frequencies from various dummy variables

From   Maarten Buis <>
Subject   Re: st: Create a row matrix with weighted frequencies from various dummy variables
Date   Fri, 1 Jul 2011 13:01:10 +0200

2011/7/1 Nikolaos Kanellopoulos:
> I have two sets of dummy variables (say d1,...,d20 and z1,...,z5) and I want to
> create a 1x100 matrix where each element will be the weighted frequency for each
> combination between each d-variable and each z-variable when both equal 1

I think the easiest way to achieve this goal is -collapse- your data.
In the example below I used the trick that a frequency is the sum of a
"variable" containing only 1s to get the weighted frequency. You can
use -mkmat- to turn this into a matrix. You can precede this with
-preserve- and end it with -restore- to keep access to the original

*------------ begin example ----------------
set seed 10235
drop _all
set obs 1000
/*Generate data*/
gen d = ceil(20*uniform())
gen z = ceil(5*uniform())
generate wt = uniform()

gen byte one = 1
collapse (sum) one [pw=wt], by(d z)
*--------------- end example ----------------
(For more on examples I sent to the Statalist see: )

Hope this helps,

PS. I presume that the d and z dummies in your example were supposed
to represent the different categories in two categorical variables (d
and z). In that case the dummies should be mutually exclusive and
exhaustive and the way you simulated your data did not achieve that
goal. I used tricks from (Buis 2007) to generate these categorical

Maarten Buis (2007) "Stata tip 48: Discrete uses for uniform()" The
Stata Journal, 7(3): 434-435.

Maarten L. Buis
Institut fuer Soziologie
Universitaet Tuebingen
Wilhelmstrasse 36
72074 Tuebingen

*   For searches and help try:

© Copyright 1996–2018 StataCorp LLC   |   Terms of use   |   Privacy   |   Contact us   |   Site index