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

I'm trying to merge two datasets that have injury information. However, I keep getting an error message that the variable contains both character and numeric values. Here is my code:

 

libname injury "X:\filedirectory";

 

proc sort data=injury.victim;
by ID;
run;


proc sort data=injury.pdo;
by I_D;
run;


data pdo;
set injury.pdo(rename=(I_D=ID));
run;


data victim_pdo;
Merge victim pdo;
by ID;
run;

 

I've looked up this problem and tried to convert the variable "ID" in both datasets to numeric:

 

data pdo;
set injury.pdo;
ID=input(ID,8.);
put ID;
run;


data victim;
set injury.victim;
ID=input(ID,8.);
put ID;
run;

 

data victim_pdo;
Merge victim pdo;
by ID;
run;

 

This didn't work, as I'm still getting the same error message. Please help!

1 ACCEPTED SOLUTION

Accepted Solutions
ballardw
Super User

@gabbyababy wrote:

I'm trying to merge two datasets that have injury information. However, I'm unable to and keep getting an error message that the variable contains both character and numeric values. Here is my code:

 

libname injury "X:\filedirectory";

 

proc sort data=injury.victim;
by ID;
run;


proc sort data=injury.pdo;
by I_D;
run;


data pdo;
set injury.pdo(rename=(I_D=ID));
run;


data victim_pdo;
Merge victim pdo;
by ID;
run;

 

I've looked up this problem and tried to convert the variable "ID" in both datasets to numeric:

 

data pdo;
set injury.pdo;
ID=input(ID2, 8.);
put ID2;
run;


data victim;
set injury.victim;
ID=input(ID2, 8.);
put ID2;
run;

 

data victim_pdo;
Merge victim pdo;
by ID2;
run;

 

This didn't work, as I'm still getting the same error message. Please help!


As long as variable(s) with the same name and different types exist in the data set the error continues.

Once a variable exists in a data set you can not change the variable type.

To replace a variable of the same name you do something like:

/* character to numeric*/
data new;
   set old (rename= problemvar =oldvar);
   problemvar = input(oldvar, best.);
   drop oldvar;
run;

/* numeric to character*/
data new;
   set old (rename= problemvar =oldvar);
   problemvar = put(oldvar, <appropriate format>.);
   drop oldvar;
run;

I am not sure why you create a new variable(?) ID with INPUT but then use ID2 to merge things on.

Only one of those needed input.

 

This difference in variable type is usually traceable to how the data is brought into SAS. ID type variables, since you generally do not do arithmetic with them, are usually best as Character not numeric. One problem with something that read the values as numeric is that if there were leading zeroes you may have a hard time getting the exact number of leading zeroes back into a character version, especially if the character versions have different lengths:  001234 and 00000034567345 for example.

View solution in original post

5 REPLIES 5
PaigeMiller
Diamond | Level 26

I've looked up this problem and tried to convert the variable "ID" in both datasets to numeric:

 

data pdo;
set injury.pdo;
ID=input(ID2, 8.);
put ID2;
run;


data victim;
set injury.victim;
ID=input(ID2, 8.);
put ID2;
run;

 

data victim_pdo;
Merge victim pdo;
by ID2;
run;

 

Perhaps you meant

 

id2=input(id,8.);
--
Paige Miller
gabbyababy
Calcite | Level 5

Sorry, that is what I meant. I'll update my original post. It's a bit confusing, I had to change the real variable names in this post due to confidentiality.

ballardw
Super User

@gabbyababy wrote:

I'm trying to merge two datasets that have injury information. However, I'm unable to and keep getting an error message that the variable contains both character and numeric values. Here is my code:

 

libname injury "X:\filedirectory";

 

proc sort data=injury.victim;
by ID;
run;


proc sort data=injury.pdo;
by I_D;
run;


data pdo;
set injury.pdo(rename=(I_D=ID));
run;


data victim_pdo;
Merge victim pdo;
by ID;
run;

 

I've looked up this problem and tried to convert the variable "ID" in both datasets to numeric:

 

data pdo;
set injury.pdo;
ID=input(ID2, 8.);
put ID2;
run;


data victim;
set injury.victim;
ID=input(ID2, 8.);
put ID2;
run;

 

data victim_pdo;
Merge victim pdo;
by ID2;
run;

 

This didn't work, as I'm still getting the same error message. Please help!


As long as variable(s) with the same name and different types exist in the data set the error continues.

Once a variable exists in a data set you can not change the variable type.

To replace a variable of the same name you do something like:

/* character to numeric*/
data new;
   set old (rename= problemvar =oldvar);
   problemvar = input(oldvar, best.);
   drop oldvar;
run;

/* numeric to character*/
data new;
   set old (rename= problemvar =oldvar);
   problemvar = put(oldvar, <appropriate format>.);
   drop oldvar;
run;

I am not sure why you create a new variable(?) ID with INPUT but then use ID2 to merge things on.

Only one of those needed input.

 

This difference in variable type is usually traceable to how the data is brought into SAS. ID type variables, since you generally do not do arithmetic with them, are usually best as Character not numeric. One problem with something that read the values as numeric is that if there were leading zeroes you may have a hard time getting the exact number of leading zeroes back into a character version, especially if the character versions have different lengths:  001234 and 00000034567345 for example.

gabbyababy
Calcite | Level 5

Thank you so much for the explanation, it worked! Also, I apologize for the confusion in my original post. I had to change the real variable names due to confidentiality concerns and made a few typos.

tiwitopi
Calcite | Level 5

cool. I will borrow some ideas from here 😉

 

 

 

SAS Innovate 2025: Call for Content

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!

Submit your idea!

Mastering the WHERE Clause in PROC SQL

SAS' Charu Shankar shares her PROC SQL expertise by showing you how to master the WHERE clause using real winter weather data.

Find more tutorials on the SAS Users YouTube channel.

Discussion stats
  • 5 replies
  • 811 views
  • 1 like
  • 4 in conversation