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

From |
"Michael Blasnik" <michael.blasnik@verizon.net> |

To |
<statalist@hsphsun2.harvard.edu> |

Subject |
st: Re: splitting variables |

Date |
Tue, 23 Nov 2004 09:08:23 -0500 |

Here's a short program that will chop apart a string every two characters and accepts a varlist. It is hard-wired to name the new variables as the old variable name plus an underscore and incrementing number (name_1, name_2,...). It also only allows string variables so, in your case, you need to make sure that the variables are strings or use tostring to accomplish that.

program define chop2

version 8.2

syntax varlist (string)

quietly {

foreach var of varlist `varlist' {

local i=1

count if substr(`var',`i'*2-1,2)!=""

while r(N)>0 {

gen `var'_`i'=substr(`var',`i'*2-1,2)

local ++i

count if substr(`var',`i'*2-1,2)!=""

}

}

}

end

You would just type -chop2 varlist- where varlist could be the hundreds of vars you need to do this to. There are other approaches, but this one works OK (use of a tempvar and summarize to calculate the maximum length of each string variable and then setting the loop explicitly rather than count/while would probably be faster)

Michael Blasnik

michael.blasnik@verizon.net

----- Original Message ----- From: "Jean Bouyer" <Bouyer@vjf.inserm.fr>

To: <statalist@hsphsun2.harvard.edu>

Sent: Tuesday, November 23, 2004 7:50 AM

Subject: st: splitting variables

I would like to split a variable into several ones, but there are no separators. Thus I don't think I can use the split Stata command. I only know that variable must be split every two digits. For instance 123456 must be split into 12 34 and 56. My problem is that I have to do that for about hundred variables and that the number of digits is not the same for all the variables (although it is always even). Thanks for help -- ------------------------------------------------------------------------------------ Jean BOUYER INSERM-INED U 569, HOPITAL DE BICETRE, 82 RUE DU GENERAL LECLERC 94276 LE KREMLIN-BICETRE CEDEX - FRANCE

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

**References**:**st: splitting variables***From:*Jean Bouyer <Bouyer@vjf.inserm.fr>

- Prev by Date:
**st: RE: splitting variables** - Next by Date:
**RE: st: RE: Graphing limitations / questions** - Previous by thread:
**st: splitting variables** - Next by thread:
**st: -makematrix- updated on SSC** - Index(es):

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