SAS Data Integration Studio, DataFlux Data Management Studio, SAS/ACCESS, SAS Data Loader for Hadoop and others

Convert character to numeric

Accepted Solution Solved
Reply
Contributor
Posts: 66
Accepted Solution

Convert character to numeric

Hi 

Calving_ease
1 
2
3
1
1
2
NA
2
NA
5
2
4
NA
NA
1
2
2
1

I would like to convert this variable records(CHARACTERS) to missing data instead of NA.  as well as I want to keep the same variable name(Calving_ease) in my original dataset (MFD4)

 

Regards 


Accepted Solutions
Solution
‎04-11-2018 01:36 PM
PROC Star
Posts: 1,577

Re: Convert character to numeric

[ Edited ]

No it hasn't in the sample you provided, below is my test and log

data have;
input Calving_ease $;
datalines;
1 
2
3
1
1
2
NA
2
NA
5
2
4
NA
NA
1
2
2
1
;

data want;
set have;
if anyalpha(calving_ease)>0 then call missing(calving_ease);
run;

436
437 data have;
438 input Calving_ease $;
439 datalines;

NOTE: The data set WORK.HAVE has 18 observations and 1 variables.
NOTE: DATA statement used (Total process time):
real time 0.02 seconds
cpu time 0.01 seconds


458 ;
459
460 data want;
461 set have;
462 if anyalpha(calving_ease) then call missing(calving_ease);
463 run;

NOTE: There were 18 observations read from the data set WORK.HAVE.
NOTE: The data set WORK.WANT has 18 observations and 1 variables.
NOTE: DATA statement used (Total process time):
real time 0.02 seconds
cpu time 0.00 seconds

 

 

View solution in original post


All Replies
PROC Star
Posts: 1,577

Re: Convert character to numeric

[ Edited ]
if anyalpha(calving_ease)>0 then call missing(calving_ease);
Contributor
Posts: 66

Re: Convert character to numeric

Posted in reply to novinosrin

this code has deleted the numeric numbers as well !! 

Solution
‎04-11-2018 01:36 PM
PROC Star
Posts: 1,577

Re: Convert character to numeric

[ Edited ]

No it hasn't in the sample you provided, below is my test and log

data have;
input Calving_ease $;
datalines;
1 
2
3
1
1
2
NA
2
NA
5
2
4
NA
NA
1
2
2
1
;

data want;
set have;
if anyalpha(calving_ease)>0 then call missing(calving_ease);
run;

436
437 data have;
438 input Calving_ease $;
439 datalines;

NOTE: The data set WORK.HAVE has 18 observations and 1 variables.
NOTE: DATA statement used (Total process time):
real time 0.02 seconds
cpu time 0.01 seconds


458 ;
459
460 data want;
461 set have;
462 if anyalpha(calving_ease) then call missing(calving_ease);
463 run;

NOTE: There were 18 observations read from the data set WORK.HAVE.
NOTE: The data set WORK.WANT has 18 observations and 1 variables.
NOTE: DATA statement used (Total process time):
real time 0.02 seconds
cpu time 0.00 seconds

 

 

Super User
Posts: 5,851

Re: Convert character to numeric

Just input(), invalid numerical data will be missing automatically.
Data never sleeps
PROC Star
Posts: 1,577

Re: Convert character to numeric

I agree with @LinusH however you may need to add a ?? modifier to prevent sas from writing NOTE: Invalid data for Calving_ease in line . messages in the log

Super User
Posts: 13,321

Re: Convert character to numeric

Posted in reply to novinosrin

@novinosrin wrote:

I agree with @LinusH however you may need to add a ?? modifier to prevent sas from writing NOTE: Invalid data for Calving_ease in line . messages in the log


Or use a custom informat to assign a special missing for the N/A values. That way you know  the source of the data was specifically marked N/A.

Super User
Posts: 6,631

Re: Convert character to numeric

To keep the same name, you have to add a little tap dancing:

 

data want;

set have;

tempvar = input(calving_ease, ??8.);

drop calving_ease;

rename tempvar = calving_ease;

run;

PROC Star
Posts: 1,577

Re: Convert character to numeric

Compress seems to work tooSmiley Happy

 

data have;
input Calving_ease $;
datalines;
1 
2
3
1
1
2
NA
2
NA
5
2
4
NA
NA
1
2
2
1
;
data want;
set have;
calving_ease=compress(calving_ease,,'kd');
run;
☑ This topic is solved.

Need further help from the community? Please ask a new question.

Discussion stats
  • 8 replies
  • 215 views
  • 3 likes
  • 5 in conversation