BookmarkSubscribeRSS Feed
jamieflynn
Calcite | Level 5

Hello everyone,

 

I'm trying to merge 2 datasets and then create a new variable in the merged dataset. 

 

The new variable is Status where if any model in Inventory dataset does not appear in Purchase dataset, Status would be "Not Sold", otherwise Status is "Sold". 

 

So I wrote it as if Quantity is missing, then Status ="Not Sold", else Status = "Sold".

 

Here is what I have:

 

/*Sort Inventory and Purchase datasets by Model*/
proc sort data=BSTA445.inventory out=invt_sorted;
by Model;
run;

proc sort data=BSTA445.purchase out=pur_sorted;
by Model;
run;


/*Merge 2 sorted datasets*/
data notpurch;
	merge invt_sorted (in=InInvent) pur_sorted (in=inPurch);
	by Model;
	if missing(Quantity) then Status = "Not Sold";
	else Status = "Sold";
	keep Model Price;
run;

title "List of unpurchased Models and respective Prices";
proc print data=notpurch noobs;
run;
title;

I don't know why in the merged dataset, there is no new variable created.

 

Can anyone please explain what has gone wrong in my code?

 

Thanks a lot!

 

 

1 REPLY 1
ballardw
Super User

 


data notpurch;
	merge invt_sorted (in=InInvent) pur_sorted (in=inPurch);
	by Model;
	if missing(Quantity) then Status = "Not Sold";
	else Status = "Sold";
	keep Model Price;
run;

 

Your KEEP statement says not to keep the STATUS variable.

Ready to join fellow brilliant minds for the SAS Hackathon?

Build your skills. Make connections. Enjoy creative freedom. Maybe change the world. Registration is now open through August 30th. Visit the SAS Hackathon homepage.

Register today!
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
  • 1 reply
  • 375 views
  • 2 likes
  • 2 in conversation