20、数据报告处理与调试技巧

数据报告处理与调试技巧

1. 合并多个 PROC REPORT 步骤输出

在数据处理中,有时需要将多个 PROC REPORT 步骤的输出合并到一个节点下。例如,之前的示例按客户所在国家(CUSTOMER_COUNTRY)生成输出,现在可能有另一个 PROC REPORT 步骤也按此生成输出,而最终报告希望将一个国家的所有表格分组在一起。

以下是示例代码:

proc sort data=orders;
by customer_country;
run;
ods document name=temp(write);
proc report data=orders contents=”;
by customer_country;
format customer_country $cntry.;
column customer_country product_line quantity total_retail_price;
define customer_country /group;
define product_line /group;
break before customer_country /page contents=”;
run;

proc report data=orders contents=”;
by customer_country;
format customer_country $cntry.;
column customer_country customer_gender,n customer_age_group,n
customer_type,n;
define customer_country /group;
define customer_gender / across;
define customer_age_group / across;
define customer_type / across;
break before customer_country /page contents=”;
run;
ods document close;
proc document name=temp;
make \multrep;
setlabel \multrep “Sale Information by Country”; ❶
make \multrep\cntry1; ❷
setlabel \multrep\cntry1 “Australia”; ❸
setlabel \Report#1\ByGroup1#1\Report#1 “Customer Sales”; ❹
copy \Report#1\ByGroup1#1\Report#1 to \multrep\cntry1#1; ❺
setlabel \Report#2\ByGroup1#1\Report#1 “Customer Demography”; ❻
copy \Report#2\ByGroup1#1\Report#1 to \multrep\cntry1#1; ❼
❽
make \multrep\cntry2;
setlabel \multrep\cntry2 “Canada”;
setlabel \Report#1\ByGroup2#1\Report#1 “Customer Sales”;
copy \Report#1\ByGroup2#1\Report#1 to \multrep\cntry2#1;
setlabel \Report#2\ByGroup2#1\Report#1 “Customer Demography”;
copy \Report#2\ByGroup2#1\Report#1 to \multrep\cntry2#1;
make \multrep\cntry3;
setlabel \multrep\cntry3 “Germany”;
setlabel \Report#1\ByGroup3#1\Report#1 “Customer Sales”;
copy \Report#1\ByGroup3#1\Report#1 to \multrep\cntry3#1;
setlabel \Report#2\ByGroup3#1\Report#1 “Customer Demography”;
copy \Report#2\ByGroup3#1\Report#1 to \multrep\cntry3#1;
make \multrep\cntry4;
setlabel \multrep\cntry4 “Israel”;
setlabel \Report#1\ByGroup4#1\Report#1 “Customer Sales”;
copy \Report#1\ByGroup4#1\Report#1 to \multrep\cntry4#1;
setlabel \Report#2\ByGroup4#1\Report#1 “Customer Demography”;
copy \Report#2\ByGroup4#1\Report#1 to \multrep\cntry4#1;
make \multrep\cntry5;
setlabel \multrep\cntry5 “Turkey”;
setlabel \Report#1\ByGroup5#1\Report#1 “Customer Sales”;
copy \Report#1\ByGroup5#1\Report#1 to \multrep\cntry5#1;
setlabel \Report#2\ByGroup5#1\Report#1 “Customer Demography”;
copy \Report#2\ByGroup5#1\Report#1 to \multrep\cntry5#1;
make \multrep\cntry6;
setlabel \multrep\cntry6 “United States”;
setlabel \Report#1\ByGroup6#1\Report#1 “Customer Sales”;
copy \Report#1\ByGroup6#1\Report#1 to \multrep\cntry6#1;
setlabel \Report#2\ByGroup6#1\Report#1 “Customer Demography”;
copy \Report#2\ByGroup6#1\Report#1 to \multrep\cntry6#1;
make \multrep\cntry7;
setlabel \multrep\cntry7 “South Africa”;
setlabel \Report#1\ByGroup7#1\Report#1 “Customer Sales”;
copy \Report#1\ByGroup7#1\Report#1 to \multrep\cntry7#1;
setlabel \Report#2\ByGroup7#1\Report#1 “Customer Demography”;
copy \Report#2\ByGroup7#1\Report#1 to \multrep\cntry7#1;
run;
ods pdf file=“CountrySales2.pdf” ;
replay \multrep; ❾
run;
ods pdf close;
quit;

操作步骤如下:
1. 排序 :使用 proc sort customer_country 对数据排序。
2. 创建文档存储 :使用 ods document 打开一个可写的文档存储。
3. 执行第一个 PROC REPORT 步骤 :按 customer_country 分组,输出相关列。
4. 执行第二个 PROC REPORT 步骤 :同样按 customer_country 分组,输出不同的列。
5. 关闭文档存储 :使用 ods document close 关闭文档存储。
6. 重新打开文档存储并操作
- 创建新目录 \multrep 并设置标签为 “Sale Information by Country”。
- 为每个国家创建子目录并设置标签。
- 为每个 PROC REPORT 步骤的输出设置节点标签。
- 将每个 PROC REPORT 步骤的输出复制到相应国家的子目录。
7. 生成 PDF 报告 :使用 ods pdf 打开 PDF 输出,使用 replay 语句重放新目录生成报告,最后关闭 PDF 输出。

这个过程可以通过下面的 mermaid 流程图展示:

graph LR
    A[数据排序] --> B[打开文档存储]
    B --> C[执行第一个 PROC REPORT]
    C --> D[执行第二个 PROC REPORT]
    D --> E[关闭文档存储]
    E --> F[重新打开文档存储并操作]
    F --> G[生成 PDF 报告]
2. PROC REPORT 调试技巧

在编写 PROC REPORT 代码时,即使对数据很熟悉且编写时很小心,也可能会出现问题。下面介绍一些常见的错误、警告和提示信息及解决方法。

2.1 DEFINE 语句相关问题
错误/警告信息 原因 解决方法
ERROR: XXXX conflicts with earlier use of XXXX. 同一报告项不能有两种不同用法,如变量不能同时作为 GROUP 和 ACROSS 使用,常因 COLUMN 语句创建别名但定义了不同用法导致。 在输入数据集上创建重复变量,新变量包含与原变量相同信息,但可在 PROC REPORT 步骤中使用任何用法。
ERROR: The width of XXXX is not between 1 and NNN. Adjust the column width or line size. WIDTH= 选项或报告项长度超过 LINESIZE 系统选项,仅影响 ODS Listing 目标。 若不需要 ODS Listing 目标则关闭它;否则在 DEFINE 语句中为产生错误的报告项指定 WIDTH= 并设置小于 LINESIZE 的值;若 LINESIZE 未达到最大值则增大它。
ERROR: There is more than one ANALYSIS usage associated with the column defined by the following elements. COLUMN 语句中的逗号表示堆叠列,但没有变量定义为 ACROSS 用法。 从 COLUMN 语句中移除逗号或将逗号旁边的一个报告项的用法改为 ACROSS。
ERROR: There is no statistic associated with XXXX DISPLAY 在 ACROSS 下时需要关联统计量。 将 DISPLAY 用法改为 GROUP 或在 ACROSS 分组后插入 N 统计量。
ERROR: A DISPLAY or GROUP variable above or below an ACROSS variable requires that there be an ORDER, GROUP, or DISPLAY variable in the report that is not above or below an ACROSS variable. GROUP 或 DISPLAY 变量在 ACROSS 下时,报告中需要有不在 ACROSS 下的 GROUP、ORDER 或 DISPLAY 变量。 若没有合适变量,在 DATA 步骤中创建分组变量并放在 COLUMN 语句的 ACROSS 变量之前,可定义为 NOPRINT 使其不显示在表中。
ERROR: An ORDER variable appears above or below other report items. ACROSS 变量不能与 ORDER 变量共享一列。 将 ORDER 用法改为 GROUP,且需要有不在 ACROSS 下的另一个 GROUP 变量。
ERROR: XXXX is not an ORDER, GROUP, or ACROSS variable and is marked DESCENDING. DESCENDING 选项仅对 ORDER、GROUP 或 ACROSS 变量有效。 移除 DESCENDING 选项以消除错误。
ERROR: You cannot have a GROUP variable stacked with an ACROSS variable when there is a DISPLAY variable by itself in a separate column. 使用 ACROSS 变量时有一些限制,不能在有不在 ACROSS 下的 DISPLAY 变量时将 GROUP 变量与 ACROSS 变量堆叠。 将 DISPLAY 变量改为 GROUP 以避免此错误。
ERROR 180 - 322: Statement is not valid or it is used out of proper order. 可能是因为在 STYLE( )= 选项中放置了无效的样式属性,或 CALL DEFINE 语句在计算块中使用不当。 检查 STYLE= 语句或 CALL DEFINE 语句中的样式规范,确保语句包含有效的样式属性。
ERROR 79 - 322: Expecting a (.
ERROR 200 -

数据报告处理与调试技巧

3. 其他常见问题及解决方法
3.1 BREAK 语句相关问题
错误/警告信息 原因 解决方法
ERROR: You can only BREAK on GROUPing and ORDERing variables. BREAK 语句上列出的变量未定义为 GROUP 或 ORDER。 移除 BREAK 语句或更改该变量在 DEFINE 语句中的用法为 GROUP 或 ORDER。
ERROR: The BREAK variable XXXX is not one of the GROUP or ORDER variables. COLUMN 语句上创建了别名且该别名列在 BREAK 语句上,PROC REPORT 不能在同一变量或位置有多个摘要行。 在输入数据集上创建重复变量,新变量包含与原变量相同信息,但可在 PROC REPORT 步骤中随意使用。
WARNING: The CONTENTS option will have no effect for variable XXXX because the PAGE option is not specified. BREAK 语句上的 CONTENTS= 选项必须与 PAGE 选项配对使用。 要么添加 PAGE 选项,要么移除 CONTENTS= 选项以消除此警告。

这个过程可以通过下面的 mermaid 流程图展示:

graph LR
    A[编写 BREAK 语句] --> B{变量是否为 GROUP 或 ORDER?}
    B -- 否 --> C[出现 ERROR 1]
    C --> D[移除或更改用法]
    B -- 是 --> E{是否为别名且列在 BREAK 上?}
    E -- 是 --> F[出现 ERROR 2]
    F --> G[创建重复变量]
    E -- 否 --> H{是否有 PAGE 选项?}
    H -- 否 --> I[出现 WARNING]
    I --> J[添加 PAGE 或移除 CONTENTS]
    H -- 是 --> K[正常执行]
3.2 Compute 块语句相关问题
错误信息 原因 解决方法
ERROR: Missing an ENDCOMP statement. COMPUTE 语句需要 ENDCOMP 语句,若缺失则会出现此错误。 添加 ENDCOMP 语句。
ERROR: There are multiple COMPUTE statements for XXXX. 每个报告项只允许有一个计算块。 将两个块的语句合并为一个。
ERROR: There are multiple COMPUTE statements for BREAK AFTER XXXX. 每个位置目标对只允许有一个计算块。 将两个块的语句合并为一个。
ERROR 22 - 322: Syntax error, expecting one of the following: a name, AFTER, BEFORE. COMPUTE 语句只包含计算关键字和分号,缺少报告项或位置。 在 COMPUTE 语句中添加报告项或位置。
ERROR: The variable type of XXXX.SUM is invalid in this context.
ERROR: Illegal reference to the array XXXX.SUM.
可能是因为 ANALYSIS 变量在 ACROSS 下通过复合名称而非列号引用,或引用了别名的复合名称,或赋值语句右侧的 ANALYSIS 变量名称拼写错误。 确认 XXXX 变量在 DEFINE 语句中的用法,更改引用方式为适合该用法的方式,确保变量名称拼写正确。
ERROR: XXXX must use a character format 在 LINE 语句中使用变量时未指定格式,LINE 语句中每个项(变量)都必须指定格式。 在 LINE 语句中的变量后面放置格式。
ERROR 22 - 322: Syntax error, expecting one of the following: a name, a format name 常见错误,可能是 CALL DEFINE 语句有问题,如属性值对属性名称无效,或数据值超出格式范围,或语句换行时属性间缺少空格。 检查所有属性名称是否正确;若 CALL DEFINE 语句的 STYLE 参数中指定了格式,检查数据值是否在格式范围内;确保每个属性间有空格。
ERROR: PAGESIZE is too small for BREAK. PROC REPORT 在 ODS Listing 目标下,没有足够空间在一页上打印摘要行和 LINE 行的所有信息,且 LINE 语句必须保持在一起不能跨页。 关闭目标或增大 PAGESIZE 值。
ERROR: Invalid column specification in CALL DEFINE. CALL DEFINE 语句中引用的变量不在 COLUMN 语句中,或作为 CALL DEFINE 语句第一个参数的列号在表中不存在。 移除 CALL DEFINE 语句或在 COLUMN 语句中添加报告项。
ERROR: LINE statements must appear in a COMPUTE block that is associated with a location in the report. LINE 语句在计算报告项块中,而 LINE 语句只能在与位置关联的计算块中使用。 从报告项块中移除该语句或在 COMPUTE 语句中添加位置。
NOTE: Variable XXXX is uninitialized. 变量 XXXX 在赋值语句右侧未初始化。 对变量进行初始化操作。
4. 临时变量值查看及通用调试技巧

在调试过程中,还可以查看临时变量的值,以确保它们包含预期的值。有两种常见的输出临时变量值的方法:
1. 通过 LINE 语句输出 :在计算块中使用 LINE 语句输出临时变量的值。
2. 通过计算变量输出 :创建计算变量并将临时变量的值赋给它,然后在报告中显示该计算变量。

此外,还有一些通用的调试技巧:
- 仔细检查代码语法,确保没有拼写错误或遗漏语句。
- 逐步执行代码,查看每一步的输出结果,定位问题所在。
- 参考官方文档和社区论坛,查找类似问题的解决方法。

总之,掌握这些 PROC REPORT 的调试技巧,可以帮助我们更高效地处理数据报告,减少错误和问题的出现,提高工作效率和报告质量。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值