Remove leading and trailing zeros from character field

Reply
Occasional Contributor csc
Occasional Contributor
Posts: 5

Remove leading and trailing zeros from character field


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;

PROC Star
Posts: 7,363

Re: Remove leading and trailing zeros from character field

data want;

  set test;

  want = tranwrd(strip(tranwrd(textchar, "0", " "))," ","0");

run;

Super Contributor
Posts: 1,636

Re: Remove leading and trailing zeros from character field

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:

New Contributor
Posts: 2

Re: Remove leading and trailing zeros from character field

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!

Respected Advisor
Posts: 3,124

Re: Remove leading and trailing zeros from character field

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

Super User
Posts: 5,082

Re: Remove leading and trailing zeros from character field

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.

Regular Contributor
Posts: 241

Re: Remove leading and trailing zeros from character field

@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(...))

PROC Star
Posts: 7,363

Re: Remove leading and trailing zeros from character field

Chang, No, strip only removes leading and trailing blanks (or, in this case, specific leading and trailing characters).

PROC Star
Posts: 7,363

Re: Remove leading and trailing zeros from character field

Put it in.  I'll vote for it!

Super Contributor
Posts: 336

Re: Remove leading and trailing zeros from character field

I did try your code,

ERROR 72-185: The STRIP function call has too many arguments.

 

Respected Advisor
Posts: 3,892

Re: Remove leading and trailing zeros from character field

@Astounding

Put it in. I'll vote for it.

Regular Contributor
Posts: 241

Re: Remove leading and trailing zeros from character field

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
*/

Occasional Contributor csc
Occasional Contributor
Posts: 5

Re: Remove leading and trailing zeros from character field

Thanks for all your input.  The reason it remains "Not Answered" is I have no icons to mark "Correct" or "Helpful".

PROC Star
Posts: 7,363

Re: Remove leading and trailing zeros from character field

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?

SAS Employee
Posts: 224

Re: Remove leading and trailing zeros from character field

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.

Ask a Question
Discussion stats
  • 14 replies
  • 7700 views
  • 3 likes
  • 10 in conversation