Statalist The Stata Listserver

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

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

From   Lia Orfei <>
Subject   Re: st: RE: RE: summing up digits and comparing to a check digit
Date   Wed, 15 Mar 2006 16:47:15 +0000

Yes my problem implied manipulation of integers only, although the discussion about decimals could be useful for someone else.
Thanks a lot, I am amazed and grateful

Nick Cox wrote:

The limitations of the -mod()- function given fractional parts are indeed important. FWIW, Tom Steichen and I wrote this up at

I was presupposing manipulations of integers only, as the context implies.
Richard Atkins

Rather than use the mod function I would recommend converting all your
values to formatted strings using the =string(varname, format) command
prior to extracting the digits.

In general the mod function works well but I have come across datasets
where values that displayed in tables and lists as (for example) 0.7
produced a '6' when I tried to extract the 'tenths' digit using
mod(int(varname*10),10). It turned out that the floating point
representation of this value in the dta file was actually 0.69999999 so
the mod function gave me exactly what I asked for rather than what I
actually wanted.

Some examples of extracting digits in this way are available in my
digit preference functions at 03/15/06 3:04 pm >>>

-pnrcheck- from SSC covers the Swedish case.
In this case, look _inside_ the program for some ideas. String functions are not the only way. -mod()- is also useful.
The authors of -pnrcheck- are two Italians
and a Welshman, just who you would expect
to be working on Swedish identifiers.
Lia Orfei

I have a variable that contains 7-digit numbers; the first digit is the remainder of the sum of the last six digits divided by 10 (check digit)

example: 1030332 => 0+3+0+3+3+2=11/10=1+1 => check digit=1
12050457 => 0+5+0+4+5+7=21/10=2+1 => check digit=1
and so on.

Now I want to assess whther the check digit is correct for each


How do I calculate the partial sum of the contents of this variable?

Extra challenge: some of the numbers sum up to 10, 20 etc so the check digit is actually 0 and it does not appear in the data for some reason (example 50456 is correct becasue 5+0+4+5+6=20 so the remainder is


Any ideas?? I was thinking about using the string functions.

* For searches and help try:

This message has been scanned for viruses and
dangerous content by MailScanner, and is
believed to be clean.

*   For searches and help try:

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