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

st: RE: splitting a numeric string


From   "Nick Cox" <[email protected]>
To   <[email protected]>
Subject   st: RE: splitting a numeric string
Date   Mon, 27 Jan 2003 10:00:53 -0000

Steven Stillman (LMPG)
> 
> 
> Hi.  I have a string variable which contains information on 
> a maximum of 6
> choices made (of education).  The choices are numbered 1-10 
> and are a
> running string.  They can be in any order and can be 
> repeated.  I want to
> create 6 variables choice1, choice2, ... choice6.  The 
> problem I am running
> into is that all numbers are 1 digit besides 10 
> (fortunately 0 is not a
> choice) and 10 can be located anywhere in the string.  
> 
> Here is a snippet of what I have and what I am trying to do.
> 
>                   ed			choice1		choice2
> choice3  etc.
>    54.         54			5		4	
> 	.
>    65.         89			8		9	
> 	.
>    66.          5			5		.	
> 	.
>    67.          1			1		.	
> 	.
>    78.        932			9		3	
> 	2
>    81.         10			10		.	
> 	.
>    97.          8			8		.	
> 	.
>  103.	    105410		10		5		4
> 
> 

I assume Stata 8. 

First, replace all occurrences of "10" by any other 
single character, say "x". 

gen ed2 = ed 
replace ed2 = subinstr(ed2,"10","x",.) 

Now split by single characters, but 
reverse the "10" -> "x" mapping: 

forval i = 1/6 { 
	gen choice`i' = substr(ed2,`i',1) 
	replace choice`i' = "10" if choice`i' == "x" 
} 

That could be 

	gen choice`i' = 
cond(substr(ed2,`i',1 == "x"), "10", substr(ed2,`i',1)) 

In Stata < 8, you need to specify a string data 
type with -generate-, as you know. 

On such multiple response problems, see also 
How do I deal with multiple responses?
http://www.stata.com/support/faqs/data/multresp.html

Nick 
[email protected] 
*
*   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–2024 StataCorp LLC   |   Terms of use   |   Privacy   |   Contact us   |   What's new   |   Site index