# st: RE: Using egen or similar

 From Lee Sieswerda To "'statalist@hsphsun2.harvard.edu'" Subject st: RE: Using egen or similar Date Fri, 22 Aug 2003 17:59:41 -0400

```I think this will give you pretty close to what you want.

* Reduce your master data to only the six
* variables of interest, and concatenate
* into a new variable
use your_master_data.dta
keep m1-m6
egen bincats = concat(m1-m6)
keep bincats
sort bincats
save temp1.dta, replace

* Generate a new dataset with all
* possible categories, in this case
* you've got 2^6 possible categories

clear
set obs 64
gen cat1 = 0
gen cat2 = 0
gen cat3 = 0

* Okay, this block is a hack job. I know
* there is a nice algorithm for
* generating all of the permutations
* of a binary outcome over n events, but
* I can't put my hands on it right now
replace cat1 = 1 in 33/64
replace cat2 = 1 in 17/32
replace cat2 = 1 in 49/64
replace cat3 = 1 in 9/16
replace cat3 = 1 in 25/32
replace cat3 = 1 in 41/48
replace cat3 = 1 in 57/64
egen cat4 = fill(0 0 0 0 1 1 1 1 0 0 0 0 1 1 1 1)
egen cat5 = fill(0 0 1 1 0 0 1 1)
egen cat6 = fill(0 1 0 1)
egen bincats = concat(cat1-cat6)

* Now generate the kind of
* labels you want, more or less
forvalues n = 1/6 {
replace cat`n' = cat`n'*`n'
}
egen numcats = concat(cat1-cat6)
keep numcats bincats

* Now merge your original data back into these
* categories
sort bincats
merge bincats using temp1.dta

* Drop the categories not needed
drop if _merge==1

* And tabulate
tab numcats

There may also be a clever way to do this with string functions rather than
with -merge-.

Regards,
Lee

Lee Sieswerda, Epidemiologist
Thunder Bay District Health Unit
Lee.Sieswerda@tbdhu.com

-----Original Message-----
From: siyama@who.int [mailto:siyama@who.int]
Sent: Friday, August 22, 2003 2:43 PM
To: statalist@hsphsun2.harvard.edu
Subject: st: Using egen or similar

Dear Stata-listers,

I have six binary variables (m1,..,m6) coded 0/1

I wish to see the frequency of the pattern of  occurring 1s, but preserving
the variable sequence.  I am not sure whether any of "egen" functions can
perform the task  (and I prefer not to use egen..=group(..)).

I started off with the following:

for X in num 2/6: recode mX 1=X

then if there is one of egen functions it would be

egen new=fun(m1 m2 m3 m4 m5 m6)

then tabulate new would yield

new		freq
------		------
1,2,3		63
1,3,4,5		23
1,4,5		30
2,5,6		20
.....etc

It is if I am "xtdes" but with variables rather than panel time-entries.

Many thanks for your help in advance.

Amani

*
*   For searches and help try:
*   http://www.stata.com/support/faqs/res/findit.html
*   http://www.stata.com/support/statalist/faq
*   http://www.ats.ucla.edu/stat/stata/
*
*   For searches and help try:
*   http://www.stata.com/support/faqs/res/findit.html
*   http://www.stata.com/support/statalist/faq
*   http://www.ats.ucla.edu/stat/stata/
```