按周计算的 SAS 宏
SAS Macro by week
我想将下面的宏转换成一个循环,这样我就可以输入开始和结束日期,以便为该日期范围创建周表。目前,我必须通过分别输入每周的开始和结束日期来多次 运行 宏。
%macro tb(start_, end_);
PROC SQL;
CREATE TABLE tbl_&start_. AS
select a.id,
&start_. as wk,
case when a.id=b.id then 1 else 0 end as flg
from tb1 a left join (select id
from tb1
where cdate <= &start_.
) b
on a.id=b.id
where orderdate <= &end_.
;
QUIT;
%mend ;
%tb(03JAN2021, 09JAN2021) ;
%tb(10JAN2021, 16JAN2021) ;
%tb(17JAN2021, 23JAN2021) ;
INTCK and INTNX 函数可能有助于此任务。以下生成一个数据集,其中包含指定时间间隔内所有周的开始和结束日期。
data weeks;
/* Specify start and end. */
start_date = "01FEB2022"d;
end_date = "28FEB2022"d;
/* Calculate number of weeks. */
num_weeks = intck("week", start_date, end_date);
/* Iterate over weeks. */
do i = 0 to num_weeks - 1;
/* Get the sunday in the week i. */
start_week = intnx("week", start_date, i);
/* Add six days = end of week. */
end_week = intnx("days", start_week, 6);
/* If you need, convert into character. */
start_ = put(start_week, date.);
end_ = put(end_week, date.);
output;
end;
run;
如果您希望周从星期一开始,您应该使用“week.2”而不是“week”。你也可以这样做bi-weekly 报告...只需查看 Date and Time intervals 的文档。
现在您可以 运行 通过调用执行您的宏。
data _null_;
set weeks;
call execute('%nrstr(%do_for_each_week('||start_||', '||end_||'))');
run;
我想将下面的宏转换成一个循环,这样我就可以输入开始和结束日期,以便为该日期范围创建周表。目前,我必须通过分别输入每周的开始和结束日期来多次 运行 宏。
%macro tb(start_, end_);
PROC SQL;
CREATE TABLE tbl_&start_. AS
select a.id,
&start_. as wk,
case when a.id=b.id then 1 else 0 end as flg
from tb1 a left join (select id
from tb1
where cdate <= &start_.
) b
on a.id=b.id
where orderdate <= &end_.
;
QUIT;
%mend ;
%tb(03JAN2021, 09JAN2021) ;
%tb(10JAN2021, 16JAN2021) ;
%tb(17JAN2021, 23JAN2021) ;
INTCK and INTNX 函数可能有助于此任务。以下生成一个数据集,其中包含指定时间间隔内所有周的开始和结束日期。
data weeks;
/* Specify start and end. */
start_date = "01FEB2022"d;
end_date = "28FEB2022"d;
/* Calculate number of weeks. */
num_weeks = intck("week", start_date, end_date);
/* Iterate over weeks. */
do i = 0 to num_weeks - 1;
/* Get the sunday in the week i. */
start_week = intnx("week", start_date, i);
/* Add six days = end of week. */
end_week = intnx("days", start_week, 6);
/* If you need, convert into character. */
start_ = put(start_week, date.);
end_ = put(end_week, date.);
output;
end;
run;
如果您希望周从星期一开始,您应该使用“week.2”而不是“week”。你也可以这样做bi-weekly 报告...只需查看 Date and Time intervals 的文档。
现在您可以 运行 通过调用执行您的宏。
data _null_;
set weeks;
call execute('%nrstr(%do_for_each_week('||start_||', '||end_||'))');
run;