BookmarkSubscribeRSS Feed
atulsingh
Obsidian | Level 7

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

6 REPLIES 6
Reeza
Super User

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

 

Trace the variables and Types. 

atulsingh
Obsidian | Level 7

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?

atulsingh
Obsidian | Level 7
I need your help guys.....!!!
RW9
Diamond | Level 26 RW9
Diamond | Level 26

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.

atulsingh
Obsidian | Level 7
RW9 Sir, Thanks a lot, It got resolved.
Kurt_Bremser
Super User

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.

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 6 replies
  • 1016 views
  • 0 likes
  • 4 in conversation