DATA Step, Macro, Functions and more

Variable defined as both character and numeric

Accepted Solution Solved
Reply
Contributor
Posts: 56
Accepted Solution

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;

Accepted Solutions
Solution
‎04-12-2018 12:54 PM
PROC Star
Posts: 1,555

Re: Variable defined as both character and numeric

[ Edited ]

I made a small correction to your code:

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;

View solution in original post


All Replies
Super User
Super User
Posts: 9,397

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.

Contributor
Posts: 56

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 

 

 

PROC Star
Posts: 1,555

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

Contributor
Posts: 56

Re: Variable defined as both character and numeric

Posted in reply to novinosrin

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

 

Solution
‎04-12-2018 12:54 PM
PROC Star
Posts: 1,555

Re: Variable defined as both character and numeric

[ Edited ]

I made a small correction to your code:

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;
Contributor
Posts: 56

Re: Variable defined as both character and numeric

Posted in reply to novinosrin

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

PROC Star
Posts: 1,555

Re: Variable defined as both character and numeric

[ Edited ]

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;
PROC Star
Posts: 1,555

Re: Variable defined as both character and numeric

Posted in reply to novinosrin

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

 

instead of

calving_ease = put(calving_ease, 7.);

I changed the same to 

 

calving_ease = put(calving_easenum, 7.);

 plus instead of 

 

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

I changed this to 

 

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

 

Contributor
Posts: 56

Re: Variable defined as both character and numeric

Posted in reply to novinosrin

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 

PROC Star
Posts: 1,555

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!

☑ This topic is solved.

Need further help from the community? Please ask a new question.

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