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" <>
To   <>
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
	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.


-----Original Message-----
[] On Behalf Of Wade T Roberts
Sent: Wednesday, September 24, 2003 2:14 PM
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.



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.

*   For searches and help try:

*   For searches and help try:

© Copyright 1996–2022 StataCorp LLC   |   Terms of use   |   Privacy   |   Contact us   |   What's new   |   Site index