Proc Optmodel SAS Variable not unique - optimization

I am using proc optmodel to solve a problem in which several items must be priced the same within the same location (let's say they are different colors of same product and are not currently priced the same). I know that volume will increase/decrease depending on direction of price change, and I have some MIN/MAX constraints as well.
The problem I am running into is that the procedure is only reading one group of unique SKUs....I think because they repeat. How can I get the procedure to optimize all unique combinations of SKU/LOCATION? I tried just changing the item numbers, which of course works, but is not practical for my business solution. Thanks.
data input_data;
input SKU DESC $ LOCATION $ OLD_PRICE MIN MAX LIFT OLD_UNITS;
cards;
111 black NY 12.99 10 15 1.3 100
222 white NY 13.45 11 15 .9 150
333 red NY 13.29 13 15 1.6 200
111 black DC 11.75 10 14 1.2 300
222 white DC 11.75 10 14 1.5 100
333 red DC 11.99 10 14 1.7 140
111 black LA 14.21 12 17 2.0 600
222 white LA 14.79 14 17 1.5 500
333 red LA 15.99 13 17 .3 200
444 orange LA 14.11 12 17 .6 300
;
run;
proc optmodel;
set<num> SKU;
string LOCATION{SKU};
string DESC{SKU};
set LOCATIONS = setof{i in SKU} LOCATION[i];
set SKUperLOCATION{gi in LOCATIONS} = {i in SKU: LOCATION[i] = gi};
number OLD_PRICE{SKU};
number MIN{SKU};
number MAX{SKU};
var NEW_PRICE{gi in LOCATIONs} >= max{i in SKUperLOCATION[gi]} MIN[i] <= min{i in SKUperLOCATION[gi]} MAX[i];
impvar NEW_PRICEbySKU{i in SKU} = NEW_PRICE[LOCATION[i]];
number LIFT{SKU};
number OLD_UNITS{SKU};
read data input_data into
SKU=[SKU]
DESC
LOCATION
OLD_PRICE
MIN
MAX
LIFT
OLD_UNITS;
max sales=sum{gi in LOCATIONs}
sum{i in SKUperLOCATION[gi]}
(NEW_PRICE[gi])*(1-(NEW_PRICE[gi]-OLD_PRICE[i])*LIFT[i]/OLD_PRICE[i])*OLD_UNITS[i];
expand;
solve;
create data results_FAM_maxsales
from [SKU]={SKU}
DESC
LOCATION
OLD_PRICE
NEW_PRICE=NEW_PRICEbySKU
MIN
MAX
LIFT
OLD_UNITS;
print NEW_PRICE sales;
quit;

One way would be to set your unique key to be SKU & Location. I haven't used OPTMODEL in a while, but something like this should work.
set<num,str> SKU_Loc;
num old_price{SKU_Loc};
<code>
read data input_data into SKU_Loc = [SKU Location];
<code>
Then change the rest of the code to reference the unique combination of SKU & location.

Related

Finding Max Price and displaying multiple columns SQL

I have a table that looks like this:
customer_id item price cost
1 Shoe 120 36
1 Bag 180 50
1 Shirt 30 9
2 Shoe 150 40
3 Shirt 30 9
4 Shoe 120 36
5 Shorts 65 14
I am trying to find the most expensive item each customer bought along with the cost of item and the item name.
I'm able to do the first part:
SELECT customer_id, max(price)
FROM sales
GROUP BY customer_id;
Which gives me:
customer_id price
1 180
2 150
3 30
4 120
5 65
How do I get this output to also show me the item and it's cost in the output? So output should look like this...
customer_id price item cost
1 180 Bag 50
2 150 Shoe 40
3 30 Shirt 9
4 120 Shoe 36
5 65 Shorts 14
I'm assuming its a Select statement within a Select? I would appreciate the help as I'm fairly new to SQL.
One method that usually has good performance is a correlated subquery:
select s.*
from sales s
where s.price = (select max(s2.price)
from sales s2
where s2.customer_id = s.customer_id
);

how to split one value from sql database into two rows

how to split one value from sql database and devide into two rows (one row set 1000000, one row is the balance) Others less than 1000000.or any idea using coldfusion . Thanks in advance.
Example
No. Code Name Account No Total
1 123 black 123456789 160000.00
2 124 red 111111222 5200.00
3 124 blue 444555666 121000.00
I want This result
No. Code Name Account No Total
1 123 black 123456789 100000.00
2 123 black 123456789 60000.00
3 124 red 111111222 5200.00
4 124 blue 444555666 100000.00
5 124 blue 444555666 21000.00
First you need to insert a new row:
INSERT INTO TABLE (CODE, NAME, ACCOUNT_NO, TOTAL)
SELECT
CODE,
NAME,
ACCOUNT_NO,
TOTAL MOD 100000 AS NEW_TOTAL
FROM TABLE
WHERE TOTAL > 100000;
That command will insert the second row. Now you need to fix the first row.
UPDATE TABLE
SET TOTAL = TOTAL - (TOTAL MOD 100000)
WHERE TOTAL > 100000;

Reordering groups within a categorical variable

I'm struggling to reorder the categories in an income group variable.
It is currently in alphabetical order but I wish to change it to be permanently in order of amount (so moving £100 000 and greater to the bottom)
Income Group | Freq. Percent Cum.
Under £20 000 | 11 9.73 9.73
£100 000 and greater | 7 6.19 15.93
£20 000 to 29 999 | 18 15.93 31.86
£30 000 to 49 999 | 38 33.63 65.49
£50 000 to 74 999 | 27 23.89 89.38
£75 000 to 99 999 | 12 10.62 100.00
Create a new variable and assign each of the categories a number. For example,
gen order = 1 if income=="Under £20 000"
replace order = 2 if income=="£20 000 to 29 999"
...
replace order = 6 if income=="£100 000 and greater"
sort order
drop order
If the income variable is numeric, rather than string, sub in the numeric values above.

MS Access, Excel, SQL, and New Tables

I'm just starting out with MS Access 2010 and have the following setup. 3 excel files: masterlist.x (which contains every product that I sell), vender1.x (which contains all products from vender1, I only sell some of these products), and vender2.x (again, contains all products from vender2, I only sell some of these products). Here's an example data collection:
masterlist.x
ID NAME PRICE
23 bananas .50
33 apples .75
35 nuts .87
38 raisins .25
vender1.x
ID NAME PRICE
23 bananas .50
25 pears .88
vender2.x
ID NAME PRICE
33 apples .75
35 nuts .87
38 raisins .25
49 kiwis .88
The vender lists get periodically updated with new items for sell and new prices. For example, vender1 raises the price on bananas to $.75, my masterlist.x would need to be updated to reflect this.
Where I'm at now: I know how to import the 3 excel charts into Access. From there, I've been researching if I need to setup relationships, create a macro, or a SQL query to accomplish my goals. Not necessarily looking for a solution, but to be pointed in the right direction would be great!
Also, once the masterlist.x table is updated, what feature would I use to see which line items were affected?
Update: discovered SQL /JOIN/ and have the following:
SELECT * FROM master
LEFT JOIN vender1
ON master.ID = vender1.ID
where master.PRICE <> vender1.PRICE;
This gives me the output (for the above scenario)
ID NAME PRICE ID NAME PRICE
23 bananas .50 23 bananas .75
What feature would instead give me:
masterlist.x
ID NAME PRICE
23 bananas .75
33 apples .75
35 nuts .87
38 raisins .25
Here is a heads up since you were asking for ideas to design. I don't really fancy your current table schema. The following queries are built in SQL Server 2008, the nearest syntax that I could get in sqlfiddle to MS Access SQL.
Please take a look:
SQLFIDDLE DEMO
Proposed table design:
vendor table:
VID VNAME
1 smp farms
2 coles
3 cold str
4 Anvil NSW
product table:
PID VID PNAME PPRICE
203 2 bananas 0.5
205 2 pears 0.88
301 3 bananas 0.78
303 3 apples 0.75
305 3 nuts 0.87
308 3 raisins 0.25
409 4 kiwis 0.88
masterlist:
ID PID MPRICE
1 203 0.5
2 303 0.75
3 305 0.87
4 308 0.25
Join queries can easily update your masterlist now. for e.g.:
When the vendor updates their prices for the fruits they provide you. Or when they stop supply on that product. You may use where clauses to add the conditions to the query as you desire.
Query:
SELECT m.id, p.vid, p.pname, p.pprice
FROM masterlist m
LEFT JOIN product p ON p.pid = m.pid
;
Results:
ID VID PNAME PPRICE
1 2 bananas 0.5
2 3 apples 0.75
3 3 nuts 0.87
4 3 raisins 0.25
Please comment. Happy to help you if have any doubts.

How do I loop through a table until condition reached

I have a table product, pick_qty, shortfall, location, loc_qty
Product Picked Qty Shortfall Location Location Qty
1742 4 58 1 15
1742 4 58 2 20
1742 4 58 3 15
1742 4 58 4 20
1742 4 58 5 20
1742 4 58 6 20
1742 4 58 7 15
1742 4 58 8 15
1742 4 58 9 15
1742 4 58 10 20
I want a report to loop around and show the number of locations and the quantity I need to drop to fulfil the shortfall for replenishment. So the report would look like this.
Product Picked Qty Shortfall Location Location Qty
1742 4 58 1 15
1742 4 58 2 20
1742 4 58 3 15
1742 4 58 4 20
Note that it is best not to think about SQL "looping through a table" and instead to think about it as operating on some subset of the rows in a table.
What it sounds like you need to do is create a running total that tells how many of the item you would have if you were to take all of them from a location and all of the locations that came before the current location and then check to see if that would give you enough of the item to fulfill the shortfall.
Based on your example data, the following query would work, though if Locations aren't actually numerics then you would need to add a row number column and tweak the query a bit to use the row number instead of the Location Number; It would still be very similar to the query below.
SELECT
Totals.Product, Totals.PickedQty, Totals.ShortFall, Totals.Location, Totals.LocationQty
FROM (
SELECT
TheTable.Product, TheTable.PickedQty, TheTable.ShortFall,
TheTable.Location, TheTable.LocationQty, SUM(ForRunningTotal.LocationQty) AS RunningTotal
FROM TheTable
JOIN TheTable ForRunningTotal ON TheTable.Product = ForRunningTotal.Product
AND TheTable.Location >= ForRunningTotal.Location
GROUP BY TheTable.Product, TheTable.PickedQty, TheTable.ShortFall, TheTable.Location, TheTable.LocationQty
) Totals
-- Note you could also change the join above so the running total is actually the count of only the rows above,
-- not including the current row; Then the WHERE clause below could be "Totals.RunningTotal < Totals.ShortFall".
-- I liked RunningTotal as the sum of this row and all prior, it seems more appropriate to me.
WHERE Totals.RunningTotal - Totals.LocationQty <= Totals.ShortFall
AND Totals.LocationQty > 0
Also - as long as you are reading my answer, an unrelated side-note: Based on the data you showed above, your database schema isn't normalized as far as it could be. It seems like the Picked Quantity and the ShortFall actually depend only on the Product, so that would be a table of its own, and then the Location Quantity depends on the Product and Location, so that would be a table of its own. I'm pointing it out because if your data contained different Picked Quantities/ShortFall for a single product, then the above query would break; This situation would be impossible with the normalized tables I mentioned.