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: creating new id variable


From   Nick Cox <njcoxstata@gmail.com>
To   statalist@hsphsun2.harvard.edu
Subject   Re: st: creating new id variable
Date   Tue, 28 Aug 2012 14:44:13 +0100

Good advice. See also

Cox, N.J. 2010. Stata tip 85: Looping over nonintegers. Stata Journal
10(1): 160-163


On Tue, Aug 28, 2012 at 12:18 PM, Joseph Coveney <jcoveney@bigplanet.com> wrote:
> Nick Cox wrote:
>
> Second example should have been
>
>  gen newid = cond(id1 < 10, "0" + string(id1), string(id1)) + cond(id2
>> < 10, "0" + string(id2), string(id2))
>
> --------------------------------------------------------------------------------
>
> Just to mention an alternative when converting numerical components to a single
> string ID, I like to use something like
>
>     generate str newid = string(id1, "02.0f") + string(id2, "02.0f")
>
> I find it a little easier to code.  Adjust the format expressions based upon the
> maxima of the numerical components, keeping in mind that as the size increases,
> a delimiter (hyphen or space) very much helps with readability:
>
>     generate str newid = string(id1, "03.0f") + "-" + string(id2, "04.0f")
>
> In addition, for concatenating string numerals, -substr()- might be a slightly
> easier alternative for those who come to Stata with experience of left() and
> right() string functions in other languages:
>
>     generate str newid = substr("0" + id1, -2, 2) + substr("0" + id2, -2, 2)
>
> Joseph Coveney
*
*   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