Re: [程式] sas sql語法
我的寫法是算出type1, type2, type3後用full join拼在一起
原始資料名稱為data,語法為
proc sql;
create table table1 as
select data1.ID, data1.casetype, type1, type2, type3
from
(select data.ID, data.casetype, a.type1, b.type2
from
data,
(select ID, casetype,count(casetype) as type1
from data
where casetype = 1
group by ID, casetype) as a full join
(select ID, casetype,count(casetype) as type2
from data
where casetype = 2
group by ID, casetype) as b on a.ID=b.ID
where data.ID = a.ID) as data1 full join
(select ID, casetype,count(casetype) as type3
from data
where casetype = 3
group by ID, casetype) as c on data1.ID=c.ID;
看起來很多很繁雜, 不過整個過程大概分兩個步驟
紅色字體內用full join把兩個view合併起來, 得到
的data1長這樣
ID casetype type1 type2
a 1 2 .
a 1 2 .
b 1 1 1
b 2 1 1
b 3 1 1
再把整個data1和第三個view合併起來, 最後的table1
應該就是你想要的結果了
--
※ 發信站: 批踢踢實業坊(ptt.cc)
◆ From: 169.231.32.90
※ 編輯: svin 來自: 169.231.32.90 (09/25 13:57)
討論串 (同標題文章)
完整討論串 (本文為第 3 之 3 篇):
程式
1
5