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   daniel klein <klein.daniel.81@googlemail.com>
To   statalist@hsphsun2.harvard.edu
Subject   Re: st: Combining strings by group
Date   Wed, 16 Nov 2011 11:48:02 +0100

Smart code, Nick. I especially like the idea of using -by- instead of
-bysort- in the second line, since the data is already sorted. I
usually use -bysort- without thinking about it's need. However, I
think there is one typo in the code.

I guess the first line

bysort code : replace type = type[_n-1] + "," + type if _n > 2

should read

bysort code : replace type = type[_n-1] + ", " + type if _n > 1

Since the required data structure only has one row per case, I would
further replace

by code : replace type = type[_N]

with

by code : keep if (_n == _N)

(Ben, to perfectly match your example, you will need an additional -rename-.)

Best
Daniel

--
It's easier than that.

bysort code : replace type = type[_n-1] + "," + type if _n > 2
by code : replace type = type[_N]
*
*   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