频率未加起来(SAS PROC SQL)

时间:2017-01-13 17:24:08

标签: sql sas proc enterprise-guide

我正在尝试仅查找唯一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';

在此处查看数据集的片段: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

SUM = 33746

33746!= 20599

寻找有关出错的方法或是否有更好的方法来获得我想要的结果(MOTPCopmponentDescription的唯一MOTPID计数。非常感谢!

2 个答案:

答案 0 :(得分:0)

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

标题"您认为问题是没问题";

title2"计算所有品牌";

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

title2"计算生产具有一定数量汽缸的汽车的品牌&#34 ;;

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"你所谓的解决方案正在产生不可预测的结果&#34 ;;

title2"如果输入以单向排序&#34 ;;

,则会产生此结果
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"如果输入以另一种方式排序&#34 ;;

,它会产生此结果
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辆汽车;

答案 1 :(得分:-1)

这是我制定的解决方案,它得到了我想要的确切结果:

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%