[英]How to count how many time some value appeard with defined ID in PROC SQL in SAS Enterprise Guide?
I have Table in SAS Enterprise Guide like below: Data type:我在 SAS 企业指南中有表,如下所示: 数据类型:
ID - numeric ID - 数字
SERVICE - character服务 - 性格
ID ![]() |
SERVICE![]() |
---|---|
123 ![]() |
P1 ![]() |
123 ![]() |
P1 ![]() |
123 ![]() |
G ![]() |
444 ![]() |
AB ![]() |
56 ![]() |
c2 ![]() |
And I need to know how many time each ID bought each SERVICE, so as a result I need something like below:而且我需要知道每个 ID 购买了每个 SERVICE 的次数,因此我需要以下内容:
ID | P1| G | AB | c2
----|---|---|----|---
123 | 2 | 1 | 0 | 0
444 | 0 | 0 | 1 | 0
56 | 0 | 0 | 0 | 1
How can I do that in PROC SQL in SAS Enterprise Guide?如何在 SAS 企业指南中的 PROC SQL 中做到这一点?
Try this尝试这个
data have;
input ID SERVICE $;
datalines;
123 P1
123 P1
123 G
444 AB
56 c2
;
proc summary data = have nway;
class ID Service;
output out = temp(drop = _type_ rename = _freq_ = n);
run;
proc transpose data = temp out = want(drop = _:);
by ID ;
id Service;
var n;
run;
If you just want to make that REPORT then you can do it directly from the data use PROC TABULATE.如果您只想制作该报告,那么您可以使用 PROC TABULATE 直接从数据中完成。
data have;
input ID SERVICE $;
cards;
123 P1
123 P1
123 G
444 AB
56 c2
;
proc tabulate data=have ;
class id service;
table id,service*N / printmiss misstext='0';
keylabel n=' ';
run;
If you need to save the counts into a dataset then use PROC FREQ to count.如果您需要将计数保存到数据集中,请使用 PROC FREQ 进行计数。
proc freq data=have ;
tables id*service / noprint out=want sparse;
run;
You could then make that REPORT using PROC REPORT.然后,您可以使用 PROC REPORT 制作该报告。 ID is the grouping variable and SERVICE is the ACROSS variable.
ID 是分组变量,SERVICE 是 ACROSS 变量。
proc report data=want ;
column id count,service;
define id / group;
define service / across ' ';
run;
If you need an actual dataset (why???) then use PROC TRANSPOSE.如果你需要一个实际的数据集(为什么???)然后使用 PROC TRANSPOSE。 But then the values of SERVICE have to be valid SAS names so they can be used to name the variables in the dataset.
但是 SERVICE 的值必须是有效的 SAS 名称,以便它们可以用于命名数据集中的变量。
proc transpose data=want out=wide (drop=_name_ _label_);
by id;
id service;
var count;
run;
proc print data=wide;
run;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.