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

Re: st: Splitting a string variable


From   Raphael Fraser <raphael.fraser@gmail.com>
To   statalist@hsphsun2.harvard.edu
Subject   Re: st: Splitting a string variable
Date   Tue, 6 Sep 2005 16:21:48 -0500

Thanks to everyone.

On 9/6/05, Nick Cox <n.j.cox@durham.ac.uk> wrote:
> Excellent.
> 
> Replacing trailing zeros looks like
> 
> reverse(substr(reverse(x), indexnot(reverse(x), "0"),.))
> 
> Nick
> n.j.cox@durham.ac.uk
> 
> Jamie Griffin
> 
> > To remove leading zeros from the string variable x without
> > looping, how
> > about:
> >
> > replace x=substr(x, indexnot(x, "0"), . )
> 
> > >>> n.j.cox@durham.ac.uk 09/06/05 7:27 pm >>>
> > This is not really a splitting problem,
> > at least not sensu -split-.
> >
> > To remove leading zeros, I would do this:
> >
> > quietly {
> >       count if substr(myvar,1,1) == "0"
> >       while `r(N)' {
> >               replace myvar = substr(myvar,2,.) ///
> >                       if substr(myvar,1,1) == "0"
> >               count if substr(myvar,1,1) == "0"
> >       }
> > }
> >
> > Any solutions without looping?
> >
> > Nick
> > n.j.cox@durham.ac.uk
> >
> >  >>> Raphael Fraser
> >
> > I have a string variable of the type listed below:
> >
> > id
> > 0008
> > 0020
> > 016A
> > 0160C
> >
> > How do I remove the leading zeros from this variable? I tried using
> > the -split- command, but it removed both leading and trailing zeros.
> > The end result should look like this:
> >
> > id
> > 8
> > 20
> > 16A
> > 160C
> 
> *
> *   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/
> 


-- 
=============================
Raphael A. Fraser, MSc
Assistant Lecturer in Biostatistics
Sickle Cell Unit
Tropical Medicine Research Institute
Faculty of Medical Sciences
University of the West Indies
Kingston
Jamaica

Tel:  (876) 927-2471; 410-4699
Fax: (876) 927-2984
e-mail: raphael.fraser@uwimona.edu.jm

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