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

 From Chantal Mascarenhas <[email protected]> To [email protected] Subject st: A STATA code for verification of a check digit Date Wed, 14 May 2003 17:33:19 +0200

Title: Message
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:

### SAS code for verifying the check digit

```/*******************************************
PNR_CHK.SAS
This code reads 10-digit person numbers and
checks that the check digit (the 10th digit
in the PNR) is correct.
It is assumed that PNR is a character
variable of length 10.

*******************************************/

/*******************
********************/
data temp;
input pnr \$ 1-10;
cards;
3103170993
3103170999
6812241450
6812241457
6511280693
6511280692
;
run;

data pnr_chk;
set temp;
length product \$ 18 result \$ 3;
array two_one {9} (2 1 2 1 2 1 2 1 2);
/*********************************************
multiply each of the first 9 digits in PNR by
the corresponding digit in the array two_one
and concatenate the result.
The COMPRESS function removes blanks.
*********************************************/
do i = 1 to 9;
product=compress(product||(substr(pnr,i,1)
*two_one{i}));
end;

/** Now we sum the digits **/
do i = 1 to length(product);
sum=sum(sum,substr(product,i,1));
end;

/** extract the check digit from PNR **/
chk=substr(pnr,10,1);

/** calculate the correct check digit **/
corr_chk=mod(10-mod(sum,10),10);

if chk=corr_chk then result='ok';
label
chk='Actual check number'
corr_chk='Correct check number'
pnr='Personnummer (10 digits)'
;
run;

proc print data="pnr_chk;"
var pnr chk product sum corr_chk result;
run;
```
Thanks and very curious,
Chantal.

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Chantal Mascarenhas - Doktorand/Ph.D Student
Dept. of Medical Epidemiology & Biostatistics
Karolinska Institutet
Nobels V�g 12A
P.O. Box 281
SE - 171 77 Stockholm
SWEDEN.
Tel:   +46 (0)8 524 82307
Mob: +46 (0)73 785 6378
Fax:  +46 (0)8 31 49 75
Email:  [email protected]
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~