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" <>
To   <>
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


The first substring wanted is


The whole is thus

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

and the numeric equivalent is

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

Not tested.


*   For searches and help try:

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