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-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

Register now!

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

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