BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
ren2010
Obsidian | Level 7
Hi,

I Have a character variable with comes with leading zeroes.

ID
0111
0222
0333

my desired result:
ID
111
222
333

Please help
1 ACCEPTED SOLUTION

Accepted Solutions
Cynthia_sas
SAS Super FREQ

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

14 REPLIES 14
sbb
Lapis Lazuli | Level 10 sbb
Lapis Lazuli | Level 10
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
Cynthia_sas
SAS Super FREQ

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;

 

 

ArtC
Rhodochrosite | Level 12
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]
sbb
Lapis Lazuli | Level 10 sbb
Lapis Lazuli | Level 10
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.
data_null__
Jade | Level 19
VERIFY returns position of first character NOT in the second argument. Embedded zero is not a problem.
sbb
Lapis Lazuli | Level 10 sbb
Lapis Lazuli | Level 10
I stand corrected - nice application, ArtC.

Scott
lordlnyc
Calcite | Level 5

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-

Ksharp
Super User

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

WilliamD_
Fluorite | Level 6

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!

SGK
Calcite | Level 5 SGK
Calcite | Level 5

Brilliant!

sas_
Fluorite | Level 6
do

id1=id*1
then if u wnat use the id1 or u can convert id1 again in to character.
id1=put(id1,char4.);
ren2010
Obsidian | Level 7
A big THANK YOU to all of you for your suggestions,appreciate it.
osvaldoberg
Calcite | Level 5

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.

 

 

MikeZdeb
Rhodochrosite | Level 12

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

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

Register now!

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 14 replies
  • 116207 views
  • 16 likes
  • 12 in conversation