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

RE: st: isolating characters in the middle of a long number


From   "Nichols, Austin" <ANichols@ui.urban.org>
To   "'statalist@hsphsun2.harvard.edu'" <statalist@hsphsun2.harvard.edu>
Subject   RE: st: isolating characters in the middle of a long number
Date   Wed, 1 Sep 2004 12:07:54 -0400

The proposed solution works well if every observation is exactly ten
``characters" (digits) long, or if you want the 5th and 6th digit from the
*end* of the number, but if you have a variable whose length varies and you
really want the 5th and 6th digit, you will go wrong:
        oldvar   newvar  
       1234567       23  
    1234567890       56  
So, assuming you really want the 5th and 6th digit, try:
. gen byte newvar = real(substr(string(oldvar,"%10.0f"),5,2))
        oldvar   newvar
       1234567       56  
    1234567890       56  

-----Original Message-----
From: David Kantor [mailto:dkantor@jhu.edu]
Sent: Tuesday, August 31, 2004 6:58 PM
To: statalist@hsphsun2.harvard.edu
Subject: Re: st: isolating characters in the middle of a long number

Try this (untested):
. gen byte newvar = mod(int(oldvar/10000), 100)
I hope this helps.
--David

> > >I have a variable that is a number of about 10 characters, and I
> > need to
> > >create a variable that is the 5th and 6th character of the longer
> > >variable. Does anyone have any suggestions on how I could do this?
> > >
*
*   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