Statalist The Stata Listserver

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

RE: st: RE: data management problem

From   "Wanli Zhao" <>
To   <>
Subject   RE: st: RE: data management problem
Date   Sun, 6 May 2007 05:51:20 -0400

Thanks a lot, Maarten. I'll try later but I got the logic. By the way,
department info actually may be redundant because without dept column, story
holds. I do need the number of depts for each firm though.

-----Original Message-----
[] On Behalf Of Maarten buis
Sent: Sunday, May 06, 2007 5:14 AM
Subject: Re: st: RE: data management problem

--- Wanli Zhao <> 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"

/*value one for the first department in each firm*/
/*value two for the second department in each firm*/
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*/
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: )

Hope this helps,

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

visiting address:
Buitenveldertselaan 3 (Metropolitan), room Z434

+31 20 5986715

Yahoo! Mail is the world's favourite email. Don't settle for less, sign up
your free account today*
*   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