Notice: On April 23, 2014, Statalist moved from an email list to a forum, based at statalist.org.
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: st: creating new id variable
From
"Joseph Coveney" <[email protected]>
To
<[email protected]>
Subject
Re: st: creating new id variable
Date
Tue, 28 Aug 2012 20:18:59 +0900
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/