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

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


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

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 

	mod(myvar,100) 

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 

	real(subinstr(string(myvar),"0","",1)) 

but that assumes that "0" occurs just once. 

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