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: Problem with creating new ID with other ID variables


From   Nick Cox <njcoxstata@gmail.com>
To   "statalist@hsphsun2.harvard.edu" <statalist@hsphsun2.harvard.edu>
Subject   Re: st: Problem with creating new ID with other ID variables
Date   Mon, 10 Jun 2013 17:46:01 +0100

You messed up by not specifying a variable type  for the new variable
-CID-. Presumably -CID- is float and there are not enough bits in a
-float- to hold every digit you want. Note that setting the -format-
will display the problem, not fix it. (Also, please use full real
names on Statalist, as requested.)

There is, in my view- a better approach:

egen CID = group(EA h1aq4 h1aq3 h1aq2 h1aq1), label

See

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()

http://www.stata-journal.com/article.html?article=dm0034 for free .pdf

Nick
njcoxstata@gmail.com


On 10 June 2013 17:37, nola l <nola0908@gmail.com> wrote:

> I have a data set like below:
>      +------------------------------------+
>       | h1aq1   h1aq2   h1aq3   h1aq4   EA |
>       |------------------------------------|
>    1. |   101       2       4       2    2 |
>    2. |   101       2       4       2    2 |
>    3. |   102       .       .       .    . |
>    4. |   101       2       4       2    2 |
>    5. |   101       2       4       2    2 |
>       |------------------------------------|
>    6. |   106       .       .       .    . |
>    7. |   102       1       1       4    1 |
>    8. |   102       1       1       4    1 |
>    9. |   102       1       1       4    1 |
>   10. |   102       1       1       4    1 |
>       |------------------------------------|
>   11. |   102       1       1       4    1 |
>   12. |   102       1       1       4    1 |
>   13. |   102       1       1       8    2 |
>   14. |   102       1       1       8    2 |
>   15. |   102       1       1       8    2 |
> and I would like to generate a ID which should looks like
> CID=EA+ h1aq4*100+ h1aq3*10000+ h1aq2*1000000+ h1aq1*100000000
>
> which means that CID for record 1 is 10102040202.
>
> When I use following command in Stata12:
> gen CID=EA+ h1aq4*100+ h1aq3*10000+ h1aq2*1000000+ h1aq1*100000000
>
> format %16.0g CID
>
> I got CID=10102040576 for record 1.
>
> Where did I mess up? Could you guys help me out on this and tell me
> how to could create correct ID?
*
*   For searches and help try:
*   http://www.stata.com/help.cgi?search
*   http://www.stata.com/support/faqs/resources/statalist-faq/
*   http://www.ats.ucla.edu/stat/stata/


© Copyright 1996–2014 StataCorp LP   |   Terms of use   |   Privacy   |   Contact us   |   Site index