DATA Step, Macro, Functions and more

Remove leading Zeros as there are many

Reply
Contributor
Posts: 37

Remove leading Zeros as there are many

data l;
input userno $1-100;
cards;
00789
00000000000000000000000004567
000000000000000000000000000000000000000000000000000000000000004567
000000000000000000000000000000004567
4567
00000000056
00lo
000ui
run;


I am having a character varibale i want to supress if the variable is having leading
zero as i cant conver it to numeric as it hassome character data .

I cant to by substr as there are hudred length i cant write substr
or if condition

i wnat the out put like this

output

789
4567
4567
4567
4567
56
lo
ui

Super Contributor
Posts: 349

Re: Remove leading Zeros as there are many

Hi,

Check this link  https://communities.sas.com/thread/35500?start=0&tstart=0

Try this..but if you have any zeros in between or after then compress will not work....

data l;

input userno $ 1-67;

xx=compress(userno,0);

cards;

00789

00000000000000000000000004567

000000000000000000000000000000000000000000000000000000000000004567

000000000000000000000000000000004567

4567

00000000056

00lo

000ui

run;

Thanks,

Shiva

Message was edited by: shiva saidala

Respected Advisor
Posts: 3,777

Re: Remove leading Zeros as there are many

VERIFY and SUBSTRN

6178  data l;

6179     infile cards truncover;

6180     input userno $1-100;

6181     put 'NOTE: before: ' userno=;

6182     userno = substrn(userno,verify(userno,'0'));

6183     put 'NOTE- after : ' userno=;

6184     cards;

NOTE: before: userno=00789

      after : userno=789

NOTE: before: userno=00000000000000000000000004567

      after : userno=4567

NOTE: before: userno=000000000000000000000000000000000000000000000000000000000000004567

      after : userno=4567

NOTE: before: userno=000000000000000000000000000000004567

      after : userno=4567

NOTE: before: userno=4567

      after : userno=4567

NOTE: before: userno=00000000056

      after : userno=56

NOTE: before: userno=00lo

      after : userno=lo

NOTE: before: userno=000ui

      after : userno=ui


Valued Guide
Posts: 765

Re: Remove leading Zeros as there are many

Hi ... just curious, why SUBSTRN versus SUBSTR as used by Ksharp?  In this posting (leading zeroes), is there any instance where SUBSTRN will do anything that SUBSTR won't do?  Thanks.

Respected Advisor
Posts: 3,777

Re: Remove leading Zeros as there are many

Yes you are correct I was thinking it needed SUBSTRN but it does not. 

Super User
Posts: 9,676

Re: Remove leading Zeros as there are many

Mike,

There is no need to use substrn(), since VERIFY() would take care of it very well.

Ksharp

Super User
Posts: 9,676

Re: Remove leading Zeros as there are many

Here are two approach. First is using verify, Second is using PRX. I prefer to PRX.

data l;
input userno : $100.;
cards;
00789
00000000000000000000000004567
000000000000000000000000000000000000000000000000000000000000004567
000000000000000000000000000000004567
4567
00000000056
00lo
000ui
;
run;
data want;
 set l;
 new=substr(userno,verify(userno,'0'));
run;






new=prxchange('s/^0+//o',1,subjid);



Ksharp

New Contributor
Posts: 3

Re: Remove leading Zeros as there are many

data x;

set l;

z=compress(userno,'1234567890') ;

n=1*userno;

use=catx("",z,n);

use1=compress(use,".");

drop z n use;

run;

As a new bie I will be doing in this way ...please let me know any efficient  ideas .

Giridhar.

Super User
Super User
Posts: 7,400

Re: Remove leading Zeros as there are many

This post is 3 years.  Start a new question if needed, with test data and required output.  If the initial post is to go by then:

new_var=strip(tranwrd(old_var,"0",""));

Should work.

Ask a Question
Discussion stats
  • 8 replies
  • 5828 views
  • 1 like
  • 7 in conversation