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: "Siyam,AA (pgr)" <[email protected]>
To: <[email protected]>
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/