Statalist The Stata Listserver


[Date Prev][Date Next][Thread Prev][Thread Next][Date index][Thread index]

st: RE: RE: how to split a variable?


From   "Nick Cox" <n.j.cox@durham.ac.uk>
To   <statalist@hsphsun2.harvard.edu>
Subject   st: RE: RE: how to split a variable?
Date   Wed, 7 Mar 2007 12:00:44 -0000

Using -destring- has one advantage here: if there are 
non-numeric characters in the variable, -destring-
will squawk. 

Otherwise if you know that there are no non-numeric 
characters, you can do it directly: 

gen house_no = real(substr(var,1,4))
gen indiv_no = real(substr(var,5,2))

You can test in advance that -var- is all numeric in content:  

assert real(var) < . 

If any value of -var- contains non-numeric characters, 
the brute force application of -real()- will yield missing. 
Conversely, all is well if all the results of -real()-
are non-missing numeric values. 

Nick 
n.j.cox@durham.ac.uk 

Rodrigo Martell
 
> try this (I'll assume your variable is called "var"):
> 
> gen house_no = substr(var,1,4)
> gen indiv_no = substr(var,5,2)
> destring house_no, replace
> destring gen indiv_no, replace
> 
> You can have a look at other string functions in the [D] 
> manual under "functions" or just by typing "help string functions"
jeongy@onid.orst.edu
 
> I have a string variable with 6 characters wide. The first 
> four characters are
> household numbbers, and the last two are the individual 
> members in a household.
> I want two variables, namely, the household and the 
> individual. Eventually, I
> want to make them numerical. So, being string variable isn't 
> important.

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