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

st: RE: A STATA code for verification of a check digit


From   "Nick Cox" <[email protected]>
To   <[email protected]>
Subject   st: RE: A STATA code for verification of a check digit
Date   Wed, 14 May 2003 19:03:21 +0100

Chantal Mascarenhas wrote, modulo deprecated MIME/HTML,

Hi!  I would like to know if there is a STATA code to use for
verification of a check digit like the one used in SAS.
This applies to the Swedish Personnummer that everyone has
in Sweden.  It is comprised of 10 digits (12) if the century
is included.  The first 6 are the birth date in YYMMDD
followed by a 3 digit birth number and a last digit which
is a check digit comprised of the previous 9 digits
calculated by algorithm.
For example, 780924 0505

Here is a description of how it works in SAS:

< snip >

>>> I don't know SAS, so I don't know what all this code
does. Here is a stab at a calculator-type program
which checks what you type at it. I haven't done
any independent checks of the code, but given
the test data, I guess that each pair contains
one OK and one bad. I also added your own example,
presumably your own PNR, which checks out.

program pnrchecki
*! 14 May 2003
        version 8

        local I 0
        foreach n of local 0 {
                if length("`n'") != 10 {
                        di as err "`n' is not 10 digits"
                        exit 198
                }
                capture confirm integer number `n'
                if _rc {
                        di as err "`n' is not a number"
                        exit 7
                }
                local n`++I' "`n'"
        }

        tokenize 2 1 2 1 2 1 2 1 2

        di "PNR          check  correct   diagnosis"

        forval i = 1/`I' {
                local result
                local pnr "`n`i''"
                local chk = substr("`pnr'",10,1)
                forval j = 1/9 {
                        local digit = real(substr("`pnr'",`j',1))
                        local result "`result'`=`digit' * ``j'''"
                }
                local sum = 0
                forval j = 1/`=length("`result'")' {
                        local sum = `sum' +
real(substr("`result'",`j',1))
                }
                local correct = mod(10 - mod(`sum',10),10)
                local diag = cond(`correct' == `chk', "{txt:OK}",
"{err:bad}")
                di as txt ///
                "{col 1}`pnr'{col 14}`chk'{col 21}`correct'" ///
                "{col 31}`diag'"
        }
end

. pnrchecki 3103170993 3103170999 6812241450 6812241457
             6511280693 6511280692 7809240505

PNR          check  correct   diagnosis
3103170993   3      3         OK
3103170999   9      3         bad
6812241450   0      0         OK
6812241457   7      0         bad
6511280693   3      2         bad
6511280692   2      2         OK
7809240505   5      5         OK

I guess, however, that you want to check a Stata variable
containing these PNRs...

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