Statalist


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

st: RE: Creating single characters from long strings


From   "Nick Cox" <[email protected]>
To   <[email protected]>
Subject   st: RE: Creating single characters from long strings
Date   Wed, 26 Mar 2008 12:06:53 -0000

I don't understand how you fit 500 characters in a variable. 

-split- doesn't deal with this case. 

I once tried to automate this, but no automation I could think of made
it easier 
than doing it from first principles. 

gen length = length(yourvar)
su length, meanonly
forval i = 1/`r(max)' { 
	gen my`i' = substr(yourvar, `i', 1) 
}

Note that in some problems, you may need to -trim()- blanks that are not
interesting or useful. Also, you may know the maximum in advance so that
you don't need to calculate lengths. If you knew that there were 8
characters at most you could 

forval i = 1/8 { 
	gen my`i' = substr(yourvar, `i', 1) 
}

Nick
[email protected] 

Andri Rauch

I should create single characters from very long strings (ie around 500
characters).
My original variable abcdefg... should be transformed to:
variable_1: a, variable_2: b, variable_3: c, variable_4: d and so on...

I assume it should be done by combining the substring and foreach
command.


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