Statalist The Stata Listserver


[Date Prev][Date Next][Thread Prev][Thread Next][Date index][Thread index]

Re: st: Re: summing up digits and comparing to a check digit


From   "Michael Blasnik" <[email protected]>
To   <[email protected]>
Subject   Re: st: Re: summing up digits and comparing to a check digit
Date   Wed, 15 Mar 2006 11:38:29 -0500

Wow, step away from your desk for a minute and a 10 post thread appears with a clear and concise bug-avoiding improvement to my little bit of code. Statalist can work at an amazing speed sometimes...

Michael

----- Original Message ----- "Nick Cox" <[email protected]> wrote:

gen x = string(myvar, "%07.0f")

gets you there in one.

Nick
[email protected]

Austin Nichols

I agree wholeheartedly with Michael Blasnik's opening caveat--was your
example 12050457 => 0+5+0+4+5+7=21/10=2+1 => check digit=1 supposed to
be 1050457 => 0+5+0+4+5+7=21/10=2+1 => check digit=1 by any chance?

As for his solution, it works well except when digits may be fewer
than 6 digits when turned into strings--I would replace his second
line with
 replace x=substr("0000000",1,7-length(x))+x
to pad numbers out to y digits with leading zeros, and then
add another line
 assert length(x)==7
to make sure there are no odd cases.

. li m x c, noo clean

      myvar         x   check
     105059   0105059       0
       2459   0002459       0
    7102059   7102059       7

On 3/15/06, Michael Blasnik <[email protected]> wrote:
> I'm not sure I understand the problem -- one of your
examples is an 8 digit
> number but you say you have 7 digit numbers.  Anyway, here
is some "highly
> advanced programming" (?) :
>
> gen x=string(myvar)
> replace x="0"+x if length(x)==6
> gen
>
check=mod(real(substr(x,2,1))+real(substr(x,3,1))+real(substr(
x,4,1))+real(substr(x,5,1))+real(substr(x,6,1))+real(substr(x,
7,1)),10)
>
> make sure to remove any line break that the email puts in
the last line.
*
*   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