0
我是SAS新手,但知道SQL,因此試圖使用SQL代碼編寫proc sql
代碼,並意識到PARTITION by
在SAS中不可用。SQL到PROC SQL-分區通過替代(最小的情況)
表
Customer_id Item_type Order Size Date ….
1. A401 Fruit Small 3/14/2016 ….
2. A401 Fruit Big 5/22/2016 ….
3. A401 Vegetable Small 7/12/2016 ….
4. B509 Vegetable Small 3/25/2015 ….
5. B509 Vegetable Big 3/15/2014 ….
6. B509 Vegetable Small 3/1/2014 ….
說明
Customer_id Item_Type Count Reason
1.A401 Fruit 2 X-WRONG-because date corresponding big item is later than others in group
2.B509 Vegetable 2 RIGHT-Note that count is 2 only because one of the dates is earlier than the Big corresponding item(3/1/2014 is earlier than 3/15/2014)
SQL輸出
Customer_id Item_Type Count
1.B509 Vegetable 2
select t.customer_id, t.item_type, count(*)
from (select t.*,
min(case when OrderSize = 'Big' then date end) over (partition by customer_id, item_type) as min_big
from t
) t
where date > min_big
group by t.customer_id, t.item_type;
got main.date doesnot exist error。 – viji
哎呀!請參閱編輯。在派生表的「SELECT」中添加* date *。替換你的星號,我忘了包括那個重要的人。 – Parfait