I'd appreciate advice on how to loop over letters in a string in Stata. I understand it can be done in Excel, but I thought it'd be nice to learn how to do it in Stata. The problem comes from generating a check digit for ISIN numbers. For example, suppose the 11-digit ISIN is US037833100 and I want to generate a check digit following the algorithm below (copied from http://stackoverflow.com/questions/3...nto-isin-codes):
Thanks in advance for any input!
- Convert any letters to numbers: U = 30, S = 28. US037833100 -> 3028037833100.
- Collect odd and even characters: 3028037833100 = (3, 2, 0, 7, 3, 1, 0), (0, 8, 3, 8, 3, 0)
- Multiply the group containing the rightmost character (which is the FIRST group) by 2: (6, 4, 0, 14, 6, 2, 0)
- Add up the individual digits: (6 + 4 + 0 + (1 + 4) + 6 + 2 + 0) + (0 + 8 + 3 + 8 + 3 + 0) = 45
- Take the 10s modulus of the sum: 45 mod 10 = 5
- Subtract from 10: 10 - 5 = 5
- Take the 10s modulus of the result (this final step is important in the instance where the modulus of the sum is 0, as the resulting check digit would be 10). 5 mod 10 = 5
Thanks in advance for any input!
Comment