BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
Uppiskalle
Fluorite | Level 6

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
Uppiskalle
Fluorite | Level 6
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;


View solution in original post

5 REPLIES 5
Kurt_Bremser
Super User

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)

Uppiskalle
Fluorite | Level 6
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.


Kurt_Bremser
Super User

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?

Kurt_Bremser
Super User

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"?

Uppiskalle
Fluorite | Level 6
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;


sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

Register now!

How to connect to databases in SAS Viya

Need to connect to databases in SAS Viya? SAS’ David Ghan shows you two methods – via SAS/ACCESS LIBNAME and SAS Data Connector SASLIBS – in this video.

Find more tutorials on the SAS Users YouTube channel.

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