Is there an easier way?
data test;
Input textchar : $5.;
cards;
00100
00101
01010
10100
10101;
run;
data want;
set test;
new1 = tranwrd(textchar, "0", " "); /* replace zeros with spaces */
new2 = substr(new1, verify(new1,' ')) /* remove spaces */
new3 = tranwrd(new2 , " ", "0"); /* replace spaces with zeros */
new4 = substr(new3, 1, length(new2)); /* remove trailing zeros */
run;
data want;
set test;
want = tranwrd(strip(tranwrd(textchar, "0", " "))," ","0");
run;
data test;
Input textchar : $5.;
cards;
00100
00101
01010
10100
10101
;
run;
data temp;
length new $ 5;
set test;
n=put(input(textchar,5.),5.);
new=ifc(substr(n,length(n),1) ne '0',n,substr(n,1,length(n)-length(scan(n,-1,"123456789"))));
drop n;
proc print;run;:smileysilly::smileysilly::smileysilly:
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 |
This method was SO much simpler than anything out there. I tried so many different variations but could not find something to remove the leading zeros and keep the other zeros in an account number....this was perfect! Thank you!
I don't know if this is easier, it is your call:
data test;
Input textchar : $5.;
cards;
00100
00101
01010
10100
10101
;
data want;
set test;
length want $5;
want=prxchange("s/(0+$)//",-1,strip(prxchange("s/(^0+)//", -1,textchar)));
run;
proc print;run;
Haikuo
Do we have an item for the SASWare Ballot here?
textchar = strip(textchar, '0');
Anybody else have a use for this type of functionality?
Note that some posted solutions work differently than others when the incoming string is 00000.
@Astounding: if so, then the strip would return "101", when the input was "10001," wouldn't it?
@Art: Thanks for the correction. I've got confused. Astounding and you are absolutely correct. strip(...) is equivalent to trimn(left(...))
Chang, No, strip only removes leading and trailing blanks (or, in this case, specific leading and trailing characters).
Put it in. I'll vote for it!
I did try your code,
ERROR 72-185: The STRIP function call has too many arguments.
Put it in. I'll vote for it.
Another prx solution, handling the "00000" separately.
ods _all_ close;
ods listing;
options nocenter;
data test;
Input textchar : $5.;
cards;
00100
00101
01010
10100
10101
11111
10001
00011
11000
00000
;
run;
data chang;
set test;
length new4 $5;
if textchar = "00000" then
new4 = "0";
else
new4 = prxchange("s|^0*(.*?)0*$|$1|", -1, textchar);
run;
proc print data=chang;
run;
/* on lst
Obs textchar new4
1 00100 1
2 00101 101
3 01010 101
4 10100 101
5 10101 10101
6 11111 11111
7 10001 10001
8 00011 11
9 11000 11
10 00000 0
*/
Thanks for all your input. The reason it remains "Not Answered" is I have no icons to mark "Correct" or "Helpful".
You must have been logged in when you were on the page, but others have faced the same problem before and I don't think anyone has yet discovered why that happens sometimes.
: Renee, Do you know why people sometime don't get to see the correct and helpful icons when they do have a post marked as a question and are logged on?
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!
Learn how use the CAT functions in SAS to join values from multiple variables into a single value.
Find more tutorials on the SAS Users YouTube channel.