Hi SAS experts.
So I have a 41 length numeric string called ID that I want to encrypt by adding a number 263, and keep the format as string.
eg.
1037699710538224500199468064894 should become
1037699710538224500199468065157
This has been proving quite tricky to me as the strings are very long. I have profiled the length of this ID field and it varies from 17 to 31.
What I have tried in the below SAS is
1.
part1= substring 1 to 13
part2= substring 14 to its last position
2. Cast part into number then add 263 which is my seed then cast this back to a string
3. Put the part1 and the adjusted part 2 together and add leading zeros if necessary.
proc sql; create table output as select
/* original ID */ ID,
/*1 - test if, either casting into numeric or adding a 263 seed change the length of the second part of the ID(substring consisting from position 14 to its last charact)*/ length(strip(substr(strip(ID),14,length(ID)-14+1))) - length(strip(put(input(strip(substr(strip(ID),14,length(ID)-14+1)),20.)+263,20.))) as diff3,
/*2 - if the length of the second part reduced, append leading zero*/ case when calculated diff3 > 0 then strip(repeat("0",calculated diff3-1))||strip(put(input(strip(substr(strip(ID),14,length(ID)-14+1)),20.)+263,20.)) else strip(put(input(strip(substr(strip(ID),14,length(ID)-14+1)),20.)+263,20.)) end as second_part3,
/*3 - Now append the two parts together so that we have the full encrypted string*/ strip(substr(ID,1,13))||strip(calculated second_part3) as E_ID /******************************************************************************************************************************/
from input ; quit;
I am a bit lost becuase this is not working properly.
The first observation with
ID=1037699710538224500199468064894 becomes
E_ID=1037699710538224500199468065152
What am I doing wrong?
Can anyone think of a better approach to this exercise?
Many thanks
... View more