Help using Base SAS procedures

How to remove leading zeroes from character variable

Accepted Solution Solved
Reply
Contributor
Posts: 74
Accepted Solution

How to remove leading zeroes from character variable

Hi,

I Have a character variable with comes with leading zeroes.

ID
0111
0222
0333

my desired result:
ID
111
222
333

Please help

Accepted Solutions
Solution
‎09-22-2016 11:25 AM
SAS Super FREQ
Posts: 8,743

Re: How to remove leading zeroes from character variable

[ Edited ]

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;

 

 

View solution in original post


All Replies
Super Contributor
Super Contributor
Posts: 3,174

Re: How to remove leading zeroes from character variable

DATA step, assignment statement using the INPUT function to convert your character variable to a SAS numeric variable, then use the desired SAS output format for the numeric variable (or take the SAS-default assigned).

Scott Barry
SBBWorks, Inc.

Suggested Google advanced search argument, this topic / post:

convert character numeric variable input function site:sas.com
Solution
‎09-22-2016 11:25 AM
SAS Super FREQ
Posts: 8,743

Re: How to remove leading zeroes from character variable

[ Edited ]

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;

 

 

Valued Guide
Posts: 632

Re: How to remove leading zeroes from character variable

Assuming that you do want a character variable without leading zeros, an alternative to the PUT INPUT functions could be the use of the VERIFY and SUBSTR functions. Not because they are better but VERIFY does not get used too much and here is its chance to shine.

[pre]
data val;
x='000asd1234';output;
x='123'; output;
x='0009876'; output;
run;
data nozero;
set val;
y = substr(x,verify(x,'0'));
run;
[/pre]
Super Contributor
Super Contributor
Posts: 3,174

Re: How to remove leading zeroes from character variable

Use the INPUT function to ensure an accurate result (based on the original data sample posted). No question that VERIFY has its purpose but definitely not here - considering an embedded zero character in the character variable string.

Scott Barry
SBBWorks, Inc.
Respected Advisor
Posts: 3,777

Re: How to remove leading zeroes from character variable

VERIFY returns position of first character NOT in the second argument. Embedded zero is not a problem.
Super Contributor
Super Contributor
Posts: 3,174

Re: How to remove leading zeroes from character variable

I stand corrected - nice application, ArtC.

Scott
N/A
Posts: 1

How to remove leading zeroes from character variable

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-

Super User
Posts: 9,687

How to remove leading zeroes from character variable

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

Occasional Contributor
Posts: 7

Re: How to remove leading zeroes from character variable

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!

Occasional Contributor SGK
Occasional Contributor
Posts: 8

Re: How to remove leading zeroes from character variable

Brilliant!

Contributor
Posts: 66

Re: How to remove leading zeroes from character variable

do

id1=id*1
then if u wnat use the id1 or u can convert id1 again in to character.
id1=put(id1,char4.);
Contributor
Posts: 74

Re: How to remove leading zeroes from character variable

A big THANK YOU to all of you for your suggestions,appreciate it.
New Contributor
Posts: 2

Re: How to remove leading zeroes from character variable

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.

 

 

Valued Guide
Posts: 765

Re: How to remove leading zeroes from character variable

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

☑ This topic is solved.

Need further help from the community? Please ask a new question.

Discussion stats
  • 14 replies
  • 43551 views
  • 7 likes
  • 12 in conversation