SAS频率类别划分

时间:2014-08-04 17:22:27

标签: sql sas

我有一个如下所示的数据集:

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;

这是我拥有的,这个ALMOST的工作原理。有两次出现在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;

感谢。

1 个答案:

答案 0 :(得分:2)

尝试:

proc sql;
create table test as
    select state
    ,count(distinct(cats(county,city)))
    ,count(distinct(city))
    ,sum(quantity)
    from have
    group by 1;

连接 是你最好的选择..