Bookmark and Share

Notice: On March 31, it was announced that Statalist is moving from an email list to a forum. The old list will shut down at the end of May, and its replacement, statalist.org is already up and running.


[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

RE: st: Combining strings by group


From   Nick Cox <n.j.cox@durham.ac.uk>
To   "'statalist@hsphsun2.harvard.edu'" <statalist@hsphsun2.harvard.edu>
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 
n.j.cox@durham.ac.uk 

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/


© Copyright 1996–2014 StataCorp LP   |   Terms of use   |   Privacy   |   Contact us   |   Site index