Help using Base SAS procedures

Modulus 10 check

Reply
N/A
Posts: 0

Modulus 10 check

Problem Description:
Problem occurs when doing a modulus 10 check on ID Numbers.
I have ID numbers (eg: 1999999999999 and 9999999999999), and the month and day is calculated as 99 and 99 respectively. The problem occurs in the alogrithm.
I have inserted a step: If sum (EvenTot + OddTot) >= 99 Then IdAlgoX = -1; this is to say it is invalid, however the error message still appears??

Can anyone help pls...


/*Fields for valid date testing*/
month = input(substr(temp,3,2),2.);
day = input(substr(temp,5,2),2.);

/*Fields for Modulus 10 check*/;
OddTot= Sum(input(Substr(temp, 1,1),1.),
input(Substr(temp, 3,1),1.),
input(Substr(temp, 5,1),1.),
input(Substr(temp, 7,1),1.),
input(Substr(temp, 9,1),1.),
input(Substr(temp,11,1),1.));

EvenTot=Sum(input(Substr(Put(input(Substr(temp, 2,1),1.)*2,z2.),1,1),1.), /* Two1 */
input(Substr(Put(input(Substr(temp, 2,1),1.)*2,z2.),2,1),1.), /* Two2 */
input(Substr(Put(input(Substr(temp, 4,1),1.)*2,z2.),1,1),1.), /* Fou1 */
input(Substr(Put(input(Substr(temp, 4,1),1.)*2,z2.),2,1),1.), /* Fou2 */
input(Substr(Put(input(Substr(temp, 6,1),1.)*2,z2.),1,1),1.), /* Six1 */
input(Substr(Put(input(Substr(temp, 6,1),1.)*2,z2.),2,1),1.), /* Six2 */
input(Substr(Put(input(Substr(temp, 8,1),1.)*2,z2.),1,1),1.), /* Eig1 */
input(Substr(Put(input(Substr(temp, 8,1),1.)*2,z2.),2,1),1.), /* Eig2 */
input(Substr(Put(input(Substr(temp,10,1),1.)*2,z2.),1,1),1.), /* Ten1 */
input(Substr(Put(input(Substr(temp,10,1),1.)*2,z2.),2,1),1.), /* Ten2 */
input(Substr(Put(input(Substr(temp,12,1),1.)*2,z2.),1,1),1.), /* Twe1 */
input(Substr(Put(input(Substr(temp,12,1),1.)*2,z2.),2,1),1.)); /* Twe2 */

IdAlgoX=10-input(Substr(Put(Sum(EvenTot,OddTot),z2.),2,1),1.);
If sum (EvenTot + OddTot) >= 99 Then IdAlgoX = -1;
IdAlgoY=input(Substr(temp,13,1),1.);
If IdAlgoX = 10 then IdAlgoX = 0;
Respected Advisor
Posts: 3,901

Re: Modulus 10 check

Hi sas-learner2

I was honestly too lazy in trying to understand your code to the last bit.

May be the following is anyway of some help to you as it implements a modulo10 check as described in http://en.wikipedia.org/wiki/Luhn_algorithm for any string with digits only.

There might be more elaborate ways to do it and I'm almost sure this wheel is already invented in the SAS world. The code below is what I can come up with right now.

HTH
Patrick

data _null_;
input id $ 1-30;
length AddIt $2;
ChkStr=left(reverse(id));
do pos=1 to length(ChkStr);
if mod(pos,2) then /* odd positions */
do;
AddIt=substr(ChkStr,pos,1);
end;
else /* even positions: digit*2 */
do;
AddIt=put(2*input(substr(ChkStr,pos,1),2.),2.);
end;

/* add digits */
do i=1 to length(AddIt);
ChkSum+input(substr(AddIt,i,1),2.);
end;
end;

/* Check if ID is valid or not (if ChkSum ends with Zero) */
if mod(ChkSum,10)=0 then
do;
put 'This is a valid ID: ' ID= ChkSum=;
end;
else
if mod(ChkSum,10) ne 0 then
do;
put 'This is a invalid ID: ' ID= ChkSum=;
end;

datalines;
123456789
3847592
48573726264859560
2843759
00028434305834
442308239586
;
run;
Ask a Question
Discussion stats
  • 1 reply
  • 153 views
  • 0 likes
  • 2 in conversation