BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
Haydn
Quartz | Level 8

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

1 ACCEPTED SOLUTION

Accepted Solutions
Astounding
PROC Star

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.

View solution in original post

5 REPLIES 5
Astounding
PROC Star

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.

Ksharp
Super User

Code: Program


data _null_;
a='123456789A';
b=input(compress(a,,'kd'),best32.);
put a= b=;
run;
MadhuKorni
Quartz | Level 8

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;

Tom
Super User Tom
Super User

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.) ;

Dalveer
Calcite | Level 5

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;

SAS Innovate 2025: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

How to Concatenate Values

Learn how use the CAT functions in SAS to join values from multiple variables into a single value.

Find more tutorials on the SAS Users YouTube channel.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 5 replies
  • 4111 views
  • 13 likes
  • 6 in conversation