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;
... View more