BookmarkSubscribeRSS Feed
bhca60
Quartz | Level 8
How do i remove leading zeros for a variable with values that look like this:

00099V67
000123

Some have two zeros in front some have three.
4 REPLIES 4
mkeintz
PROC Star

You apparently have a character variable, since one of your sample values has the letter "V" in it.

 

First, and definitely foremost:  why do you want to remove leading zeroes from a non-numeric variable? 

 

Let's say you did so.  Then, since your sort order is lexicographic, instead of sort order of

 

000999
002231
011577

 

 

with leading zeroes removed (and the remaining characters left-justified) you would have this order:

11577
2231
999

 

Do you really want that

 

 

--------------------------
The hash OUTPUT method will overwrite a SAS data set, but not append. That can be costly. Consider voting for Add a HASH object method which would append a hash object to an existing SAS data set

Would enabling PROC SORT to simultaneously output multiple datasets be useful? Then vote for
Allow PROC SORT to output multiple datasets

--------------------------
Tom
Super User Tom
Super User

This has been asked before, you could search and see if you can find the older discussions.

 

This is what the VERIFY function is good at.

data have;
  input str $12.;
cards;
00099V67
000123
12345
;

data want;
  set have;
  if verify(trim(str),'0') then str=substr(str,verify(str,'0'));
run;
SASKiwi
PROC Star

What does your variable represent? Leading zeroes can have meaning in character variables and could be dangerous to remove.

Ksharp
Super User
data have;
  input str $12.;
cards;
00099V67
000123
12345
;

data want;
  set have;
  want=prxchange('s/^0+//',1,strip(str));
run;

SAS Innovate 2025: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

How to Concatenate Values

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 4 replies
  • 1818 views
  • 0 likes
  • 5 in conversation