BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
newbie_grad
Fluorite | Level 6

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!

1 ACCEPTED SOLUTION

Accepted Solutions
ballardw
Super User

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

4 REPLIES 4
Reeza
Super User

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!



 

ballardw
Super User

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.

newbie_grad
Fluorite | Level 6

Thank you! That worked out great!

Astounding
PROC Star

Perhaps a little simpler:

 

age = inputn(mother, n_char - 1);

SAS Innovate 2025: Register Now

Registration is now open for SAS Innovate 2025 , our biggest and most exciting global event of the year! Join us in Orlando, FL, May 6-9.
Sign up by Dec. 31 to get the 2024 rate of just $495.
Register now!

How to Concatenate Values

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

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