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

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

From   "Sayer, Bryan" <>
To   "''" <>
Subject   st: RE: RE: eliminating the nth digit of a numeric variable
Date   Thu, 27 Feb 2003 09:48:50 -0500

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

newnumvar =

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.

Bryan Sayer
Statistician, SSS Inc.

-----Original Message-----
From: Nick Cox []
Sent: Thursday, February 27, 2003 8:11 AM
Subject: st: RE: eliminating the nth digit of a numeric variable

Sanz De Galdeano, Anna
> I have a numeric variable which takes, for instance, values like:
> 1017
> 273076
> 518097
> 3046
> As you see, all these numbers have one thing in common: the 
> third difit
> starting from the right is always 0. That 0 should not be 
> there and I want
> to get rid of it so that my numeric variable looks like:
> 117
> 27376
> 51897
> 346
> I think I could get what I want by using decode to transfor 
> my numeric
> variable into a string variable and then use some of the 
> string functions
> and/or some of the egen options for strings.
> I guess (although I did not work it out) I could also use 
> an algorithm. But
> I have run into similar problems in the past and I would 
> like to know
> whether there is any stata routine that can be used 
> (something similar to
> what the string functions do, but for numeric varriables 
> instead) to get the
> desired result faster.

Your last two digits are 


and the rest of the number is 

	100 * int(myvar/1000) 

so everything is 

	100 * int(myvar/1000) + mod(myvar,100) 

Another way to do it 


but that assumes that "0" occurs just once. 


*   For searches and help try:
*   For searches and help try:

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