You are on page 1of 2

3-Topping Pizzas - SAS

Problem
Given a list of pizza toppings, consider all the possible 3-topping pizzas, and print out the
total cost of those 3 toppings.

Input

Topping Name Ingredient Cost


Pepperoni 0.5
Sausage 0.7
Chicken 0.55
Extra Cheese 0.4

Output

pizza costing
Cheese,Chicken,Pepperoni 1.45
Cheese,Chicken,Sausage 1.65
Cheese,Pepperoni,Sausage 1.6
Chicken,Pepperoni,Sausage 1.75

Dataset

data pizza_toppings;
attrib
Topping_name Label="Topping Name"
Ingredient_cost Label="Ingredient Cost";
infile datalines dsd dlm=',' truncover;
input Topping_name :$30. Ingredient_cost;
datalines;
"Pepperoni",0.50
"Sausage",0.70
"Chicken",0.55
"Cheese",0.40
;

Solution
proc sort data=pizza_toppings; by Topping_name; run;
proc sql noprint;
select distinct cat("'", strip(Topping_name), "'") as Topings,
count(distinct Topping_name) as Pizza_toppings
into :Topings separated by ' ', :n from Pizza_toppings;
quit;

proc transpose data=pizza_toppings


out=trans_pizza_toppings(drop=_:);
id Topping_name;
var Ingredient_cost;
run;
data _temp;
array Pizza[&n] $50 (&Topings);
k=3;
ncomb=comb(dim(Pizza), k);
do j=1 to ncomb+1;
call allcomb(j, k, of Pizza[*]);
output;
end;
drop Pizza4 k ncomb j;
call sortc(of Pizza:);
merge trans_pizza_toppings;
run;
proc sort data=_temp nodup; by _all_; run;

data __temp;
set _temp;
pizza = catx(',',Pizza1 ,Pizza2, Pizza3 );
if ^missing(sum(of _numeric_));
array pizzas(*) $30. Pizza1-Pizza3;
costing = 0;
do i = 1 to dim(pizzas);
if ^missing(vvaluex(pizzas(i)));
costing + vvaluex(pizzas(i));
end;
Keep Pizza Costing;
proc print; run;

Output

-Vijay Pratap

You might also like