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

st: RE: how to split numeric variable


From   "Daniel R Sabath" <sabathd@u.washington.edu>
To   <statalist@hsphsun2.harvard.edu>
Subject   st: RE: how to split numeric variable
Date   Wed, 24 Sep 2003 16:30:27 -0700

Since you really are not using the state variable as a numeric, convert it
to a string.

	tostring geocode, generate(str_geocode)

Then use the string processing functions to get what you want. In this case 
	gen state = substr(str_geocode,-2,2) /* -2 is 2 from the right side
for 2 characters */
	gen county = substr(str_geocode,-4,2)

The only problem you have is where the city code is less than 100. 

This pads the string out to 7 characters if it only has 6.
	replace str_geocode = "0" + str_geocode if length(str_geocode) == 6
Then
	gen city = substr(str_geocode,1,3)

More information can be had by typing "help substr" which will bring up help
on all the string functions.

-dan

-----Original Message-----
From: owner-statalist@hsphsun2.harvard.edu
[mailto:owner-statalist@hsphsun2.harvard.edu] On Behalf Of Wade T Roberts
Sent: Wednesday, September 24, 2003 2:14 PM
To: statalist@hsphsun2.harvard.edu
Subject: st: how to split numeric variable


Hi, I was hoping someone might be able to shed some light on this issue.

I have a single numeric variable that identifies the city/county/state for
each case, where the first three digits represent the city, the next two the
county, and the final two represent the state.

Examples:

 223406
1453209
2785845
etc...

I'm only interested in identifying cases by state at the moment.  How do I
go about singling out this part of the data, or creating a new identifying
state variable?

Thanks for any help.

Wade
*
*   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–2021 StataCorp LLC   |   Terms of use   |   Privacy   |   Contact us   |   What's new   |   Site index