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-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

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
  • 3 replies
  • 492 views
  • 0 likes
  • 3 in conversation