Statalist The Stata Listserver


[Date Prev][Date Next][Thread Prev][Thread Next][Date index][Thread index]

Re: st: RE: Count presence of item on different vars [solved]


From   "Nikolaos A. Patsopoulos" <npatsop@cc.uoi.gr>
To   statalist@hsphsun2.harvard.edu
Subject   Re: st: RE: Count presence of item on different vars [solved]
Date   Mon, 09 Oct 2006 20:03:34 +0300

Nick Cox wrote:
Well, now you tell us. Possibly some variant on this:
quietly { foreach v of var v6-v146 { levelsof `v', local(this) local all : list all | this }

local all : list sort all
foreach text of local all { local count = 0 foreach v of var var6-v146 { count if `v' == "`text'" local count = `count' + r(N)
}
noi di as txt "`text' `count'" }
}
Nick n.j.cox@durham.ac.uk
Nikolaos A. Patsopoulos

Nick Cox wrote:

Check out -tabm- from -tab_chi- on SSC.
Alternatively, an approach from first
principles is to -reshape long- so that this reduces to a two-way tabulation.
Nikolaos A. Patsopoulos


I've got a set of variables v6-v146, each one containing string values (unique values ~4500):

v6 v7 v8 v9 v10...
text1 text1 text3 text45 text1...
text5 text2 text1 text455 text1
.....

I want to count how many times each of the values appears
in at least
one of the v6-v146 in each row [observation], e.g. text1
appears at
least once in 7365obs.

How can achieve this?

I tried :
tabm v7-v146

I had to kill the process after few minutes (it's a 150MB database)

Then used:

tabm v7-v8 // just to see what will come up but that's what I got:
too many values
r(134);

The unique values for each var is 4364 from a total of 25725 observations
*
* 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/


Just for the record, this worked for me fine:

gen countAppear
local y=0
forvalues i=1/88 {
local z=`y'+50
quietly {
levelsof v6 if (id_v6<=`z' & id_v6>`y'), local(this)
local all`i' : list all`i' | this
local all`i' : list sort all`i'

foreach text of local all`i' {
local count = 0
foreach v of var v7-v146 {
count if `v' == "`text'"
local count = `count' + r(N)
}
replace countAppear=`count' if v6=="`text'"
noi di as txt "`text' `count'"
}
noi di "y=`y' z=`z'"
}
local y=`z'
}




Thank you all for your help,

Nikos

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




© Copyright 1996–2014 StataCorp LP   |   Terms of use   |   Privacy   |   Contact us   |   What's new   |   Site index