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-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

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