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

st: RE: RE: identifying rounded numbers


From   "Nick Cox" <[email protected]>
To   <[email protected]>
Subject   st: RE: RE: identifying rounded numbers
Date   Fri, 7 Jun 2002 19:26:48 +0100

Anja Decressin wrote 

> > I have wage data and would like to identify if an observation has been
> > rounded.
> > I would like to know if a number has been rounded with regard 
> > to the last
> > digit and the last two digit.
> > 
> > obervation number		wage	"last digit rounded"	
> > "last two digit rounded"
> > 1. 				500	yes			
> > 	yes
> > 2.				1441	no			
> > 	no
> > 3.				1450	yes			
> > 	no
> > 
> > Is there a way to identify if a number is an integer? Then I 
> > could just
> > divide it by 10 or 100 and check if it is an integer.
> > 
> > I could alsp try to make a string variable out of the "wage"-value
> > (using -decode(wage),gen(strwage)- would require me to create 
> > labels for the
> > values, probably through some loop), counting the length of the string
> > (through -length(s)-), and then using -index(strwage,"0")- 
> > and the length(s)
> > value to find the 0s.
> > This seems way too complicated.
> > 
> > I guess, I could program a loop
> >    	gen round10=.
> > 	program define rounded10
> > 	  local i=1
> > 	  while`1' <=1000 {
> > 		replace round10=1 if wage=i
> > 		local i=`i' +1
> > 		}
> > 	end
> > 
> > but there must be Stata command for this?!

and Nick Winter wrote 
> 
> How about:
> 
> 	. generate rounded1 = ((int(wage/10)*10) == wage) if wage != .
> 	. generate rounded2 = ((int(wage/100)*100) == wage) if wage != .
> 
> How this works:
> 
> The stuff between the first equal sign and the -if- is an expression
> that evaluates to one or zero.  It evaluates to one (ie, 'true'), if
> (int(wage/10)*10) equals wage, which will be the case only when the
> final digit of wage is zero.
> 
> The -if wage!=.- ensures that the rounded variable is not assigned for
> cases where wage is missing.

A variation on Nick's theme (and to underline that you 
do not need to play with strings to attack this): 

Alternatively, use -mod(,)- and then negate the result. 

gen rounded1 = !mod(wage,10) if wage < . 
gen rounded2 = !mod(wage,100) if wage < . 

Explanation: 

1. mod(wage,10) is 0 if wage is a multiple of 10 and not 0 otherwise. 
The negation of 0 is 1 and of (not 0) is 0. You might find 
it helpful to think of mod(,) as supplying the remainder 
from a division, or whatever language is fashionable among
school mathematics teachers.  

2. Same principle for multiple of 100. 

3. wage missing is mapped to rounded? missing. 

A future "Speaking Stata" column in Stata Journal 2(4), 2002 
-- the Winter issue -- will be devoted to favourite functions. 
I am always on the lookout for nice examples. 

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