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:56:56 +0000

Correct. Thanks for the fix. I fixed the code when testing it but did not correct my draft. 

Nick 
n.j.cox@durham.ac.uk 


-----Original Message-----
From: owner-statalist@hsphsun2.harvard.edu [mailto:owner-statalist@hsphsun2.harvard.edu] On Behalf Of daniel klein
Sent: 16 November 2011 10:48
To: statalist@hsphsun2.harvard.edu
Subject: Re: st: Combining strings by group

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/

*
*   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