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

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

 From Nikolaos Kanellopoulos To statalist@hsphsun2.harvard.edu Subject Re: st: Create a row matrix with weighted frequencies from various dummy variables Date Mon, 4 Jul 2011 07:34:30 +0100 (BST)

```Nick and Maarten,

thank you very much for your help.

Regards
Nikolaos

----- Original Message ----
From: Maarten Buis <maartenlbuis@gmail.com>
To: statalist@hsphsun2.harvard.edu
Sent: Fri, July 1, 2011 2:01:10 PM
Subject: Re: st: Create a row matrix with weighted frequencies from various
dummy variables

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
data.

*------------ begin example ----------------
clear
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:
http://www.maartenbuis.nl/example_faq )

Hope this helps,
Maarten

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
variables.

Maarten Buis (2007) "Stata tip 48: Discrete uses for uniform()" The
Stata Journal, 7(3): 434-435.
<http://www.stata-journal.com/article.html?article=pr0032>

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

http://www.maartenbuis.nl
--------------------------

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

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