DATA Step, Macro, Functions and more

Reg: Variable defined as both character and numeric error

Reply
Contributor
Posts: 38

Reg: Variable defined as both character and numeric error

I am entering the following code: 

data mtcars0;
set mtcars (keep=cars mpg cyl);
price = mpg*1000 + cyl*100;
format price dollar7.;
proc sort data=mtcars0;
by cars;run;
proc print data=mtcars0;
run;
proc contents data=mtcars0;
run;

data mtcars2;
set mtcars (drop=mpg cyl disp drat);
set mtcars0 (keep=price);
proc sort data=mtcars2;
by cars;run;
proc contents data=mtcars2;
run;
proc print data=mtcars2;
run;

 

But it's giving an error that the variable price has been defined as both character and numeric. 

Please suggest me a solution to this. 

 

Thanks in Advance

Super User
Posts: 19,855

Re: Reg: Variable defined as both character and numeric error

Posted in reply to atulsingh

Price is defined as numeric in 0 data and character in mtcars. They need to match, so convert one. 

 

Trace the variables and Types. 

Contributor
Posts: 38

Re: Reg: Variable defined as both character and numeric error

When price is defined once as numeric in data mtcars0

then why it is again getting defined as a character with KEEP option in mtcars2?

Contributor
Posts: 38

Re: Reg: Variable defined as both character and numeric error

Posted in reply to atulsingh
I need your help guys.....!!!
Super User
Super User
Posts: 7,979

Re: Reg: Variable defined as both character and numeric error

Posted in reply to atulsingh

Right, well I am going to take a guess here (per no test data) that in the original mtcars dataset there is a variable called price which is character.  Now, this code:

 

data mtcars0;
set mtcars (keep=cars mpg cyl);
price = mpg*1000 + cyl*100;

 

Creates a new dataset mtcars0 which keeps cars mpg cyl from the original dataset, and creates a new variable prie which is numeric.  Now later on we have this code:

 

data mtcars2;
set mtcars (drop=mpg cyl disp drat);
set mtcars0 (keep=price);

 

Whic does not drop the character variable price from mtcars, and keeps the numeric variable price from mtcars0, hence you have char/num for the same variable problem.

 

I am also noot sure that last datastep will do what you want, what you will end up with is twice the number of rows from mtcars, the set puts the data below, its merge that joins the two and from what you poost it looks like you just want to update price per your formula, therefore the code would look like (assuming you want numeric variable out (note how I have used the {i} button top of post to post in a code window - preserves formatting - have indented/finished blocks etc. to make reading clear):

data want;
  set mtcars (drop price);
  price=mpg*1000 + cyl*100;
  format price dollar7.;
run;

Thats it, thats all you need, drop the original value, then create a new one.

Contributor
Posts: 38

Re: Reg: Variable defined as both character and numeric error

RW9 Sir, Thanks a lot, It got resolved.
Super User
Posts: 7,844

Re: Reg: Variable defined as both character and numeric error

[ Edited ]
Posted in reply to atulsingh

Look at the contents of mtcars.

STRONG hint:

When you posted your original question, you saw this:

 

Stop right there! Before pressing POST, tick off this checklist. Does your post …

✔ Have a descriptive subject line, i.e., How do I ‘XYZ’? ✔ Use simple language and provide context? Definitely mention what version you’re on. ✔ Include code and example data? Consider using the SAS Syntax feature.

 

 

 

 

 

Note the third section, "example data"; providing your dataset mtcars (in a data step, as described here: https://communities.sas.com/t5/SAS-Communities-Library/How-to-create-a-data-step-version-of-your-dat...) would make helping you much easier.

 

PS Take a good look at Maxim 3.

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
Ask a Question
Discussion stats
  • 6 replies
  • 168 views
  • 0 likes
  • 4 in conversation