【发布时间】:2014-08-04 17:22:26
【问题描述】:
我有一个如下所示的数据集:
data have;
input county $ city $ state $ quantity;
cards;
A Springfield AZ 1000
A Townsville AZ 1000
A Selma AZ 1000
A Dunno AZ 1000
B City NC 2000
B Town NC 1000
B Village NC 2000
C Springfield AZ 2000
C Fargo AZ 1000
;
run;
我正在尝试计算每个州内有多少不同的县和市,并总结每个州的数量。因此,最终目标是:
data want;
input state $ freq_counties freq_cities sum_quantity;
cards;
AZ 2 6 7000
NC 1 3 5000
;
run;
这就是我所拥有的,这几乎可以工作。 Springfield, AZ 出现了两次,这个 SQL 只计算一次(当然,这正是它应该做的)。但是,由于它们是不同的县,我希望将它们分开计算。我考虑过连接县和市来制作第三个变量,但如果有更简单的方法,我宁愿不这样做。想法?
proc sql;
create table test as
select state
,count(distinct(county))
,count(distinct(city))
,sum(quantity)
from have
group by 1;
quit;
谢谢。
【问题讨论】: