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.
April 27 – 30 | Gaylord Texan | Grapevine, Texas
Walk in ready to learn. Walk out ready to deliver. This is the data and AI conference you can't afford to miss.
Register now and lock in 2025 pricing—just $495!
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.