You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用SAS Proc Format合并SchoolSize的S与M分组失败求助

SAS Proc Format合并分组失效的解决方案

我尝试用SAS的proc format将数据集按SchoolSize变量分组,把小型(S)和中型(M)学校合并为一组,但编写的三段代码都没实现预期效果,结果仍按原始数据的L、M、S分别分组,无法得到合并后的均值统计。

原始代码

proc format;
    value $SchoolSize
        "L"="Large"
        OTHER="Small and Medium";
run;
title "Means of Data Set COLLEGE5";
proc means data=college5 mean median maxdec=2;
    class  SchoolSize;
    var ClassRank GPA;
    format SchoolSize SchoolSize$.; /* 格式引用错误 */
run;

替代代码1

proc format;
    value $SchoolSize
        "L"="Large"
        "S"="Small and Medium"
        "M"="Small and Medium";
run;
title "Means of Data Set COLLEGE5";
proc means data=college5 mean median maxdec=2;
    class  SchoolSize;
    var ClassRank GPA;
    format SchoolSize SchoolSize$.; /* 格式引用错误 */
run;

替代代码2

proc format;
    value $SchoolSize
        "L"="Large"
        "S", "M"="Small and Medium";
run;
title "Means of Data Set COLLEGE5";
proc means data=college5 mean median maxdec=2;
    class  SchoolSize;
    var ClassRank GPA;
    format SchoolSize SchoolSize$.; /* 格式引用错误 */
run;

问题原因

默认情况下,proc means的class语句基于变量原始值分组,而非格式化后的值。即使定义了格式,SAS只是用格式标签替换显示文本,但分组逻辑仍以原始的L、M、S为依据,因此无法合并分组。此外,代码中格式引用顺序错误:字符格式需以$开头,正确写法应为$SchoolSize.,而非SchoolSize$.;。

正确解决方案

在class语句后添加groupformat选项,指定按格式化后的值分组,同时修正格式引用的语法错误:

proc format;
    value $SchoolSize
        "L"="Large"
        OTHER="Small and Medium";
run;

title "Means of Data Set COLLEGE5";
proc means data=college5 mean median maxdec=2;
    class SchoolSize / groupformat; /* 启用格式化分组 */
    var ClassRank GPA;
    format SchoolSize $SchoolSize.; /* 正确引用字符格式 */
run;

关键说明

  • groupformat:让CLASS变量使用格式化后的结果作为分组依据,S和M会被合并到"Small and Medium"组中统计
  • 字符格式引用:必须以$开头,格式名与定义的$SchoolSize对应,写法为$SchoolSize.

内容的提问来源于stack exchange,提问作者Srividya

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.26 14:41:22