Solved
Contributor
Posts: 48

# remove last charater and convert to numeric

Hi,

I have a field which has nine numeric characters and a letter.. 123456789A. I want to remove the last character and convert the field to numberic.

I have te following to strip the last character ... SUBSTR(ABC,1,9)  as ABCI and have also found this to convert to numeric .. input(substr(ABCI,5),best.) as ABCII

I'm still new to SAS, but can these be combined or is there another/better method to remove the last character from ABC and convert to to a numeric value?

Cheers

Haydn

Accepted Solutions
Solution
‎07-01-2015 09:16 AM
Super User
Posts: 6,751

## Re: remove last charater and convert to numeric

An easier way:  Don't bother to strip the last character.  Just read the first 9 digits into a numeric:

newvar = input(oldvar, 9.);

Of course, this relies on the first 9 characters always being digits.

Good luck.

All Replies
Solution
‎07-01-2015 09:16 AM
Super User
Posts: 6,751

## Re: remove last charater and convert to numeric

An easier way:  Don't bother to strip the last character.  Just read the first 9 digits into a numeric:

newvar = input(oldvar, 9.);

Of course, this relies on the first 9 characters always being digits.

Good luck.

Super User
Posts: 10,761

## Re: remove last charater and convert to numeric

### Code: Program

`data _null_; a='123456789A'; b=input(compress(a,,'kd'),best32.); put a= b=;run;`
Frequent Contributor
Posts: 76

## Re: remove last charater and convert to numeric

As it is a character variable, it will be left alligned and you need the first 9 characters which doesnt require to use strip.

data want;

set have;

newvar=input(substr(var,1,9),9.);

run;

data Sample;

having='123456789A';

wanted=input(substr(having,1,9),9.);

run;

Super User
Posts: 8,068

## Re: remove last charater and convert to numeric

So the INPUT function might be all you need.

num=input(char,9.);

Now if the number part might use less than 9 digits then you might need to do some more work.  If the variable is defined as length 10 then adding a RIGHT() function to move the value over so that letter is in the 9th place will work.

num = input(right(char),9.);

or perhaps use substr() to make sure you only have 10 characters.

num = input(right(substr(char,1,10)),9.) ;

Occasional Contributor
Posts: 10

## Re: remove last charater and convert to numeric

Hi,

you can use this code to make it more efficient:-

input dataset:-->

data cont;

input col1 \$ 10.;

cards;

123456789A

;

run;

Desired output:--->

data cont1;

set cont;

char=compress(col1,'0-9','d');

num=input(compress(col1,'a-z','a'),best12.);

run;

🔒 This topic is solved and locked.