## Converting character data to numeric

Solved
Occasional Contributor
Posts: 9

# Converting character data to numeric

Hello everyone!

I am having trouble converting a character column to numeric. So the Smoker and Age columns were created from the MOTHER column, and now I want the age column to be numeric. I tried to do the Age = Input(Age,2.); but the result was the same.

``````data Minute1;
title 'Corrected minute1 dataset';
set Homework.minute1;
if Appearance in ('0','1','2') and Pulse in ('0','1','2') and Grimace in ('0','1','2') and Activity in ('0','1','2') and Respiration in ('0','1','2');
Score=sum(of Appearance Pulse Grimace Activity Respiration);
length Smoker \$ 1;
n_char = length(MOTHER);
Smoker = substr(MOTHER,n_char,1);
Age = substr(MOTHER,1,n_char-1);
keep Baby_ID MOTHER Appearance Pulse Grimace Activity Respiration Smoker Age;
run;
proc print data=Minute1;
run;``````

Thanks!

Accepted Solutions
Solution
‎02-01-2018 11:57 AM
Super User
Posts: 13,321

## Re: Converting character data to numeric

Age = input ( substr(MOTHER,1,n_char-1) , best.);

Your approach did not work because once a variable is created as character (use of substr in this case) you cannot change the variable type. So do it in one string.

OR create a temporary variable and then input that though that approach leads to many temporary variables that you may want to drop.

All Replies
Super User
Posts: 23,296

## Re: Converting character data to numeric

[ Edited ]

You can't change a variable type on the fly so you need to create a new variable. Otherwise your code and approach is correct.

``Age_Num = input(age, 2.);``

Hello everyone!

I am having trouble converting a character column to numeric. So the Smoker and Age columns were created from the MOTHER column, and now I want the age column to be numeric. I tried to do the Age = Input(Age,2.); but the result was the same.

``````data Minute1;
title 'Corrected minute1 dataset';
set Homework.minute1;
if Appearance in ('0','1','2') and Pulse in ('0','1','2') and Grimace in ('0','1','2') and Activity in ('0','1','2') and Respiration in ('0','1','2');
Score=sum(of Appearance Pulse Grimace Activity Respiration);
length Smoker \$ 1;
n_char = length(MOTHER);
Smoker = substr(MOTHER,n_char,1);
Age = substr(MOTHER,1,n_char-1);
keep Baby_ID MOTHER Appearance Pulse Grimace Activity Respiration Smoker Age;
run;
proc print data=Minute1;
run;``````

Thanks!

Solution
‎02-01-2018 11:57 AM
Super User
Posts: 13,321

## Re: Converting character data to numeric

Age = input ( substr(MOTHER,1,n_char-1) , best.);

Your approach did not work because once a variable is created as character (use of substr in this case) you cannot change the variable type. So do it in one string.

OR create a temporary variable and then input that though that approach leads to many temporary variables that you may want to drop.

Occasional Contributor
Posts: 9

## Re: Converting character data to numeric

Thank you! That worked out great!

Super User
Posts: 6,632

## Re: Converting character data to numeric

Perhaps a little simpler:

age = inputn(mother, n_char - 1);

☑ This topic is solved.