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!
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.
SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!
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.
Ready to level-up your skills? Choose your own adventure.