Notice: On April 23, 2014, Statalist moved from an email list to a forum, based at statalist.org.
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/