Pyrite | Level 9

Variable defined as both character and numeric

Hi

I have a problem when I run this code in order to combine two datasets together by using this code, Calving_ease is the issue, this code can present only the number of calving_ease of the first dataset, but the numbers of calving_ease  in the second dataset are all missing.

regards

``````data  dataall;

set data48 data910(rename=(calving_ease=calving_easenum));

calving_ease = put(calving_ease, 7.);

drop calving_easenum;

run;``````
1 ACCEPTED SOLUTION

Accepted Solutions
Tourmaline | Level 20

Re: Variable defined as both character and numeric

assuming  calving_ease in 48 is character and 910 needs to become character

Try and let me know

``````data  dataall;

set data48 data910(in=b rename=(calving_ease=calving_easenum));

if b then calving_ease = put(calving_easenum, 7.);/*changed here to calving_easenum*/

drop calving_easenum;

run;``````
10 REPLIES 10
Diamond | Level 26

Re: Variable defined as both character and numeric

In dataset date48 you have a variable called calving_ease, in data910 you also have this, however you rename it to calving_easenum  Now when these two datasets are set together the output creates a variable called calving_ease which holds the data from the first table, and fills in empties from the second, as that variable does not exist.  This is the logic you have presented below, and hence that is what is achieved at the end.  I cannot guess what you are trying to do, nor can I understand why your thread topic is char vs num but that is not what the question you present is.  A quick check on the guidance when presenting questions would help, present test data in the form of a datastep, show what you want out at the end.  I am going to guess that in one dataset calving_ease is character, and the other is numeric?  If so convert one or the other before setting.

Pyrite | Level 9

Re: Variable defined as both character and numeric

yeas

your  guess is right because I had this issue I did this code, the normal code for  combining  data is this

data all;

set data1 data2;

run;

but I have an error with the code as the calvng_ease defined as both character and numeric.

so I tried to do it by the previous code that I posted,  but I got missing data for the second dataset

Tourmaline | Level 20

Re: Variable defined as both character and numeric

@Barkamih you need to convert one of the variables to either character or numeric to make it consistent for successful append

Pyrite | Level 9

Re: Variable defined as both character and numeric

could you please provide an example code, how to convert it ?

Tourmaline | Level 20

Re: Variable defined as both character and numeric

assuming  calving_ease in 48 is character and 910 needs to become character

Try and let me know

``````data  dataall;

set data48 data910(in=b rename=(calving_ease=calving_easenum));

if b then calving_ease = put(calving_easenum, 7.);/*changed here to calving_easenum*/

drop calving_easenum;

run;``````
Pyrite | Level 9

Re: Variable defined as both character and numeric

calving_ease of the second dataset are all missing by using this code

Tourmaline | Level 20

Re: Variable defined as both character and numeric

Did you notice my edit in the previous message. Anyway, here is a demo

``````data have;
input var \$;
datalines;
1
2
3
;

data have1;
input var;
datalines;
1
2
3
;

data want;
set have have1(in=b rename=(var=_var));
if b then var=put(_var,1.);
drop _var;
run;``````
Tourmaline | Level 20

Re: Variable defined as both character and numeric

Your approach is bang on correct. I think you are having a very minor problem:

``calving_ease = put(calving_ease, 7.);``

I changed the same to

``calving_ease = put(calving_easenum, 7.);``

``set data48 data910(rename=(calving_ease=calving_easenum));``

I changed this to

``set data48 data910(in=b rename=(calving_ease=calving_easenum));``

Pyrite | Level 9

Re: Variable defined as both character and numeric

thank you much, I appreciate that, you are done a very good jobe with me, and I'm sorry if I took you time

best regards

thanks again

Barkamih

Tourmaline | Level 20

Re: Variable defined as both character and numeric

@Barkamih  Hahaha nothing to be apologetic. We all learn and I am sure you share your knowledge too with tohers. Knowledge is meant to be shared and learning good things should a right. Take care and have a nice day!

Discussion stats
• 10 replies
• 1339 views
• 6 likes
• 3 in conversation