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!
@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.
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.);
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.
@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.
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.
cool. I will borrow some ideas from here 😉
Save $250 on SAS Innovate and get a free advance copy of the new SAS For Dummies book! Use the code "SASforDummies" to register. Don't miss out, May 6-9, in Orlando, Florida.
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.