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:
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?
I don't know how this happens. I can't reproduce the experience, so it is hard to debug.
I have been working with the Jive tech support staff. They suggested that we verify the the post was opened as a question -- this one was. And that the person who is trying to mark it as helpful verify that they are actively logged in (refresh the page to make sure your log in hasn't expired).
The other option for @Csc to try is to select Reply on the answer they want to mark as correct. It should also show up then.
I'd love to hear from you if either of these things work.
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.