Converting character data to numeric

Accepted Solution Solved
Reply
Occasional Contributor
Posts: 9
Accepted Solution

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.

age.jpg

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
Highlighted
Solution
3 weeks ago
Super User
Posts: 12,148

Re: Converting character data to numeric

Posted in reply to newbie_grad

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.

View solution in original post


All Replies
Super User
Posts: 21,564

Re: Converting character data to numeric

[ Edited ]
Posted in reply to newbie_grad

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.);

 


newbie_grad wrote:

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.

age.jpg

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!



 

Highlighted
Solution
3 weeks ago
Super User
Posts: 12,148

Re: Converting character data to numeric

Posted in reply to newbie_grad

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,011

Re: Converting character data to numeric

Posted in reply to newbie_grad

Perhaps a little simpler:

 

age = inputn(mother, n_char - 1);

☑ This topic is solved.

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

Discussion stats
  • 4 replies
  • 165 views
  • 6 likes
  • 4 in conversation