SAS中分组值的组合
我只是试图在以下数据集中的记录仅因列类型不同的情况下找到各种组合。例如:前三行只因列类型不同而不同 给定数据集SAS中分组值的组合,sas,proc-sql,Sas,Proc Sql,我只是试图在以下数据集中的记录仅因列类型不同的情况下找到各种组合。例如:前三行只因列类型不同而不同 给定数据集 ins_id ins_number type 1234 1234-1234-1 AU 1234 1234-1234-1 HM 1234 1234-1234-1 RE 567 567-567-12 TL 567 567-567-13 TL 9101 9101-1234-1 AU 9101
ins_id ins_number type
1234 1234-1234-1 AU
1234 1234-1234-1 HM
1234 1234-1234-1 RE
567 567-567-12 TL
567 567-567-13 TL
9101 9101-1234-1 AU
9101 9101-1234-1 TX
9101 9101-1234-1 CN
8854 8854-1234-1 TX
8854 8854-1234-1 GB
8854 8854-1234-1 RE
8854 8854-1234-2 RX
Expected Output:
combination count
AU,HM,RE 1
AU,TX,CN 1
TX,GB,RE 1
我尝试编写查询,但没有得到所需的输出,请帮助:
proc sql;create table tst as select cp.type,
count(distinct ins_id)
from (select distinct fac_prod_typ from dataset3a) cp cross join
(select distinct ins_number from dataset3a) pes left join
dataset3a
on dataset3a.type = cp.type and
dataset3a.ins_number = pes.ins_number
group by cp.type, pes.ins_number;quit;
使用第一/最后一个逻辑在这里很好。 要获得计数,请在最终输出上运行PROC FREQ,这也将允许您识别混音的ins_id
data have;
informat ins_id $8. ins_number $25. type $2.;
input ins_id $ ins_number $ type $;
cards;
1234 1234-1234-1 AU
1234 1234-1234-1 HM
1234 1234-1234-1 RE
567 567-567-12 TL
567 567-567-13 TL
9101 9101-1234-1 AU
9101 9101-1234-1 TX
9101 9101-1234-1 CN
8854 8854-1234-1 TX
8854 8854-1234-1 GB
8854 8854-1234-1 RE
;;;;
data want;
set have;
by ins_id ins_number type notsorted;
retain combo;
length combo $256.;
if first.ins_number then call missing(combo);
if first.type then combo = catx(", ", combo, type);
if last.ins_number and countw(combo)>1 then output;
run;
您需要对数据进行排序,以确保类型列表在所有ID上保持一致。 道指在集合上的循环。。。;通过每个组将输出一个类型列表。 最后一步是使用Proc FREQ计算每个类型列表的ID数 例如:
data have;
informat ins_id $8. ins_number $25. type $2.;
input ins_id $ ins_number $ type $;
cards;
1234 1234-1234-1 AU
1234 1234-1234-1 HM
1234 1234-1234-1 RE
567 567-567-12 TL
567 567-567-13 TL
9101 9101-1234-1 AU
9101 9101-1234-1 TX
9101 9101-1234-1 CN
8854 8854-1234-1 TX
8854 8854-1234-1 GB
8854 8854-1234-1 RE
8854 8854-1234-2 RX
;
/* force specific ordering of type within group id and number */
/* necessary for proper frequency counting */
/* if sequence of types IS important do not sort and data step by ... NOTSORTED */
proc sort data=have;
by ins_id ins_number type;
run;
data types(keep=types);
length types $200;
do until (last.ins_number);
set have;
by ins_id ins_number;
if indexw(types, type) = 0 then types = catx(',',types,type);
end;
if index(types,',') then output;
run;
proc freq noprint data=types;
table types / out=types_counts(keep=types count) ;
run;
在这里,使用分组处理,数据步骤会容易得多。这是一个选项吗?是的,只要输出正确就可以。如果组中的不同类型中存在重复类型,您想知道该类型出现了多少次吗?例如,一个包含6条记录、3种类型和一些重复的id可以总结为AU、HM3、RE2Hi Richard,如果我们像我在原始帖子中所做的那样再添加一行,那么它就不起作用了。那个测试用例失败了。所以基本上,对于8854,应该有TX、GB、RE,而不是RXI。BY组需要是两个变量INS\U ID INS\U编号。会修好的。是的,这很有效。你太棒了,谢谢你,如果我们像我在原来的帖子中那样多加一行的话,那是行不通的。那个测试用例失败了。所以基本上,对于8854,应该有TX、GB、RE而不是RXS,所以你只想要前三个?按顺序还是由于其他原因排除RX?假设INS_编号是关键,只需更改第一个/最后一个以引用唯一标识分组的最后一个变量即可。