Help using Base SAS procedures

Remove character data while converting to numeric

Reply
Occasional Contributor
Posts: 15

Remove character data while converting to numeric

I have a field called customer_id which has field type=character but It has 90,000 numbers and about 100 names.

When I convert the field type from character to numeric, it stumbles on the 100 names since they cannot be converted to numeric. I do not care about these names and want to remove them so only numbers will be retained.

Code
-------
data temp (keep = customer_skey customer_id) ;
set fdw.customer;
customer_id_new=input(customer_id, 8.);
run;

Error message :
------------------


Mathematical operations could not be performed at the following places. The results of the operations have been set to missing values.

The problem is that it does NOT create the new field which is numeric, it just gives up. I would like the program to ignore these errors where the data are characters

OR

I would like to remove the character data so the remaining numbers can easily converted to numeric data types.


Is there a way to purge out all character data from a field and keep only numeric, so the datatype can be converted from character to numeric?
Super Contributor
Super Contributor
Posts: 3,174

Re: Remove character data while converting to numeric

You can explore using the DATA step function and logic such as:

IF ANYALPHA() = 0 THEN ;

For observations with non-numeric values, you will have a SAS MISSING value condition.

Scott Barry
SBBWorks, Inc.
N/A
Posts: 0

Re: Remove character data while converting to numeric

You can eliminate these warnings/errors by using:

if input(customer_id, ?? best.) ne . then customer_id_new=input(customer_id, 8.);

~ Sukanya E
Contributor jf
Contributor
Posts: 22

Re: Remove character data while converting to numeric

data temp2 ;
set temp;
customer_id_new ^= .;
run;
Frequent Contributor
Posts: 91

Re: Remove character data while converting to numeric

Or,
data temp (keep = customer_skey customer_id) ;
set fdw.customer;
if missing(input(customer_id, ??8.)) then delete;
run;
Ask a Question
Discussion stats
  • 4 replies
  • 105 views
  • 0 likes
  • 5 in conversation