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!
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.
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.
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!
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.
Thank you! That worked out great!
Perhaps a little simpler:
age = inputn(mother, n_char - 1);
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!
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.