【问题标题】:Frequencies not adding up (SAS PROC SQL)频率未累加 (SAS PROC SQL)
【发布时间】:2017-01-13 17:24:08
【问题描述】:

我正在尝试仅查找唯一 ID 号的频率。我尝试了 PROC FREQ,但无法弄清楚如何执行 SELECT DISTINCT 的 SAS 等效项。我运行了以下代码并得到了不相加的数字。

代码: PROC SQL; SELECT COUNT (DISTINCT MOTPID) FROM WORK.'0__1_MOTP_COMMENTS_0000'n;

结果: 20599

代码:

PROC SQL;
SELECT COUNT (DISTINCT MOTPID) FROM WORK.'0__1_MOTP_COMMENTS_0000'n
WHERE MOTPComponentDescription = '1a (obs): Demonstrating knowledge of content and pedagogy';

PROC SQL;
SELECT COUNT (DISTINCT MOTPID) FROM WORK.'0__1_MOTP_COMMENTS_0000'n
WHERE MOTPComponentDescription = '1a (p&p): Demonstrating knowledge of content and pedagogy';

PROC SQL;
SELECT COUNT (DISTINCT MOTPID) FROM WORK.'0__1_MOTP_COMMENTS_0000'n
WHERE MOTPComponentDescription = '1e (obs): Designing coherent instruction';

PROC SQL;
SELECT COUNT (DISTINCT MOTPID) FROM WORK.'0__1_MOTP_COMMENTS_0000'n
WHERE MOTPComponentDescription = '1e (p&p): Designing coherent instruction';

PROC SQL;
SELECT COUNT (DISTINCT MOTPID) FROM WORK.'0__1_MOTP_COMMENTS_0000'n
WHERE MOTPComponentDescription = '2a: Creating an environment of respect and rapport';

PROC SQL;
SELECT COUNT (DISTINCT MOTPID) FROM WORK.'0__1_MOTP_COMMENTS_0000'n
WHERE MOTPComponentDescription = '2d: Managing student behavior';

PROC SQL;
SELECT COUNT (DISTINCT MOTPID) FROM WORK.'0__1_MOTP_COMMENTS_0000'n
WHERE MOTPComponentDescription = '3b: Using questioning and discussion techniques';

PROC SQL;
SELECT COUNT (DISTINCT MOTPID) FROM WORK.'0__1_MOTP_COMMENTS_0000'n
WHERE MOTPComponentDescription = '3c: Engaging students in learning';

PROC SQL;
SELECT COUNT (DISTINCT MOTPID) FROM WORK.'0__1_MOTP_COMMENTS_0000'n
WHERE MOTPComponentDescription = '3d: Using assessment in instruction';

PROC SQL;
SELECT COUNT (DISTINCT MOTPID) FROM WORK.'0__1_MOTP_COMMENTS_0000'n
WHERE MOTPComponentDescription = '4e (obs): Growing and developing     professionally';

PROC SQL;
SELECT COUNT (DISTINCT MOTPID) FROM WORK.'0__1_MOTP_COMMENTS_0000'n
WHERE MOTPComponentDescription = '4e (p&p): Growing and developing professionally';

在此处查看数据集的 sn-p:https://docs.google.com/spreadsheets/d/1WDcsezb4xiT67J9t3Nlyi_QEofs0dhyZ23yC32ccbqg/edit?usp=sharing

结果: 1a(obs):展示内容和教学法知识:700

1a (p&p):展示内容和教学法知识:606

1e (obs):设计连贯指令:15622

1e (p&p):设计连贯指令:1135

2a:创​​造尊重和融洽的环境:2466

2d:管理学生行为:1005

3b:使用提问和讨论技巧:808

3c:让学生参与学习:2516

3d:在教学中使用评估:3058

4e (obs):专业成长和发展:5245

4e (p&p):专业成长和发展:588

总和 = 33746

33746 != 20599

寻找关于哪里出了问题的任何想法,或者是否有更好的方法来获得我想要的结果(MOTPCopmponentDescription 的唯一 MOTPID 的计数。提前非常感谢!

【问题讨论】:

  • 你期待什么?在这种情况下,“加起来”对您意味着什么?
  • 帖子中声明的结果:MOTPComponentDescription 的唯一 MOTPID 计数。对此进行扩展:有一个标有 MOTPID 的列带有 ID 号,还有一个标有 MOTPComponentDescription 的列带有另一个标识符。每个 MOTPID 最多可以有 8 行(每个 MOTPComponentDescription 1 个)。我想获得每个 MOTPComponentDescription 的唯一 MOTPID 数量。示例:“4e (p&p):Growing and developmently professional”可能有 950 行,但只有 508 个唯一的 MOTPID。我希望结果是 508,而不是 950。
  • "加起来" = 使所有单个 MOTPComponentDescription 计数的总和与所有唯一 MOTPID 的计数相加。目前所有组件计数的总和为 22746,数据集的所有唯一 MOTPID 的计数为 20599。这些(显然)不相等,我无法弄清楚问题出在哪里 - 为什么这是我的结果我正在接收。
  • SQL 可以写成聚合:PROC SQL; SELECT MOTPComponentDescription, COUNT (DISTINCT MOTPID) FROM WORK.'0__1_MOTP_COMMENTS_0000'n GROUP BY MOTPComponentDescription

标签: sql sas proc enterprise-guide


【解决方案1】:

要在 StackOverflow 上讨论 SAS 问题,SASHELP 库上的示例数据非常方便。让我们使用 CARS 数据集。;

title "你看到的问题没问题";

title2 "计算所有品牌";

proc sql;
    select count (distinct Make) as distinct_makes from sashelp.cars;
quit;
  • 给 38;

title2“计算生产具有一定数量气缸的汽车的品牌”;

proc sql;
    select 'n.a.' as Cylinders, count (distinct Make) as distinct_makes from sashelp.cars where Cylinders = . union
    select ' 3  ' as Cylinders, count (distinct Make) as distinct_makes from sashelp.cars where Cylinders = 3 union
    select ' 4  ' as Cylinders, count (distinct Make) as distinct_makes from sashelp.cars where Cylinders = 4 union
    select ' 5  ' as Cylinders, count (distinct Make) as distinct_makes from sashelp.cars where Cylinders = 5 union
    select ' 6  ' as Cylinders, count (distinct Make) as distinct_makes from sashelp.cars where Cylinders = 6 union
    select ' 8  ' as Cylinders, count (distinct Make) as distinct_makes from sashelp.cars where Cylinders = 8 union
    select '10  ' as Cylinders, count (distinct Make) as distinct_makes from sashelp.cars where Cylinders = 10 union
    select '12  ' as Cylinders, count (distinct Make) as distinct_makes from sashelp.cars where Cylinders = 12;
quit;
  • 给出 1 个生产 3 个气缸,26 个生产 4 个气缸等等,“加起来”超过 80 个

title2 "您可以手动验证这些列表中的结果";

proc sql;
    select Cylinders, Make, Model from sashelp.cars order by Cylinders, Make;
    select Make, Cylinders, Model from sashelp.cars order by Make, Cylinders;
quit;

title "你所谓的解决方案会产生不可预知的结果";

title2 "如果输入以一种方式排序,它会产生这个结果";

proc sort data=sashelp.cars out=cars_short2long;
    by length;
run;
proc sort data=cars_short2long nodupkey out=cars_short2long_clean dupout=dups;
    by Make;
run;
proc freq data=cars_short2long_clean;
    table Cylinders;
run;
  • 表示没有人会制造 10 缸汽车

title2 "如果输入以另一种方式排序,它会产生这个结果";

proc sort data=sashelp.cars out=cars_long2short;
    by descending length;
run;
proc sort data=cars_long2short nodupkey out=cars_long2short_clean dupout=dups;
    by Make;
run;
proc freq data=cars_long2short_clean;
    table Cylinders;
run;
  • 表示没有人会制造 3 缸汽车

【讨论】:

  • 感谢您的示例。但是,当我重新运行按其他列排序的查询时,输出与我的答案中的结果相同。
【解决方案2】:

这是我制定的解决方案,得到了我正在寻找的确切结果:

data comment_analysis;
set WORK.'0__1_MOTP_COMMENTS_0001'n;
run;

proc sort data=comment_analysis nodupkey out=comment_analysis_clean dupout=dups;
by motpid;
run;

proc freq data=comment_analysis_clean;
table MOTPComponentDescription;
run;

这是我正在寻找的输出: MOTPComponentDescription 频率百分比

1a (obs):展示内容和教学法知识 520 2.52%

1a (p&p):展示内容和教学法知识 400 1.94%

1e (obs):设计连贯指令 11423 55.45%

1e (p&p):设计连贯的指令 526 2.55%

2a:创​​造尊重和融洽的环境 1629 7.91%

2d:管理学生行为 556 2.70%

3b:使用提问和讨论技巧 563 2.73%

3c:让学生参与学习 1593 7.73%

3d:在指令 1818 中使用评估 8.83%

4e (obs):专业成长和发展 1235 6%

4e (p&p):专业成长和发展 336 1.64%

【讨论】:

  • 这里的结论是什么?这不返回原始的sql:SELECT COUNT (DISTINCT MOTPID) FROM WORK.'0__1_MOTP_COMMENTS_0000'n;吗?因此有 20,599 个?
  • 不,这将返回 MOTPComponentDescription 的计数。添加输出以回答。
  • dupkey 在整个数据集中保留第一个不同的 MOTPID,删除稍后显示的行,无论描述字段如何。因此,如果 MOTPID 出现不止一次,描述计数就会被低估。请参阅此aritcle 以及正在讨论您的确切策略的位置。
  • 对不起,你认为你喜欢你的问题,但我相信你没有。正如 Parfait 解释的那样,你得到了错误的结果。我什至相信您的结果取决于输入数据的排序顺序。
  • @DirkHorsten 你能告诉我你的意思吗?我阅读了我的一些结果(意思是数百个 ID 号),到目前为止,一切对我来说都是正确的。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-02-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多