Solved
New Contributor
Posts: 3

# Modulus-10

Hi, I need to check Swedish personal identity numbers and need code to check wether the numer in a variable is correct or not. Please submit code if you have any.

Accepted Solutions
Solution
‎01-15-2016 11:50 AM
New Contributor
Posts: 3

## Re: Modulus-10

Posted in reply to KurtBremser
Swell, the last digit to check is the ninth. The difference between the nearest larger multiple of ten should correspond to the tenth digit. So with a slight adjustment of the number of i, you function worked, which I show by using the common personal identity number with repeated 12 and by adding a flag for the result being correct or not. Thanks.

data _null_;
x = '1212121212';
sum = 0;
do i = 1 to 9;
sumi = input(substr(x,i,1),1.);
if mod(i,2) = 1
then do;
sumi = sumi * 2;
if sumi > 9 then sumi = sumi - 9;
end;
sum + sumi;
put i=;
put sumi=;
end;
put sum=;
check = mod(sum,10);
put check=;

if (int(check/10)+1)*10-check=substr(x,10,1) then do;
correct=1;
end;
else do;
correct=0;
end;
put correct=;

run;

All Replies
Super User
Posts: 9,890

## Re: Modulus-10

[ Edited ]
Posted in reply to Uppiskalle

What is the exact condition for the number to be valid?

In reference to your thread title, you might take a look at the mod() function (if this is somehow used to verify that the number is a valid one)

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
How to convert datasets to data steps
How to post code
New Contributor
Posts: 3

## Re: Modulus-10

Posted in reply to KurtBremser
The personal identity numer has 10 digits (YYYYMMDDXXXX) where the last digit is the checksum. Each of the first nine digits are multiplied, the first by 2, the second by 1, the third by 2, the fourth by 1 etc. Any sum bigger of 10 or bigger is handled as two numbers. The numbers are then summarized, and compared to the last checkshum using modulus-10 (giving the distance to the closest multiple of 10).

Example 19630810-1632 makes 1*2+9*1+6*2+3*1+0*2+8*1+1*2+0*1+1*2+6*1+3*2=2+9+12+3+0+8+2+0+2+6 but the 12 is taken as 1+2 so that 2+9+1+2+3+0+8+2+0+2+6=35. This should be compared to the modulo 10 of 35 should correspond to the tenth digit in the personal identity number.

Super User
Posts: 9,890

## Re: Modulus-10

Posted in reply to Uppiskalle

The modulo of 35 by 10 would be 5. Where in the number should the 5 be found? Or should 5 then be the sum of the digits of the last 2 positions?

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
How to convert datasets to data steps
How to post code
Super User
Posts: 9,890

## Re: Modulus-10

Posted in reply to Uppiskalle

A preliminary implementation of the check algorithm looks like this:

``````data _null_;
x = '196308101632';
sum = 0;
do i = 1 to 10;
sumi = input(substr(x,i,1),1.);
if mod(i,2) = 1
then do;
sumi = sumi * 2;
if sumi > 10 then sumi = sumi - 9;
end;
sum + sumi;
put i=;
put sumi=;
end;
put sum=;
check = mod(sum,10);
put check=;
run;
``````

The question is now, with what should we compare the value "check"?

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
How to convert datasets to data steps
How to post code
Solution
‎01-15-2016 11:50 AM
New Contributor
Posts: 3

## Re: Modulus-10

Posted in reply to KurtBremser
Swell, the last digit to check is the ninth. The difference between the nearest larger multiple of ten should correspond to the tenth digit. So with a slight adjustment of the number of i, you function worked, which I show by using the common personal identity number with repeated 12 and by adding a flag for the result being correct or not. Thanks.

data _null_;
x = '1212121212';
sum = 0;
do i = 1 to 9;
sumi = input(substr(x,i,1),1.);
if mod(i,2) = 1
then do;
sumi = sumi * 2;
if sumi > 9 then sumi = sumi - 9;
end;
sum + sumi;
put i=;
put sumi=;
end;
put sum=;
check = mod(sum,10);
put check=;

if (int(check/10)+1)*10-check=substr(x,10,1) then do;
correct=1;
end;
else do;
correct=0;
end;
put correct=;

run;

🔒 This topic is solved and locked.

Need further help from the community? Please ask a new question.

Discussion stats
• 5 replies
• 629 views
• 0 likes
• 2 in conversation