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

如何让SAS报表保留未达阈值的location、year、month缺失记录?

解决方案

要保留所有location、year、month组合(即使无达标记录),核心思路是先构建所有可能的维度组合,再与过滤后的达标数据做左连接,确保无数据的组合也能被保留。

步骤1:生成全量维度组合

先从原表have中提取所有不重复的location、year、month,生成包含所有可能组合的数据集all_combinations:

proc sql;
create table all_combinations as
select distinct a.location, b.year, c.month
from (select distinct location from have) a,
     (select distinct year from have) b,
     (select distinct month from have) c;
quit;

步骤2:左连接并计算达标天数

将全量组合表与原表左连接,把阈值过滤条件放在ON子句中(而非WHERE),确保不匹配的组合仍被保留,同时计算达标天数(无达标记录时显示缺失值):

proc sql;
create table newtable as
select ac.location, 
       ac.year, 
       ac.month,
       case 
           when count(distinct h.day) > 0 then count(distinct h.day)
           else .  /* 无达标记录时显示缺失值 */
       end as daycount
from all_combinations ac
left join have h
on ac.location = h.location 
and ac.year = h.year 
and ac.month = h.month
and h.threshold > 90  /* 过滤条件放在JOIN中,不影响全量组合保留 */
group by ac.location, ac.year, ac.month;
quit;

步骤3:保持原Proc Report代码不变

原proc report代码无需修改,此时newtable已包含所有维度组合,报表会自动显示所有location、month,以及跨列的year,无达标记录的位置会显示缺失值(.):

proc report data=newtable;
columns location month year, daycount;
define location/group;
define month/group;
define year/across;
define daycount/analysis sum;
run; 

补充说明

  • 如果希望无达标记录时显示0而非缺失值,只需将else .改为else 0即可。
  • 若原表中存在单独的维度表(比如专门的locations表),可以直接用维度表生成全量组合,无需从have中提取,结果会更准确。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.28 13:27:39