🔒 This topic is solved and locked.
Need further help from the community? Please
sign in and ask a new question.
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
Posted 01-15-2016 08:24 AM
(2527 views)
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.
1 ACCEPTED SOLUTION
Accepted Solutions
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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;
5 REPLIES 5
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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)
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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?
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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"?
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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;