## removing leading zeros of a character variable

Solved
Super Contributor
Posts: 301

# removing leading zeros of a character variable

Hi everyone, how can I remove

a) the first leading zero: 0

b) the two leading zeros: 00

of the character variable subjid:

data new;

input subjid \$;

datalines;

x-001

x-0023

x-0056

x-00123

x-00234

x-00255

;

V.

Accepted Solutions
Solution
‎05-31-2012 07:21 AM
Posts: 3,852

## Re: removing leading zeros of a character variable

substr(subjid,3)=put(input(substr(subjid,3),f8.),f8.-l);

All Replies
Super Contributor
Posts: 301

## Re: removing leading zeros of a character variable

Sorry, I had a mistake in the dataset:

i have this dataset:

data  have;

input subjid \$;

datalines;

x-0001

x-0023

x-0056

x-0123

x-0234

x-0255

x-1000

;

run;

I want this:

x-1

x-23

x-56

x-123

x-234

x-255

x-1000

V

Solution
‎05-31-2012 07:21 AM
Posts: 3,852

## Re: removing leading zeros of a character variable

substr(subjid,3)=put(input(substr(subjid,3),f8.),f8.-l);

Posts: 3,167

## Re: removing leading zeros of a character variable

data_null_,

I am *not* exaggerating,  I learn some from almost every one of your post. but I could not find the informat or format of 'f8.', and what is the purpose of '-l' ?

Thanks,

Haikuo

Posts: 3,852

## Re: removing leading zeros of a character variable

F is an alias for w.d "unnamed" format/informat.  It is returned by V functions.  -L is the justification modifier.

3900  data _null_;

3901     x = 1;

3902     f = vformat(x);

3903     fn = vformatN(x);

3904     put (_all_)(=);

3905     stop;

3906     format x 4.1;

3907     run;

x=1.0 f=F4.1 fn=F

Posts: 3,167

## Re: removing leading zeros of a character variable

Thank you, Data_null_!

Posts: 3,167

## Re: removing leading zeros of a character variable

Or Prx solution:

data have;

input subjid \$;

newid=prxchange('s/x-0+/x-/',-1,subjid);

datalines;

x-0001

x-0023

x-0056

x-0123

x-0234

x-0255

x-1000

;

proc print;run;

Haikuo

Super Contributor
Posts: 1,636

## Re: removing leading zeros of a character variable

or

data  have;

input subjid \$;

datalines;

x-0001

x-0023

x-0056

x-0123

x-0234

x-0255

x-1000

;

data want(drop=subjid rename=(new=subjid));

set have;

new=catx('-','x',put(input(scan(subjid,2),4.),4.));

proc print;run;

Super Contributor
Posts: 301

## Re: removing leading zeros of a character variable

Thanks guys,

I found another one, using veryfy function:

data want;

set new;

code=catx('-','x',substr(scan(subjid,2),verify(scan(subjid,2),'0')));

run;

Such us:

x-01

x-023

x-056

x-0123

x-0234

x-0255

x-1000

Super Contributor
Posts: 1,636

## Re: removing leading zeros of a character variable

data  have;

input subjid \$;

datalines;

x-0001

x-0023

x-0056

x-0123

x-0234

x-0255

x-1000

;

data want;

set have;

subjid=tranwrd(tranwrd(subjid,'-00','-0'),'-00','-0');

proc print;run;

Linlin

Super Contributor
Posts: 301

## Re: removing leading zeros of a character variable

Hi Lilin,

thanks, it works, but it is a bit complex

Can you explain how you get this?

Jose.

Super Contributor
Posts: 1,636

## Re: removing leading zeros of a character variable

Hi Jose,

tranwrd(subjid,'-00','-0') changes '-00', to '-0'. We need to use tranwrd twice because first tranwrd changed "x-0001" to "x-001", second tranwrd changed "x-001" to "x-01".

data  have;

input subjid \$;

datalines;

x-0001

x-0023

x-0056

x-0123

x-0234

x-0255

x-1000

;

data want;

set have;

subjid_1=tranwrd(subjid,'-00','-0');

subjid_2=tranwrd(subjid_1,'-00','-0');

proc print;run;

Obs    subjid    subjid_1    subjid_2

1     x-0001     x-001       x-01

2     x-0023     x-023       x-023

3     x-0056     x-056       x-056

4     x-0123     x-0123      x-0123

5     x-0234     x-0234      x-0234

6     x-0255     x-0255      x-0255

7     x-1000     x-1000      x-1000

Valued Guide
Posts: 765

## Re: removing leading zeros of a character variable

hi ... since this is still ongoing, back to the original question of removing leading zeroes ...

substr(subjid,3)=cat(input(substr(subjid,3),8.));

Super User
Posts: 10,784

## Re: removing leading zeros of a character variable

It is easy for Perl Regular Expression.

```data  have;

input subjid \$;

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

datalines;
x-0001
x-0023
x-0056
x-0123
x-0234
x-0255
x-1040
002022
du002304
;
run;

```

Ksharp

Message was edited by: xia keshan

Message was edited by: xia keshan

Message was edited by: xia keshan

Posts: 3,167

## Re: removing leading zeros of a character variable

Thanks, Ksharp, for sharing a more general solution. Question: new=prxchange('s/^(\D*)0+/\$1/o',1,subjid); What is 'o' here for? I do have searched, but could not find the documentation on this one.

Thanks,

Haikuo

☑ This topic is solved.