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

st: Re: converting a string variable that has leading and embedded blanks to a numeric variable.


From   "Joyce Chen" <chen20@fas.harvard.edu>
To   <statalist@hsphsun2.harvard.edu>
Subject   st: Re: converting a string variable that has leading and embedded blanks to a numeric variable.
Date   Tue, 8 Feb 2005 13:51:59 -0500

You could try using the subinstr command, which can substitute the blanks with zeros. I think you would want:

gen newhhid=subinstr(hhid," ","0",.)

and you could use destring or real(newhhid) to create a numeric variable.

----- Original Message ----- From: "Ali Murat" <alimuratberker@gmail.com>
To: <statalist@hsphsun2.harvard.edu>
Sent: Tuesday, February 08, 2005 1:33 PM
Subject: st: converting a string variable that has leading and embedded blanks to a numeric variable.



Hello all,

I am trying to convert a string household indentification variable
,that has leading and embedded blanks, to a numeric one. When I used
the destring command, the new numeric household identification number
has no longer unique values due to leading and embedded blanks in the
orıginal string household identification variable. To be more
precise, I would like to document what I have done so far:



gen newhhid=hhid

destring newhhid, replace ignore(" ")

sort newid

l hhid newhhid



Obsnumber hhid newhhid

161. | 21 1 211 |

162. | 21 2 212 |

163. | 212 212 |

164. | 213 213 |

165. | 21 3 213 |

|------------------------|

166. | 214 214 |

167. | 21 4 214 |



Please note that the observation 162 and 163 have a distinct hhid; the
fist has an embedded blankd, the second has a leading blank. However,
after using the destring command, they have same hhid number.

How can I fix this problem.

Thank you all for your kind concern and help.

Best regards.



Ali M. Berker

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