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;
@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?
@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.
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.
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;
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.
It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.
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.
Ready to level-up your skills? Choose your own adventure.