BookmarkSubscribeRSS Feed
Pooja98
Fluorite | Level 6
Hi experts,
I came across a sample dataset where I want to convert character to numeric ones.

The sample datasets contains

The datasets what I have
Column 1
345
A56
450
124
567
R45

The column variable is character... I want to convert to numeric... but The condition is if any one of the variable contains character it should me missing

The dataset what I want
Column (Numeric)
345

450
124
567


Please help me to overcome this problem


TIA
7 REPLIES 7
PaigeMiller
Diamond | Level 26

You have to create a new variable for the numeric, as you can't change a variable from character to numeric.

 

data want;
    set have;
    numeric = input(column1,3.);
run;
--
Paige Miller
Pooja98
Fluorite | Level 6
How can I remove those observation which contain character
PaigeMiller
Diamond | Level 26

@Pooja98 wrote:
How can I remove those observation which contain character

Do you mean delete them from the data set? What does this have to do with your original question which is converting to numeric?

--
Paige Miller
ballardw
Super User

@Pooja98 wrote:
How can I remove those observation which contain character

If the value, such a A53, cannot be converted to numeric by the informat used you will get 1) invalid data messages in the log and 2) a missing value as a result.

 

Or are you changing the requirement from the first post? Which was " if any one of the variable contains character it should me missing"?

Delete means remove the entire record.

Reeza
Super User

You're describing the default behaviour for the INPUT() function. If it cannot be converted to a numeric value it will be set to missing by and an NOTE message will be in the log.

You can suppress those notes or check for alphabetical characters with the ANYALPHA function.

if NOT anyalpha(column1) then new_numeric_variable = input(column1, 8.);
else call missing(new_numeric_variable);



Except you don't need the ELSE because that's SAS default.
So it can become:

if NOT anyalpha(column1) then new_numeric_variable = input(column1, 8.);


Now, there's also an option to suppress the NOTES within the INPUT function, ?? so you can change it to be:

new_numeric_variable = input(column1, ?? 8.);


All the solutions above will give you the desired output, the last is the simplest.

Kurt_Bremser
Super User

Use the ?? modifier for the BEST. informat:

data have;
input column1 $;
datalines;
345
A56
450
124
567
R45
;

data want;
set have;
numeric = input(column1,??best.);
run;
Tom
Super User Tom
Super User

BEST is just an alias for the normal numeric informat.

 

Note that if the width of the INFORMAT specification is shorter than the length of the string being read the extra characters in the string are ignored.  But the INPUT() function does not mind if the width of the informat specification is longer than the length of the string being read.  So frequently is safer to just go ahead and code the maximum width the informat supports.

numeric = input(column1,??32.);

If the strings might also contain commas, dollar signs, percent signs a parentheses and you also want to convert those strings to the numbers they represent then use the COMMA (or perhaps COMMAX) informat instead.

numeric = input(column1,??comma32.); 

So in many situations the COMMA informat a better "BEST" informat than the normal  w. informat that the BEST informat is aliased to.

 

hackathon24-white-horiz.png

The 2025 SAS Hackathon has begun!

It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.

Latest Updates

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
  • 7 replies
  • 1573 views
  • 2 likes
  • 6 in conversation