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-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!

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.

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
  • 5 replies
  • 3410 views
  • 13 likes
  • 6 in conversation