You don't need to learn any non-SAS skills, like regular expressions, to solve this and you don't need reams of code. The easiest way to deal with the endings of strings is to use reverse with strip (or trim) and the eventual solution code will need less conditions/statements.
The "sample" input includes 00000 (mentioned in another reply).
data test;
input textchar : $5.;
cards;
00000
00100
00101
01010
10100
10101
;
run;
data want;
set test;
length want $5;
want=textchar;
do while (substr(strip(reverse(want)),1,1) eq "0");
want=strip(reverse(substr(strip(reverse(want)),2)));
end;
run;
proc print;run;
The result is:
Obs
textchar
want
1
00000
2
00100
001
3
00101
00101
4
01010
0101
5
10100
101
6
10101
10101
Note: This is a solution based on my interpretation of the requirement.
The alternative "do" loop construct (still based on the above):
do until (substr(strip(reverse(want)),1,1) ne "0");
does not work - observations 3 and 6 are incorrect:
Obs
textchar
want
1
00000
2
00100
001
3
00101
00101
4
01010
0101
5
10100
101
6
10101
10101
... View more