我试图根据ID和日期获取默认变量中的更改数。我已经使用SAS一周了,所以如果我需要详细的解释,请原谅我。
我现在有
data test111;
input Date $ Acc $ Default $;
datalines;
jan-10 A N
feb-10 A D
mar-10 A D
apr-10 A D
may-10 A D
jan-10 B N
feb-10 B N
mar-10 B D
apr-10 B D
may-10 B D
jan-10 C N
feb-10 C N
mar-10 C N
apr-10 C D
may-10 C D
jan-10 D N
feb-10 D D
mar-10 D N
apr-10 D D
may-10 D D
jan-10 E D
feb-10 E D
mar-10 E D
apr-10 E N
may-10 E D我想要一个输出(下面的表1),当每个唯一帐户的缺省值从N更改为D时,该输出就会计算在内。但这取决于日期。我只知道如何使用Excel显示所需的输出(手动计数)。表2是如何计算帐户,如果我不清楚。
Table 1
month+1 month+2 month+3 month+4
Jan-10 2 1 1 0
Feb-10 1 1 0
Mar-10 2 0
Apr-10 1
Table 2 (FYR)
month+1 month+2 month+3 month+4
Jan-10 A,D B C -
Feb-10 B C -
Mar-10 C,D -
Apr-10 E 我尝试过创建一个新列,当N变为D时,这样我就可以在tag =1时进行求和。
by first.Acc
if first.Acc then tag = 0;
if default = 'D' then do;
tag = 1;
tag+1;不确定这是否能为我的第一行得到正确的结果。但是如果这样做有效的话,它只能给出我想要的输出的第一行。我有超过100个月,它可以循环或排列它吗?
如果我的SAS数据输入技能失败,我已经包括Excel屏幕截图。Excel截图
发布于 2018-07-04 14:26:47
最普遍的解决方案将涉及到每个组内的完全外部连接。
SQL可以执行联接,并在N为默认值的情况下计算一个月间隔的月份,然后再用D默认值计算最早的一个月。表格可以显示计数的网格。
数据
将月份字符串转换为用于显示目的的格式化yymon的月份日期值的SAS1-st。
data have;
input Date $ Acc $ Default $;
month = input ('01-'||date, date9.);
format month yymon.;
datalines;
jan-10 A N
feb-10 A D
mar-10 A D
apr-10 A D
may-10 A D
jan-10 B N
feb-10 B N
mar-10 B D
apr-10 B D
may-10 B D
jan-10 C N
feb-10 C N
mar-10 C N
apr-10 C D
may-10 C D
jan-10 D N
feb-10 D D
mar-10 D N
apr-10 D D
may-10 D D
jan-10 E D
feb-10 E D
mar-10 E D
apr-10 E N
may-10 E D
run;示例SQL
proc sql;
create view have_v as
select
left.acc
, left.month as from_month
, right.month as to_month
, intck ('MONTH', left.month, right.month) as months_apart
from
have as left
join have as right on left.acc = right.acc
where
left.month < right.month
& left.default = 'N'
& right.default = 'D'
group by left.acc, left.month
having right.month = min(right.month)
order by
left.month, right.month
;
create table grid_bounds as
select
min(from_month) as min_from
, max(from_month) as max_from
, 1 as min_apart
, max(months_apart) as max_apart
from have_v
;SQL解释了
自联接是表对自身的连接。
have as left join have as rightjoin约束将from/to月份组合限制为仅位于同一帐户中的月份组合。
on left.acc = right.accwhere约束进一步将from/to组合限制为将来只有to且具有所需的default转换的组合。
where
left.month < right.month
& left.default = 'N'
& right.default = 'D'group by操作符和SAS Proc SQL having子句自动重新合并特性允许简单的语句来为给定的from选择最早的to。
group by left.acc, left.month
having right.month = min(right.month)对于所选的联接行,可以使用SAS数据函数INTCK计算月份间隔(我认为函数名是短语“句点类型的INTerval计数”的缩写)。
, intck ('MONTH', left.month, right.month) as months_apart如果已知最大的组大小,则特定的解决方案可以使用数组。
输出表
由于没有过渡,有些月可能不存在。同样,也可能不存在一些差距。这些情况在have_v中不会有任何行。为了全面覆盖报表,生成了所有可能的交叉(或组合),以便在Proc TABULATE中使用。
proc sql;
create table grid_bounds as
select
min(from_month) as min_from
, max(from_month) as max_from
, 1 as min_apart
, max(months_apart) as max_apart
from have_v
;
quit;
data grid (label="All crossings to be shown in the output");
set grid_bounds;
do from_month = min_from to max_from;
do months_apart = 1 to max(12,max_apart);
OUTPUT;
end;
end;
keep from_month months_apart;
format from_month yymon.;
run;为每个from_month输出带有间隙频率的网格报告
options missing = '0' nocenter;
title "Account frequency";
title2 "Gap of month default changing from N to D";
proc tabulate data=have_v classdata=grid;
class from_month months_apart;
table
from_month=''
, months_apart * N=''
;
run;
options missing = '.';https://stackoverflow.com/questions/51169518
复制相似问题