【问题标题】:SAS frequency of category divisions类别划分的 SAS 频率
【发布时间】: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;

谢谢。

【问题讨论】:

    标签: sql sas


    【解决方案1】:

    试试:

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

    串联您在这里的最佳选择..

    【讨论】:

    • 谢谢。这样可行。猜猜我是在正确的轨道上,但不需要创建一个变量,只需要一个连接来计数。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-09-04
    相关资源
    最近更新 更多