我终其一生都无法弄清楚如何让这些格式正确用于可用的日期宏。我有两条信息想要用来创建日期宏变量。第一条是年份,第二条是标签,用于让我知道我是否为给定年份或前一年创建变量。例如,如果我将年份设置为 2025 年并将期间设置为 PREV,我想为 2024 年创建日期变量。我想要的日期是年初、年底、年初前 6 个月和年底后 6 个月。我希望它们都采用 01JAN2024 的形式。
我一直在浏览论坛和 SAS 帮助网站,尝试了上千种组合。我找不到任何可行的方法,我甚至不知道如何正确打印结果进行检查。以下是我最近尝试过的方法:
%let year = 2025;
%let period = PREV;
%let start_date = %sysfunc(mdy(1, 1, &meas_yr));
%macro set_dates;
%if &period = PREV
%then %do;
%let p_start = %sysfunc(intnx(year, &start_date, -1, beg));
%let p_end = %sysfunc(intnx(year, &start_date, -1, end));
%let period_start = %sysfunc(putn(&p_start, date9.));
%let period_end = %sysfunc(putn(&p_end, date9.));
%end;
%else %do;
%let p_start = %sysfunc(intnx(year, &start_date, 0, beg));
%let p_end = %sysfunc(intnx(year, &start_date, 0, end));
%let period_start = %sysfunc(putn(&p_start, date9.));
%let period_end = %sysfunc(putn(&p_end, date9.));
%end;
%mend set_dates;
%set_dates;
%let period_lkbk = %sysfunc(intnx(month, &p_start., -6, beg), date9.);
%put &period_lkbk;
%let period_lkfw = %sysfunc(intnx(month, &p_end., 6, end), date9.);
%put &period_lkfw;
以下是我用来测试正在设置的内容的代码:
%put Date_Test_1 : %sysfunc(putn(&p_start, date9.));
%put Date_Test_2 : &period_start;
%put Date_Test_3 : %sysfunc(putn(&p_end, date9.));
%put Date_Test_4 : &period_end;
%put Date_Test_5 : &period_lkbk;
我得到的结果是:
SYMBOLGEN: Macro variable P_START resolves to 23376
27
28 %put Date_Test_1 : %sysfunc(putn(&p_start, date9.));
Date_Test_1 : 01JAN2024
29 %put Date_Test_2 : &period_start;
SYMBOLGEN: Macro variable PERIOD_START resolves to 01JAN2024
Date_Test_2 : 01JAN2024
30 %put Date_Test_3 : %sysfunc(putn(&p_end, date9.));
WARNING: Apparent symbolic reference P_END not resolved.
WARNING: Apparent symbolic reference P_END not resolved.
ERROR: Argument 1 to function PUTN referenced by the %SYSFUNC or %QSYSFUNC macro function is not a number.
ERROR: Invalid arguments detected in %SYSCALL, %SYSFUNC, or %QSYSFUNC argument list. Execution of %SYSCALL statement or %SYSFUNC
or %QSYSFUNC function reference is terminated.
Date_Test_3 :
31 %put Date_Test_4 : &period_end;
SYMBOLGEN: Macro variable PERIOD_END resolves to 31DEC2024
Date_Test_4 : 31DEC2024
32 %put Date_Test_5 : &period_lkbk;
SYMBOLGEN: Macro variable PERIOD_LKBK resolves to 01JUL2023
Date_Test_5 : 01JUL2023
33 %put Date_Test_6 : &period_lkfw;
SYMBOLGEN: Macro variable PERIOD_LKFW resolves to .
Date_Test_6 : .
最终我想在 proc sql 中的语句中使用它,如下所示:
WHERE startdate >= "&period_lkbk."d and enddate <= "&period_lkfw."d;
为什么 p_end 没有解析?我感觉我已经很接近了,但却无法到达终点。