我有一个如下所示的数据集:
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;
谢谢。
最佳答案
尝试:
proc sql;
create table test as
select state
,count(distinct(cats(county,city)))
,count(distinct(city))
,sum(quantity)
from have
group by 1;
串联是您的最佳选择..
关于sql - SAS 类别划分频率,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25124202/