Editor's note: The INPUT ifunction is the easist solution first mentioned by @sbb. Cynthia goes into a little more detail below on the solution.
Hi:
Remember that there may be a reason that an ID number has a leading zero. It could be that the ID will be invalid without the leading zero.
It would be useful to know whether you want to create a new numeric variable without leading zeroes (easily done with the INPUT function); or whether you want to create a new character variable (still possible, but needs to use both the PUT and INPUT functions).
In the conversion of a character string using the INPUT function, leading zeroes automatically "go away". Once you have a new numeric variable, you might be satisfied with that value as a number. On the other hand, if you really just want a new character variable, then you can take the results of the INPUT function and use the PUT function to turn the new number back into a character string. If you want the value "left-justified", then either the COMPRESS function or the LEFT function would get rid of leading spaces in the new character string. In my example below, I use the LEFT function with the PUT function. (I added some test cases to the data to show that the 2 methods will work for a broader range of data scenarios.)
cynthia
data charval;
infile datalines;
input ID $;
** create a numeric variable which will not contain a leading 0;
** the INPUT function turns a character string into a number value;
newnum = input(ID,4.);
** The INPUT and PUT function together would correctly make a;
** new character variable without leading zeroes;
newchar_fromPUT = left(put(newnum,$4.));
return;
datalines;
0111
0222
0333
0404
5348
0044
;
options nodate nonumber;
proc print data=charval;
var ID newnum newchar_fromPUT;
title 'Get Rid of -ONLY- the Leading Zero in ID Char String';
title2 'INPUT method makes a numeric variable';
title3 'PUT with INPUT makes a character variable';
run;
Editor's note: The INPUT ifunction is the easist solution first mentioned by @sbb. Cynthia goes into a little more detail below on the solution.
Hi:
Remember that there may be a reason that an ID number has a leading zero. It could be that the ID will be invalid without the leading zero.
It would be useful to know whether you want to create a new numeric variable without leading zeroes (easily done with the INPUT function); or whether you want to create a new character variable (still possible, but needs to use both the PUT and INPUT functions).
In the conversion of a character string using the INPUT function, leading zeroes automatically "go away". Once you have a new numeric variable, you might be satisfied with that value as a number. On the other hand, if you really just want a new character variable, then you can take the results of the INPUT function and use the PUT function to turn the new number back into a character string. If you want the value "left-justified", then either the COMPRESS function or the LEFT function would get rid of leading spaces in the new character string. In my example below, I use the LEFT function with the PUT function. (I added some test cases to the data to show that the 2 methods will work for a broader range of data scenarios.)
cynthia
data charval;
infile datalines;
input ID $;
** create a numeric variable which will not contain a leading 0;
** the INPUT function turns a character string into a number value;
newnum = input(ID,4.);
** The INPUT and PUT function together would correctly make a;
** new character variable without leading zeroes;
newchar_fromPUT = left(put(newnum,$4.));
return;
datalines;
0111
0222
0333
0404
5348
0044
;
options nodate nonumber;
proc print data=charval;
var ID newnum newchar_fromPUT;
title 'Get Rid of -ONLY- the Leading Zero in ID Char String';
title2 'INPUT method makes a numeric variable';
title3 'PUT with INPUT makes a character variable';
run;
I was doing a google search to find the answer to my SAS problem, and found this question and answer.
I know this posting is more than 1.5 years old, but I just wanted to say tha your answer is exactly what I needed and I thank you. My data is a character string of mixed numbers and letters almost like your first example, and I needed to drop the leading zeros.
Oh I also want to thank ren2010 for posting the question in the first place.
-Larry-
There are always a couple of ways to solve a problem in SAS.
data val; x='000asd1234';output; x='123'; output; x='0009876'; output; run; data nozero; set val; y =prxchange('s/^0+//o',-1,x); run;
Ksharp
Just wanted to shout out to Art C. I hadn't used the verify function before, but I applied it much the way you did in your example to achieve exactly what I needed. Also, love your book on Innovative SAS Techniques!
Brilliant!
I do it that way.
DATA FINAL;
SET ORIGEM;
TEST = COMPRESS(PUT(INPUT(VARIABLE_WITH_ZERO,8.),8.));
RUN;
8. is the size of the variable.
Compress is for remove blank spaces.
Hi, try this ...
data x;
input id :$6. @@;
datalines;
0111 0222 0333 000012 0099 88
;
data xplus;
set x;
id = cat(input(id,6.));
run;
data set XPLUS
Obs id
1 111
2 222
3 333
4 12
5 99
6 88
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!
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.