- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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;
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
Scott Barry
SBBWorks, Inc.
Suggested Google advanced search argument, this topic / post:
convert character numeric variable input function site:sas.com
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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;
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
[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]
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
Scott Barry
SBBWorks, Inc.
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
Scott
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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-
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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!
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
Brilliant!
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
id1=id*1
then if u wnat use the id1 or u can convert id1 again in to character.
id1=put(id1,char4.);
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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