简体   繁体   English

在SAS中准确创建一个月的日期

[英]Creating a date exactly one month in the past in SAS

I am trying to create two datetime variables in SAS 9.3. 我正在尝试在SAS 9.3中创建两个日期时间变量。 The first, "endDate" is the current datetime at the time the program is run. 第一个“ endDate”是程序运行时的当前日期时间。 The second, "startDate" is exactly one month in the past. 第二个“ startDate”恰好是过去一个月。

My code is: 我的代码是:

  %let endDate = %sysfunc(DATETIME() datetime.);
  %let startDate = %sysfunc(intnx('month', DATETIME(), -1) datetime.);

Based on any documentation I can find, I can't figure out what is wrong with it, but I am getting the following error message: 根据我能找到的任何文档,我无法弄清楚它出了什么问题,但是我收到以下错误消息:

"ERROR: Expected close parenthesis after macro function invocation not found." “错误:未找到宏函数调用后的预期右括号。”

What am I doing wrong? 我究竟做错了什么?

Some additional background: I want to use these two macro variables inside a proc sql statement so I can filter a table to data from within the past month at the run-time. 其他一些背景知识:我想在proc sql语句中使用这两个宏变量,以便可以在运行时将表筛选为过去一个月内的数据。

Thanks! 谢谢!

You have a couple of issues: 您有几个问题:

  1. %sysfunc() takes two parameters, the second one is optional, but it does need to be separated by a comma. %sysfunc()具有两个参数,第二个参数是可选的,但确实需要用逗号分隔。
  2. Your use of DateTime() function in the intnx function also requires a %sysfunc() 您在intnx函数中使用DateTime()函数还需要一个%sysfunc()
  3. You have datetime variables so you need to use DTMONTH as your interval instead of month. 您有datetime变量,因此您需要使用DTMONTH作为间隔而不是月份。
  4. You don't need quotes around literals in a macro call 您不需要在宏调用中在文字前后加上引号

      %let endDate = %sysfunc(DATETIME(), datetime.); %put &endDate; %let startDate = %sysfunc(intnx(dtmonth, %sysfunc(datetime()), -1), datetime.); %put &StartDate; 

Four things: 四件事:

  • You don't need the single quotes around string arguments when calling functions via %sysfunc 通过%sysfunc调用函数时,不需要在字符串参数周围加上单引号
  • You need to use a %sysfunc for every individual data step function you want to call in the macro environment, which means you need to nest them in this case as you're calling one function inside another. 您需要为要在宏环境中调用的每个单独的数据步骤函数使用%sysfunc ,这意味着在这种情况下您需要嵌套它们,因为您要在另一个函数中调用另一个函数。 This can get messy. 这会变得凌乱。
  • You need to increment your datetime by a datetime-month increment, not a month increment, so use dtmonth instead of month . 您需要以datetime-month增量而不是month增量来增加datetime,因此请使用dtmonth而不是month
  • You need a comma between the function call and the format within %sysfunc 您需要在函数调用和%sysfunc的格式之间加逗号

Putting it all together: 放在一起:

 %let endDate = %sysfunc(DATETIME(), datetime.);
 %let startDate = %sysfunc(intnx(dtmonth, %sysfunc(DATETIME()), -1), datetime.);
%let endDate = %sysfunc(DATETIME(), datetime21.);
%let startDate =%sysfunc(putn(%sysfunc(intnx(dtmonth, %sysfunc(DATETIME()), -1,same)),datetime21.));
%put  &enddate &startdate;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM