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
Nick Cox <[email protected]>
To
[email protected]
Subject
Re: st: creating new id variable
Date
Tue, 28 Aug 2012 10:35:57 +0100
Second example should have been
gen newid = cond(id1 < 10, "0" + string(id1), string(id1)) + cond(id2
> < 10, "0" + string(id2), string(id2))
On Tue, Aug 28, 2012 at 8:58 AM, Nick Cox <[email protected]> wrote:
> If the two variables are string, this could be
>
> gen newid = cond(length(id1) == 1, "0" + id1, id1) + cond(length(id2)
> == 1, "0" + id2, id2)
>
> If the two variables are numeric, this could be
>
> gen newid = cond(id1 < 10, "0" + string(id1), string(id1)) + cond(id2
> < 10, "0" + id2, id2)
>
> although
>
> gen newid = 100 * id1 + id2
>
> might well prove adequate.
>
> Either way,
>
> egen newid = group(id1 id2), label
>
> is a good solution, as William signals. Note, however, that the use
> within -egen- of the -concat()- function (not subcommand) does not
> depend on the arguments being strings, as is documented.
>
> See also
>
> SJ-7-4 dm0034 . . . Stata tip 52: Generating composite categorical variables
> . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . N. J. Cox
> Q4/07 SJ 7(4):582--583 (no commands)
> tip on how to generate categorical variables using
> tostring and egen, group()
>
> which is accessible to all at
>
> http://www.stata-journal.com/article.html?article=dm0034
>
> Nick
>
> On Tue, Aug 28, 2012 at 3:43 AM, William Buchanan
> <[email protected]> wrote:
>
>> I would also recommend looking at the -egen- command. If the id variables are stored as strings you can use the -concat- subcommand to join them together, and if they are stored as numbers you can use the -group- subcommand to create a new variable based on the values of the two variables. Either way, you should definitely read more of the documentation and the resources available online.
>
>> On Aug 27, 2012, at 19:08, A Muhyidin <[email protected]> wrote:
>>
>>> Dear Statalisters,
>>>
>>> I am trying to make a new id based on the two previous id, but so far
>>> without any luck.
>>> The new id that I'd like to make should be 4 digits, 2 digits from id1
>>> and and 2 digits from id2.
>>> If id2 consists of 1 digit number, I would like to add zero before the
>>> last number to make new_id as 4 digits.
>>> Any help would be appreciated.
>>>
>>>
>>> | id1 id2| new_id
>>> |------------|
>>> 1. | 11 1 | 1101
>>> 2. | 11 5 | 1105
>>> 3. | 11 8 | 1108
>>> 4. | 11 9 | 1109
>>> 5. | 11 10 | 1110
>>> |------------|
>>> 6. | 11 14 | 1114
>>> 7. | 11 15 | 1115
>>> 8. | 11 16 | 1116
>>> 9. | 11 17 | 1117
>>> 10. | 11 18 | 1118
>>> +------------+
>>>
*
* 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/