Statalist The Stata Listserver


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

st: data management problem


From   Kit Baum <[email protected]>
To   [email protected]
Subject   st: data management problem
Date   Sun, 6 May 2007 09:47:09 -0400

sacrificial lamb

With regard to Wanli Zhao's problem of counting the number of products per firm, using his original data:

list
tabstat firm, by(firm) stat(N)
reshape wide product, i(firm) j(dept) string
ds prod*
local allprod `r(varlist)'
g allprod=""
foreach v of local allprod {
qui replace allprod = allprod + `v' + " "
}

g uniqprod=""
g nprod=.
su firm,mean
forv i=1/`r(N)' {
local prodlist = allprod[`i']
local prodlist : subinstr local prodlist "," "",all
local uniqprod : list uniq prodlist
qui replace uniqprod = "`uniqprod'" in `i'
local nprod : word count `uniqprod'
qui replace nprod = `nprod' in `i'
}

list firm uniqprod nprod

+--------------------------+
| firm uniqprod nprod |
|--------------------------|
1. | 1 a b c r d 5 |
2. | 2 d f I y g 5 |
+--------------------------+




Kit Baum, Boston College Economics and DIW Berlin
http://ideas.repec.org/e/pba1.html
An Introduction to Modern Econometrics Using Stata:
http://www.stata-press.com/books/imeus.html


*
* 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–2024 StataCorp LLC   |   Terms of use   |   Privacy   |   Contact us   |   What's new   |   Site index