BookmarkSubscribeRSS Feed
veda8
Fluorite | Level 6

These are the two questions given

1) Explicitly output rows for products with sales (matches 
products in the profit table) to the profit_detail table. 
*2) Explicitly output rows for products without sales to the *
 product_nosales table.

 

 

proc sort data=crb.profit out=profit_sort;
by Product_ID;
run;

proc sort data=crb.products out=products_sort;
by Product_ID;
run;

data profit_detail product_nosales;
merge profit_sort(in=inprof) products_sort(in=inprod);
by Product_ID;
if inprof=1 and inprod=1 then output profit_detail;
else if inprof=0 and inprod=1 then output product_nosales;

(i didnt understood how from the question this statment was written .I felt the question was not quite clear,Can someone please explain)
run;

proc print data=product_detail;
run;

proc print data=product_nosales;
run;

@Tom @FreelanceReinh 

3 REPLIES 3
Tom
Super User Tom
Super User

Your answer looks good to me. 

 

When I am answering a question that seems be imprecisely worded I like to explicitly state how I have interpreted the question and any assumptions I have to make to be able to provide an answer.

 

 

veda8
Fluorite | Level 6

the solution isnt written by me.I am not understanding from the question how they derived this solution ie. especially the second if statement

Kurt_Bremser
Super User

Take a good look at your code:

data profit_detail product_nosales;
merge profit_sort(in=inprof) products_sort(in=inprod);
by Product_ID;
if inprof=1 and inprod=1 then output profit_detail;
else if inprof=0 and inprod=1 then output product_nosales;
run;

In both conditions that lead to an output, inprod must be 1 (true), so you can move that up and use a subsetting if:

if inprod;

and then direct the output depending on inprof, so the complete code would be:

data
  profit_detail
  product_nosales
;
merge
  profit_sort (in=inprof)
  products_sort (in=inprod)
;
by Product_ID;
if inprod;
if inprof
then output profit_detail;
else output product_nosales;
run;

sas-innovate-2026-white.png



April 27 – 30 | Gaylord Texan | Grapevine, Texas

Registration is open

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!

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 3 replies
  • 1193 views
  • 0 likes
  • 3 in conversation