# Re: st: RE: data management problem

 From Maarten buis To statalist@hsphsun2.harvard.edu Subject Re: st: RE: data management problem Date Sun, 6 May 2007 10:14:01 +0100 (BST)

--- Wanli Zhao <zhaowl@temple.edu> wrote:
> The data looks like follows:
>
> firm	dept	product
> 1	a	a, b
> 1	b	c, r
> 1	c	d, a
> 2	d	d, f
> 2	e	I, y
> 2	f	g
>
> Firm 1 & 2 each has some departments. Each department has some
> products. I want to a variable which lists all the products a firm
> has (a, b, c, d, r for firm 1) and for firm 2 (d, f, I, y, g).

*----------- begin example --------------
set more off
drop _all

input firm	str1 dept	str4 product
1	a	"a, b"
1	b	"c, r"
1	c	"d, a"
2	d	"d, f"
2	e	"I, y"
2	f	"g"
end

/*value one for the first department in each firm*/
/*value two for the second department in each firm*/
/*etc*/
bys firm: gen deptnum = _n

/*store maximum number of department in local `Ndepts'*/
sum deptnum, meanonly
local Ndepts = r(max)

/*products of the first department are in variable products1*/
/*products of the second department are in variable products2*/
/*etc*/
reshape wide product dept, i(firm) j(deptnum)

/*loop over departments*/
gen prods = product1
forvalues i = 2/`Ndepts' {
replace prods = prods + ", " + product`i'
}

/*bring the data back into original long format*/
reshape long dept product, i(firm) j(deptnum)

/*show the result*/
list, clean
*-------------- end example ----------------
(For more on how to use examples I sent to the Statalist, see:
http://home.fsw.vu.nl/m.buis/stata/exampleFAQ.html )

Hope this helps,
Maarten

-----------------------------------------
Maarten L. Buis
Department of Social Research Methodology
Vrije Universiteit Amsterdam
Boelelaan 1081
1081 HV Amsterdam
The Netherlands

Buitenveldertselaan 3 (Metropolitan), room Z434

+31 20 5986715

http://home.fsw.vu.nl/m.buis/
-----------------------------------------

___________________________________________________________
Yahoo! Mail is the world's favourite email. Don't settle for less, sign up for