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.
substr(subjid,3)=put(input(substr(subjid,3),f8.),f8.-l);
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
substr(subjid,3)=put(input(substr(subjid,3),f8.),f8.-l);
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
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
Thank you, Data_null_!
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
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;
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;
What about, keeping the first leading 0?
Such us:
x-01
x-023
x-056
x-0123
x-0234
x-0255
x-1000
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
Hi Lilin,
thanks, it works, but it is a bit complex
Can you explain how you get this?
Thanks in advance.
Jose.
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
hi ... since this is still ongoing, back to the original question of removing leading zeroes ...
substr(subjid,3)=cat(input(substr(subjid,3),8.));
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
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 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!
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.
Ready to level-up your skills? Choose your own adventure.