turn on suggestions

Auto-suggest helps you quickly narrow down your search results by suggesting possible matches as you type.

Showing results for

Find a Community

- Home
- /
- Data Management
- /
- Forum
- /
- Modulus-10

Topic Options

- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content

01-15-2016 08:24 AM

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

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content

01-15-2016 11:18 AM

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;

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

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content

01-15-2016 08:30 AM - edited 01-15-2016 08:31 AM

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

Maxims of Maximally Efficient SAS Programmers

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content

01-15-2016 08:42 AM

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.

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.

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content

01-15-2016 09:22 AM

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

Maxims of Maximally Efficient SAS Programmers

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content

01-15-2016 09:27 AM

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

Maxims of Maximally Efficient SAS Programmers

Solution

01-15-2016
11:50 AM

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content

01-15-2016 11:18 AM

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;