BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
michtka
Fluorite | Level 6

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

;

Thanks in advance.

V.

1 ACCEPTED SOLUTION

Accepted Solutions
data_null__
Jade | Level 19

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

View solution in original post

16 REPLIES 16
michtka
Fluorite | Level 6

Sorry, I had a mistake in the dataset:

Please, forget the first request:

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

Thanks in advance.

V

data_null__
Jade | Level 19

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

Haikuo
Onyx | Level 15

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

data_null__
Jade | Level 19

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

Haikuo
Onyx | Level 15

Thank you, Data_null_!

Haikuo
Onyx | Level 15

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

Linlin
Lapis Lazuli | Level 10

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;

michtka
Fluorite | Level 6

Thanks guys,

I found another one, using veryfy function: Smiley Happy

data want;

set new;

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

run;

What about, keeping the first leading 0?

Such us:

x-01

x-023

x-056

x-0123

x-0234

x-0255

x-1000

Linlin
Lapis Lazuli | Level 10

how about:

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

michtka
Fluorite | Level 6

Hi Lilin,

thanks, it works, but it is a bit complex Smiley Sad

Can you explain how you get this?

Thanks in advance.

Jose.

Linlin
Lapis Lazuli | Level 10

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

MikeZdeb
Rhodochrosite | Level 12

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

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

Ksharp
Super User

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

Haikuo
Onyx | Level 15

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

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!

What is Bayesian Analysis?

Learn the difference between classical and Bayesian statistical approaches and see a few PROC examples to perform Bayesian analysis in this video.

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
  • 16 replies
  • 7827 views
  • 8 likes
  • 7 in conversation