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" <BSayer@s-3.com>
To   "'statalist@hsphsun2.harvard.edu'" <statalist@hsphsun2.harvard.edu>
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
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.

Bryan Sayer
Statistician, SSS Inc.
bsayer@s-3.com

-----Original Message-----
From: Nick Cox [mailto:n.j.cox@durham.ac.uk]
Sent: Thursday, February 27, 2003 8:11 AM
To: statalist@hsphsun2.harvard.edu
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 

	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 
n.j.cox@durham.ac.uk 

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