Statalist The Stata Listserver


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

Re: st: RE: data management problem


From   Maarten buis <[email protected]>
To   [email protected]
Subject   Re: st: RE: data management problem
Date   Sun, 6 May 2007 10:14:01 +0100 (BST)

--- Wanli Zhao <[email protected]> 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

visiting address:
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
your free account today http://uk.rd.yahoo.com/evt=44106/*http://uk.docs.yahoo.com/mail/winter07.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