11-08-2013 06:17 PM
I have a piece of code where it looks at an ID variable that should only have numbers in it. But I created some test data where the IDs are messed up and have some letters in it.
The goal is to transform the letters (a, b, c...) into zeros (0). The code does the job well if and only if there is only the same type of character in the string. Example:
551215A becomes 5512150
57A1215 becomes 5701215
55Cc13c becomes 5500130.
But IDs like:
55Aab5A became 55AA05A (Only the B was transformed to zero - I would like it to be 5500050)
abCDE5g became ABCDE50 (only the G was transformed to zero - I would like it to be 0000050).
That is, only the last letter the do loop sees in the observation gets transformed to zero.
Bellow follows the code:
INPUT claimid $;
set test_id (rename = (claimid = orig_claimid));
claim_num = orig_claimid + 0;
claimid = upcase(orig_claimid);
do i = 'A', 'B', 'C', 'D', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N',
'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z';
if claim_num = . and (index(claimid,i) > 0) then do;
claim_use = tranwrd(claimid,i,'0');
if claim_use = '' then do;
claim_use = claimid;
proc print data = test_id2;
I would appreciate any ideas on how to get the code to transform all letters in the string into zeros. Thank you for your hep.
11-08-2013 06:36 PM
Do you know why there are letters in your claim id's? You need a proper explanation for this before transforming such id's or you risk to fold multiple id's into one.
11-08-2013 06:40 PM
Yeah, Patrick. In theory there shouldn't be letters in this variable, so if it happen I definitely would have to separate those records and inquire as to what's going on.
But for the sake of exercise, data_null_'s response works like a charm and does what I want.
11-08-2013 09:07 PM
A PRX approach:
INPUT claimid $;