sas 中的日期格式在条件下与 proc sql 连接
Date format in sas concatenate with a proc sql under conditions
我有一个 macro_variable :%let date=201909
和 table :
ID Sending-date item
1 15-jul-2019 A
2 23-sep-2019 B
3 12-sep-2019 A
4 1-jan-2019 B
5 5-feb-2019 B
我想做的是使用 proc sql 验证是否有在我的日期(9 月 9 日)和前两个月(8 月和 7 月)指定的月份发送的项目并且没有添加新变量。
result_table 预期是这样的:
Month Year Number of items
9 2019 2
8 2019 0
7 2019 1
最大的问题是如何像我的macro_variable日期那样转换table中日期的格式。
这是一种方法。
我使用 cutoff_date 而不是日期,因为它有助于更轻松地区分日期。
使用 INTNX() 进行日期计算。在这种情况下,我将截止日期设置为 cutoff_month 的末尾和两个月前的开始。您可能需要更清楚地定义它以满足您的需求,但这行得通。
%let cutoff_date=201909;
proc sql;
create table want as
select month(sending_date) as Month, count(*) as num
from have
where sending_date between intnx('month', input("&cutoff_date.", yymmn6.), 0, 'e') and intnx('month', input("&cutoff_date.", yymmn6.), -2, 'b')
group by calculated Month;
quit;
我有一个 macro_variable :%let date=201909 和 table :
ID Sending-date item
1 15-jul-2019 A
2 23-sep-2019 B
3 12-sep-2019 A
4 1-jan-2019 B
5 5-feb-2019 B
我想做的是使用 proc sql 验证是否有在我的日期(9 月 9 日)和前两个月(8 月和 7 月)指定的月份发送的项目并且没有添加新变量。
result_table 预期是这样的:
Month Year Number of items
9 2019 2
8 2019 0
7 2019 1
最大的问题是如何像我的macro_variable日期那样转换table中日期的格式。
这是一种方法。
我使用 cutoff_date 而不是日期,因为它有助于更轻松地区分日期。 使用 INTNX() 进行日期计算。在这种情况下,我将截止日期设置为 cutoff_month 的末尾和两个月前的开始。您可能需要更清楚地定义它以满足您的需求,但这行得通。
%let cutoff_date=201909;
proc sql;
create table want as
select month(sending_date) as Month, count(*) as num
from have
where sending_date between intnx('month', input("&cutoff_date.", yymmn6.), 0, 'e') and intnx('month', input("&cutoff_date.", yymmn6.), -2, 'b')
group by calculated Month;
quit;