Stata The Stata listserver
[Date Prev][Date Next][Thread Prev][Thread Next][Date index][Thread index]

Re: st: String problem.


From   Ulrich Kohler <kohler@wz-berlin.de>
To   statalist@hsphsun2.harvard.edu
Subject   Re: st: String problem.
Date   Wed, 7 Sep 2005 17:08:18 +0200

Maybe something along the line of:

. gen hhid2 = hv001*100 + hv002
. tostring hhid2, replace
. replace hhid2 = subinstr(hhid2,"0"," ",.)

regards,
uli


adiallo5@worldbank.org wrote:
> Hi,
> I have problem generating a hid variable.
> I want to reproduce an exact clone of my
> hhid. It is 12 characters long.
> I wish somebody out there could help me.
> (A copy and paste in notepad would produce
> better tables).
>
> . use amhr41rt, clear
>
> . li hhid  hv001 hv002 hv003 hv021 hv022 in 1/20
>      +------------------------------------------------------+
>
>      |         hhid   hv001   hv002   hv003   hv021   hv022 |
>      |------------------------------------------------------|
>
>   1. |         1  2       1       2       1       1       1 |
>   2. |         1  3       1       3       1       1       1 |
>   3. |         1  5       1       5       2       1       1 |
>   4. |         1  6       1       6       1       1       1 |
>   5. |         1  7       1       7       2       1       1 |
>
>      |------------------------------------------------------|
>
>   6. |         1  8       1       8       1       1       1 |
>   7. |         1  9       1       9       1       1       1 |
>   8. |         1 10       1      10       1       1       1 |
>   9. |         1 11       1      11       2       1       1 |
>  10. |         1 12       1      12       1       1       1 |
>
>      |------------------------------------------------------|
>
>  11. |         1 13       1      13       1       1       1 |
>  12. |         1 14       1      14       2       1       1 |
>  13. |         1 15       1      15       2       1       1 |
>  14. |         1 16       1      16       1       1       1 |
>  15. |         1 17       1      17       2       1       1 |
>
>      |------------------------------------------------------|
>
>  16. |         1 18       1      18       2       1       1 |
>  17. |         1 19       1      19       3       1       1 |
>  18. |         1 20       1      20       2       1       1 |
>  19. |         1 21       1      21       2       1       1 |
>  20. |         1 22       1      22       1       1       1 |
>      +------------------------------------------------------+
>
> . tostring hv001 hv002, replace
> hv001 was double now str3
> hv002 was double now str2
> . g hhid2 = hv001+hv002
> . li hhid hhid2 hv001 hv002 hv003 hv021 hv022 in 1/20
>      +--------------------------------------------------------------+
>
>      |         hhid   hhid2   hv001   hv002   hv003   hv021   hv022 |
>      |--------------------------------------------------------------|
>
>   1. |         1  2      12       1       2       1       1       1 |
>   2. |         1  3      13       1       3       1       1       1 |
>   3. |         1  5      15       1       5       2       1       1 |
>   4. |         1  6      16       1       6       1       1       1 |
>   5. |         1  7      17       1       7       2       1       1 |
>
>      |--------------------------------------------------------------|
>
>   6. |         1  8      18       1       8       1       1       1 |
>   7. |         1  9      19       1       9       1       1       1 |
>   8. |         1 10     110       1      10       1       1       1 |
>   9. |         1 11     111       1      11       2       1       1 |
>  10. |         1 12     112       1      12       1       1       1 |
>
>      |--------------------------------------------------------------|
>
>  11. |         1 13     113       1      13       1       1       1 |
>  12. |         1 14     114       1      14       2       1       1 |
>  13. |         1 15     115       1      15       2       1       1 |
>  14. |         1 16     116       1      16       1       1       1 |
>  15. |         1 17     117       1      17       2       1       1 |
>
>      |--------------------------------------------------------------|
>
>  16. |         1 18     118       1      18       2       1       1 |
>  17. |         1 19     119       1      19       3       1       1 |
>  18. |         1 20     120       1      20       2       1       1 |
>  19. |         1 21     121       1      21       2       1       1 |
>  20. |         1 22     122       1      22       1       1       1 |
>      +--------------------------------------------------------------+
>
> . g hhid3 = "hv001"+" "+"hv002"
> . li hhid* hv001 hv002 hv003 hv021 hv022 in 1/20
>
> +--------------------------------------------------------------------------
>--+
>
>      |         hhid   hhid2         hhid3   hv001   hv002   hv003   hv021
>
> hv022 |
>
> |--------------------------------------------------------------------------
> |--|
>
>   1. |         1  2      12   hv001 hv002       1       2       1       1
> 1 |
>   2. |         1  3      13   hv001 hv002       1       3       1       1
> 1 |
>   3. |         1  5      15   hv001 hv002       1       5       2       1
> 1 |
>   4. |         1  6      16   hv001 hv002       1       6       1       1
> 1 |
>   5. |         1  7      17   hv001 hv002       1       7       2       1
> 1 |
>
> |--------------------------------------------------------------------------
> |--|
>
>   6. |         1  8      18   hv001 hv002       1       8       1       1
> 1 |
>   7. |         1  9      19   hv001 hv002       1       9       1       1
> 1 |
>   8. |         1 10     110   hv001 hv002       1      10       1       1
> 1 |
>   9. |         1 11     111   hv001 hv002       1      11       2       1
> 1 |
>  10. |         1 12     112   hv001 hv002       1      12       1       1
> 1 |
>
> |--------------------------------------------------------------------------
> |--|
>
>  11. |         1 13     113   hv001 hv002       1      13       1       1
> 1 |
>  12. |         1 14     114   hv001 hv002       1      14       2       1
> 1 |
>  13. |         1 15     115   hv001 hv002       1      15       2       1
> 1 |
>  14. |         1 16     116   hv001 hv002       1      16       1       1
> 1 |
>  15. |         1 17     117   hv001 hv002       1      17       2       1
> 1 |
>
> |--------------------------------------------------------------------------
> |--|
>
>  16. |         1 18     118   hv001 hv002       1      18       2       1
> 1 |
>  17. |         1 19     119   hv001 hv002       1      19       3       1
> 1 |
>  18. |         1 20     120   hv001 hv002       1      20       2       1
> 1 |
>  19. |         1 21     121   hv001 hv002       1      21       2       1
> 1 |
>  20. |         1 22     122   hv001 hv002       1      22       1       1
> 1 |
>
> +--------------------------------------------------------------------------
>--+ . g hhid4 = hv001+" "+hv002
> . li hhid* hv001 hv002 hv003 hv021 hv022 in 1/20
>
> +--------------------------------------------------------------------------
>----------+
>
>      |         hhid   hhid2         hhid3   hhid4   hv001   hv002   hv003
>
> hv021   hv022 |
>
> |--------------------------------------------------------------------------
> |----------|
>
>   1. |         1  2      12   hv001 hv002     1 2       1       2       1
> 1       1 |
>   2. |         1  3      13   hv001 hv002     1 3       1       3       1
> 1       1 |
>   3. |         1  5      15   hv001 hv002     1 5       1       5       2
> 1       1 |
>   4. |         1  6      16   hv001 hv002     1 6       1       6       1
> 1       1 |
>   5. |         1  7      17   hv001 hv002     1 7       1       7       2
> 1       1 |
>
> |--------------------------------------------------------------------------
> |----------|
>
>   6. |         1  8      18   hv001 hv002     1 8       1       8       1
> 1       1 |
>   7. |         1  9      19   hv001 hv002     1 9       1       9       1
> 1       1 |
>   8. |         1 10     110   hv001 hv002    1 10       1      10       1
> 1       1 |
>   9. |         1 11     111   hv001 hv002    1 11       1      11       2
> 1       1 |
>  10. |         1 12     112   hv001 hv002    1 12       1      12       1
> 1       1 |
>
> |--------------------------------------------------------------------------
> |----------|
>
>  11. |         1 13     113   hv001 hv002    1 13       1      13       1
> 1       1 |
>  12. |         1 14     114   hv001 hv002    1 14       1      14       2
> 1       1 |
>  13. |         1 15     115   hv001 hv002    1 15       1      15       2
> 1       1 |
>  14. |         1 16     116   hv001 hv002    1 16       1      16       1
> 1       1 |
>  15. |         1 17     117   hv001 hv002    1 17       1      17       2
> 1       1 |
>
> |--------------------------------------------------------------------------
> |----------|
>
>  16. |         1 18     118   hv001 hv002    1 18       1      18       2
> 1       1 |
>  17. |         1 19     119   hv001 hv002    1 19       1      19       3
> 1       1 |
>  18. |         1 20     120   hv001 hv002    1 20       1      20       2
> 1       1 |
>  19. |         1 21     121   hv001 hv002    1 21       1      21       2
> 1       1 |
>  20. |         1 22     122   hv001 hv002    1 22       1      22       1
> 1       1 |
>
> +--------------------------------------------------------------------------
>----------+ . g l1=length(hhid)
> . g l2=length(hhid4)
> . su l1 l2
>     Variable |       Obs        Mean    Std. Dev.       Min        Max
> -------------+--------------------------------------------------------
>           l1 |      5980          12           0         12         12
>           l2 |      5980    5.211204    .7336612          3          6
> . g str12 hhid5 = hv001+" "+hv002
> . g l3=length(hhid5)
> . li hhid* hv001 hv002 hv003 hv021 hv022 in 1/20
>
> +--------------------------------------------------------------------------
>------------------+
>
>      |         hhid   hhid2         hhid3   hhid4   hhid5   hv001   hv002
>
> hv003   hv021   hv022 |
>
> |--------------------------------------------------------------------------
> |------------------|
>
>   1. |         1  2      12   hv001 hv002     1 2     1 2       1       2
> 1       1       1 |
>   2. |         1  3      13   hv001 hv002     1 3     1 3       1       3
> 1       1       1 |
>   3. |         1  5      15   hv001 hv002     1 5     1 5       1       5
> 2       1       1 |
>   4. |         1  6      16   hv001 hv002     1 6     1 6       1       6
> 1       1       1 |
>   5. |         1  7      17   hv001 hv002     1 7     1 7       1       7
> 2       1       1 |
>
> |--------------------------------------------------------------------------
> |------------------|
>
>   6. |         1  8      18   hv001 hv002     1 8     1 8       1       8
> 1       1       1 |
>   7. |         1  9      19   hv001 hv002     1 9     1 9       1       9
> 1       1       1 |
>   8. |         1 10     110   hv001 hv002    1 10    1 10       1      10
> 1       1       1 |
>   9. |         1 11     111   hv001 hv002    1 11    1 11       1      11
> 2       1       1 |
>  10. |         1 12     112   hv001 hv002    1 12    1 12       1      12
> 1       1       1 |
>
> |--------------------------------------------------------------------------
> |------------------|
>
>  11. |         1 13     113   hv001 hv002    1 13    1 13       1      13
> 1       1       1 |
>  12. |         1 14     114   hv001 hv002    1 14    1 14       1      14
> 2       1       1 |
>  13. |         1 15     115   hv001 hv002    1 15    1 15       1      15
> 2       1       1 |
>  14. |         1 16     116   hv001 hv002    1 16    1 16       1      16
> 1       1       1 |
>  15. |         1 17     117   hv001 hv002    1 17    1 17       1      17
> 2       1       1 |
>
> |--------------------------------------------------------------------------
> |------------------|
>
>  16. |         1 18     118   hv001 hv002    1 18    1 18       1      18
> 2       1       1 |
>  17. |         1 19     119   hv001 hv002    1 19    1 19       1      19
> 3       1       1 |
>  18. |         1 20     120   hv001 hv002    1 20    1 20       1      20
> 2       1       1 |
>  19. |         1 21     121   hv001 hv002    1 21    1 21       1      21
> 2       1       1 |
>  20. |         1 22     122   hv001 hv002    1 22    1 22       1      22
> 1       1       1 |
>
> +--------------------------------------------------------------------------
>------------------+ . assert hhid==hhid5
> 5980 contradictions in 5980 observations
> assertion is false
> r(9);
>
> . su l1 l2 l3
>     Variable |       Obs        Mean    Std. Dev.       Min        Max
> -------------+--------------------------------------------------------
>           l1 |      5980          12           0         12         12
>           l2 |      5980    5.211204    .7336612          3          6
>           l3 |      5980    5.211204    .7336612          3          6
>
> Best regards.
> Amadou.
>
> *
> *   For searches and help try:
> *   http://www.stata.com/support/faqs/res/findit.html
> *   http://www.stata.com/support/statalist/faq
> *   http://www.ats.ucla.edu/stat/stata/

-- 
kohler@wz-berlin.de
+49 (030) 25491-361
*
*   For searches and help try:
*   http://www.stata.com/support/faqs/res/findit.html
*   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   |   What's new   |   Site index