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

st: RE: RE: RE: eliminating the nth digit of a numeric variable


From   "Nick Cox" <[email protected]>
To   <[email protected]>
Subject   st: RE: RE: RE: eliminating the nth digit of a numeric variable
Date   Thu, 27 Feb 2003 15:36:54 -0000

Sayer, Bryan
>
> Or find the maximum value of the numeric variable (so that
> you know the
> number of characters), generate a string version, remove
> the zero, and
> convert back to a numeric.  Say the maximum is 9999999
> (seven digits).  I
> don't do this enough in Stata to know the exact commands,
> but in SAS I would
> do:
>
> newnumvar =
> input(substr(put(oldnumvar,z7.0),1,4)||substr(put(oldnumvar,
> z7.0),6,2),f6.0)
>
> Which is putting the old numeric variable zero filled to
> the left in a
> string format of length 7, concatenating the first 4
> characters with the
> last two, and then inputting the concatenated version into
> a new numeric
> variable.  As Nick says, this REQUIRES that the unwanted
> zero appears in the
> third from the right position.
>
> I'm sure the equivalent is possible in Stata, I just don't
> know how to put a
> numeric into a string with zero fill.

Zero fill is unnecessary, even this route.

The last two characters are

substr(string(myvar),-2,2)

The first substring wanted is

substr(string(myvar),1,length(string(myvar))-3)

The whole is thus

substr(string(myvar),-2,2) +
substr(string(myvar),1,length(string(myvar))-3)

and the numeric equivalent is

real(substr(string(myvar),-2,2) +
substr(string(myvar),1,length(string(myvar))-3))

Not tested.

Nick
[email protected]

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