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

Hi all,

I got error by using both of theses codes in order to merge two datasets
Data data2004; merge d2004 county; by herd; run; quit; data Data2004; merge D2004(IN=K) county(IN=Q); if K and Q; by herd; run; quit;


and this the log:


355 data Data2004;
356 merge D2004(IN=K) county(IN=Q);
ERROR: Variable HERD has been defined as both character and numeric.
357 if K and Q; by herd; run;

 regards 

 

Ibrahim 


1 ACCEPTED SOLUTION
13 REPLIES 13
PaigeMiller
Diamond | Level 26

I think the error message is relatively clear. You have to fix the problem so in both data sets HERD is numeric, or in both data sets HERD is character.

--
Paige Miller
Barkamih
Pyrite | Level 9

i did this code but to convert the variable, but how to complete it correctly?
data county;
herd = ' ';
new_herd = input(herd, );
drop herd;
rename new_herd = herd;
run;

Reeza
Super User
How did you create these data sets initially? Did you verify the variable types were imported correctly and what you need?

If you imported these files via CSV or a text data set its better to go back to that step and fix it there. If not, youc an convert to a new variable but you need to rename it before you bring it in.

data county;
set oldData (rename = Herd=OldHerd);
*convert to numeric;
Herd =input(herd, 8.);
drop oldHerd;
run;
Reeza
Super User

How did you create these data sets initially? Did you verify the variable types were imported correctly and what you need?
If not, you need to go back and do that step.

 


@Barkamih wrote:

Hi all,

I got error by using both of theses codes in order to merge two datasets
Data data2004; merge d2004 county; by herd; run; quit; data Data2004; merge D2004(IN=K) county(IN=Q); if K and Q; by herd; run; quit;


and this the log:


355 data Data2004;
356 merge D2004(IN=K) county(IN=Q);
ERROR: Variable HERD has been defined as both character and numeric.
357 if K and Q; by herd; run;

 regards 

 

Ibrahim 



 

Barkamih
Pyrite | Level 9

 

how to make it correct?

 

data county;
herd = '             ';
new_herd = input(herd,               );
drop herd;
rename new_herd = herd;
run;

 

Barkamih
Pyrite | Level 9

 

Hi, I'm really sorry about that, I was so busy yesterday, 

 

this is an example of the herd variable

 

so, I have big data that has these numbers and I want to merge some information to these file by a herd.

 

Herd number means Farm name or farm id, 

 

I got these data in excel file form a company.

 

data was imported to SAS as a CSV file type. 

 

I'm really confused about how to merge these file together. 

 

best regards to everyone 

 

Ibrahim 

 

 

Herd 
5179940
5180223
5599499
60001827
390464233
700114030
1206840374
PaigeMiller
Diamond | Level 26

It doesn't help if you type in the data from ONE of the data sets. Typing in the data doesn't help anyway. You have to show us the code that shows how BOTH (not one) data sets were created. Or show us PROC CONTENTS output of BOTH data sets.

 

 

--
Paige Miller
Barkamih
Pyrite | Level 9

 this is data county 

Alphabetic List of Variables and Attributes# Variable Type Len Format Informat 1
     
herdNum8BEST12.BEST32.

Sort InformationSortedbyValidatedCharacter Set
herd
YES
ANSI

 

 

 this is data d 2004

Alphabetic List of Variables and Attributes# Variable Type Len Format Informat
1
HERDChar42$42.$42.

 

 

 

 

Tom
Super User Tom
Super User

@Barkamih wrote:

 

data was imported to SAS as a CSV file type. 

Since you are reading from a text file you can have complete control over how the variables are created.

You probably used PROC IMPORT to read the files.  That will just generate a data step to read the file.  Copy the data steps and modify the definition of HERD to be the same for both.  Then re-run the modified data steps and now your HERD variable will be compatible.

Shivam
Calcite | Level 5

you need to fix the data type in your contributing tables, there is no issue with your merge code

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
  • 13 replies
  • 1921 views
  • 2 likes
  • 6 in conversation