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

Re: st: Splitting long string variable


From   Phil Schumm <[email protected]>
To   [email protected]
Subject   Re: st: Splitting long string variable
Date   Tue, 7 Jun 2005 04:30:23 -0500

At 3:59 PM +0800 6/7/05, Max Bulsara wrote:
I have a long string variable that contains a two digit code. I need to split it into separate variables. I don't have a separator so cannot use 'split'. I could use a substr command but the length of the string can be 50 characters long and hence requires 25 substr commands. Is there another way of splitting this variable? I am using stata v9. Would appreciate any comments.

eg:
Var1 Var11 Var12 Var13 Var14 Var15......
==========================================
aabbccddeeffgg aa bb cc dd ee
aa bbccgg wwzz rr aa bb cc gg
bbddyy bb dd yy

Why not just use a loop?  For example:


forv i = 1(2)50 {
    gen Var1`=(`i'+1)/2' = substr(Var1,`i',2)
}


will work with your example above.


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