在SAS中使用PROC SQL创建标志变量,对比新旧数据集变量计数变化
实现方法
要完成标记变量计数差异的需求,需先分别统计新旧数据集各分类变量的计数,再合并结果并根据阈值生成标志变量,具体实现如下:
1. 统计旧数据集分类变量计数
先对旧数据集按目标变量(如gender)分组统计人数:
proc sql; create table old_counts as select gender as category, count(person_key) as count_old from oldperson group by gender; quit;
2. 统计新数据集分类变量计数
对新数据集执行相同逻辑的统计:
proc sql; create table new_counts as select gender as category, count(person_key) as count_new from newperson group by gender; quit;
3. 合并计数并生成差异标志
将两个计数表合并,计算差值并根据±100的阈值生成标志(重点突出下降情况):
proc sql; create table diff_flags as select coalesce(o.category, n.category) as category, coalesce(o.count_old, 0) as count_old, coalesce(n.count_new, 0) as count_new, case when (n.count_new - o.count_old) <= -100 then '显著下降' when (n.count_new - o.count_old) >= 100 then '显著增长' when (n.count_new - o.count_old) < 0 then '下降' else '无显著变化/小幅增长' end as change_flag from old_counts o full join new_counts n on o.category = n.category; quit;
关键说明
- 用
coalesce处理某分类仅存在于单一数据集的情况,默认缺失计数为0 - 标志逻辑优先标记显著下降,再依次处理显著增长、普通下降,其余情况归为无显著变化
- 若要处理
sex、race_cd等其他变量,只需将代码中的gender替换为对应变量名即可
内容的提问来源于stack exchange,提问作者Maria
相关产品推荐
相关产品推荐

