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

st: RE: Re: Formatting variables


From   "Siyam,AA (pgr)" <A.A.Siyam@lse.ac.uk>
To   <statalist@hsphsun2.harvard.edu>
Subject   st: RE: Re: Formatting variables
Date   Thu, 13 Jun 2002 19:24:13 +0100

My great thanks to Mark Schaffer and Marcela Perticara, both offering perfect solutions.

Amani

-----Original Message-----
From: Marcela Perticara [mailto:perticara@econ.tamu.edu]
Sent: 13 June 2002 19:12
To: statalist@hsphsun2.harvard.edu
Subject: st: Re: Formatting variables


You can also use the substring function.

qui gen str9 hhid=substr(snid,1,1)+"0"+substr(snid,2,7)

Also keep in mind that you can check the length of the string in a previous
command, and only execute
this line for the observations or files where the string has eight digits
instead of nine.

-----Original Message-----
From: Mark Schaffer [mailto:M.E.Schaffer@hw.ac.uk]
Sent: 13 June 2002 18:55
To: statalist@hsphsun2.harvard.edu; Siyam,AA (pgr)
Subject: Re: st: Formatting variables


Amani,

Whenever you're dealing with identifiers, you should create variables 
as doubles.  What's happening to the last couple of digits may just 
be rounding error resulting from the fact that floats don't have 
enough digits of accuracy for what you're doing.

Cheers,
Mark 

----- Original Message -----
From: "Siyam,AA (pgr)" <A.A.Siyam@lse.ac.uk>
To: <statalist@hsphsun2.harvard.edu>
Sent: Thursday, June 13, 2002 12:51 PM
Subject: st: Formatting variables


> Dear Stata-listers,
>
> I am sorry to bother you with, but I have checked the FAQ contents and
have spent a few hours now trying to understand what STATA is doing with the
following:
>
> basically I have an identification variable "snid" that should be made-up
of 9 digits, arranged as follows
> digit       1 = Oblast no. (1 to 8)
> digits  2-3=rayon no. (01 to 09)
> digits  4-5=population point (01 to 55)
> digits  6-7=segment no. (01 to 18)
> digits  8-9=household number (01 to 20)
>
> Instead for some of the files I have, "snid" coming out with 8 digits as a
result of bad coding ( rayon number above should have been stored as 01 to
09 but instead was entered as 1 to 9).  What I thought I could do was the
following
>
> . destring id, gen(snid) float
> . format snid %9.0f
> . gen hhid=100000000 + (snid-10000000)
> . format hhid %9.0f
> . list snid hhid
>            snid           hhid
>    1.  11010101  101010104
>    2.  11010102  101010104
>    3.  11010103  101010104
>    4.  11010104  101010104
> ...
>   24.  11010401  101010400
>   25.  11010402  101010400
>   26.  11010403  101010400
>   27.  11010404  101010400
>   28.  11010405  101010408
>   29.  11010406  101010408
>
> I can see that "zero" has been added, I can't understand why is STATA
changing the last two digits of the newly created variable "hhid" .
>
> Many thanks for your help in advance.
>
> Amani
> *
> *   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/

*
*   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/
*
*   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