Notice: On April 23, 2014, Statalist moved from an email list to a forum, based at statalist.org.
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
RE: st: Combining strings by group
From 
 
Nick Cox <[email protected]> 
To 
 
"'[email protected]'" <[email protected]> 
Subject 
 
RE: st: Combining strings by group 
Date 
 
Wed, 16 Nov 2011 10:11:13 +0000 
It's easier than that. 
bysort code : replace type = type[_n-1] + "," + type if _n > 2
by code : replace type = type[_N] 
Nick 
[email protected] 
daniel klein
try a combination of -reshape- and -egen-'s -concat()- function.
. bys code : g byte j = _n
. reshape wide type ,i(code) j(j)
. egen combinedtype = concat(type*) ,p(" ")
. replace combinedtype = subinstr(combinedtype, " ", ", ", .)
. drop j type* // clean up
Ben Hoen 
Assume I had the following dataset with two variables:
code	type
1	a
1	c
2	e
3	f
3	g	
3	z
I want to create a third (string) variable that concatenates the string
"type" by code group, [...]
code	combinedtype
1	"a, c"
2	"e"
3	"f, g, z"
*
*   For searches and help try:
*   http://www.stata.com/help.cgi?search
*   http://www.stata.com/support/statalist/faq
*   http://www.ats.ucla.edu/stat/stata/