Statalist The Stata Listserver

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

st: RE: data management problem

From   "Wanli Zhao" <>
To   <>
Subject   st: RE: data management problem
Date   Sun, 6 May 2007 19:58:17 -0400

Thanks a lot, Kit. I tried the solution and I think it works. Just wondering
why sometimes the uniqprod shows only partial of the product name. In my
example, the names are single letters but in my data they are words. It only
happens to some firms and always shows in the last word. For example,
"governance, audit, compensation, g". The "g" puzzles me.
In addition, if I have a string variable looks like "book, mirror, desk,
chair, book, chair, desk, wall, mirror". And the number of words and their
repeat vary across firms. How do I get the unique list of the variable for
each firm? The desired should be "book, mirror, desk, chair, wall". Sequence
does not matter. Any help is appreciated.


-----Original Message-----
[] On Behalf Of Kit Baum
Sent: Sunday, May 06, 2007 9:47 AM
Subject: st: data management problem

sacrificial lamb

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

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
An Introduction to Modern Econometrics Using Stata:

*   For searches and help try:

*   For searches and help try:

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