DATA Step, Macro, Functions and more

Leadin 0's Removal

Posts: 56

Leadin 0's Removal


Is there any SAS function that will remove all leading zeop from a string ? I check all the CAT(x) functions but non of them deal with leading zero alone.

Super Contributor
Super Contributor
Posts: 3,176

Re: Leadin 0's Removal

The presumption here is that you are dealing with a SAS CHARACTER variable - one that has zero character(s) as part of the string? If so, look at using the combination of an INPUT function invocation (to convert the number char-string to a SAS NUMERIC variable, and then using the PUT function in an assignment statement to convert back to character -- add the -l operand as part of the PUT function's second argument to left-justify, if desired.

Scott Barry
SBBWorks, Inc.

Recommended Google advanced search argument for this topic/post:

convert number character remove leading zeros
Regular Contributor
Posts: 165

Re: Leadin 0's Removal


x=translate(left(translate(x,' 0','0 ')),' 0','0 ');

this should switch 0's and ' ', take off the leading blanks and switch back any 0's and ' '
Respected Advisor
Posts: 3,852

Re: Leadin 0's Removal

An alternative using the "new" SUBSTRN function.

99 data _null_;
100 input s:$20.;
101 z = substrn(s,verify(s,'0'));
102 put 'NOTE: '(_all_)(=);
103 cards;

NOTE: s=000ABC z=ABC
NOTE: s=00aa z=aa
NOTE: s=001 z=1
NOTE: s=100 z=100
NOTE: s=Help z=Help
Posts: 56

Re: Leadin 0's Removal

Thanks for all your sample codes. The issue solved.

Ask a Question
Discussion stats
  • 4 replies
  • 4 in conversation