[英]Primary Key using SAS
我希望SAS程序從基於速率最高的行的數據集中找到主要服務,但是當出現平局時,將第一行作為主要服務。 請參閱下面的數據集。
ID line rate outcome
TTT 1 .95 Primary
TTT 2 .43
RRR 1 .75 Primary
RRR 2 .75
AAA 1 .23
AAA 2 .12
AAA 3 .65 Primary
我創建了兩個具有相同數據的表,然后使用以下內容
使用的代碼:
proc sql;
create table test as
select a.ID, a.line, a.rate
(case
when ((a.ID = b.ID) and (a.rate ge b.rate)) then "Primary"
else ' '
end) as outcome
from table1 a,table2 b
where a.ID = b.ID;
quit;
這可能不是最佳解決方案,但我建議您分兩步進行。
這是未經測試的示例代碼:
*Calculate max rate per ID;
proc sql;
create table temp1 as
select a.*, max(rate) as max_rate
from table1
group by ID
order by ID, line;
quit;
*Assign primary key;
data want;
set temp1;
by ID;
retain flag 0;
if first.ID then flag=0;
if rate=max_rate and flag=0 then do;
flag=1;
key='Primary';
end;
run;
proc print data=want;
run;
另一個選項是帶有排序的數據步驟,排序使您擁有最大的數據,最小的行位於頂部,並使用BY處理將鍵設置為Primary。
proc sort data=have;
by ID descending rate line;
run;
data want;
set have;
by id;
if first.id then key='Primary';
run;
proc sort data=want;
by id line;
run;
proc print data=want;
run;
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.