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 06:17:20 -0400

I tried the solution and I got some errors. First it says local option not
allowed. Then I change the Ndepts to a variable I have which is the number
of departments for each firm. Still does not work. Also it says my dataset
is already wide.
It also seems that the code does not take into account of repeated values in
products. I am not sure about blanks in product column. Any suggestions?

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