例如,我有一个数字日期字段,显示为201603(CalendarYearMonth)。我希望能从这个日期减去90天,这样结束日期是201512(保留年份月)。然后我需要参考另一张表中的结束日期。当我尝试基于此日期加入表时,它们不匹配,因为201512在原始表中显示为20424,而在我尝试加入的表中则显示为20423。你知道为什么我会在201512得到两个不同的值吗?
我尝试了以下方法:
DATA Quote2;
SET Quote;
YearMonth = INPUT(STRIP(PUT(CalendarYearMonth,6.))||'01',YYMMDD10.);
RetentionYearMonth = YearMonth - 90;
FORMAT RetentionYearMonth YYMMN6.;
RUN;
我很困惑。你的代码在我看来是有效的:
37 DATA Quote2;
38 CalendarYearMonth=201603;
39 YearMonth = INPUT(STRIP(PUT(CalendarYearMonth,6.))||'01',YYMMDD10.);
40 RetentionYearMonth = YearMonth - 90;
41 put (CalendarYearMonth YearMonth RetentionYearMonth)(=);
42 put RetentionYearMonth 8.;
43 format YearMonth RetentionYearMonth mmddyy10.;
44 RUN;
CalendarYearMonth=201603 YearMonth=03/01/2016 RetentionYearMonth=12/02/2015
20424
YearMonth是SAS 2016年3月1日的日期。保留年月日是2015年12月2日的SAS日期(年月日前90天)。SAS 2015年12月2日的日期为20424。
再读一遍,你可能希望RetentionYearMonth是SAS 2015年12月1日的日期?如果是这样,那么你就不能简单地减去90天。但是你也许可以使用INTNX函数,例如:
53 DATA Quote2;
54 CalendarYearMonth=201603;
55 YearMonth = INPUT(STRIP(PUT(CalendarYearMonth,6.))||'01',YYMMDD10.);
56 RetentionYearMonth = intnx('month',YearMonth,-3);
57 put (CalendarYearMonth YearMonth RetentionYearMonth)(=);
58 put RetentionYearMonth 8.;
59 format YearMonth RetentionYearMonth mmddyy10.;
60 RUN;
CalendarYearMonth=201603 YearMonth=03/01/2016 RetentionYearMonth=12/01/2015
20423
NOTE: The data set WORK.QUOTE2 has 1 observations and 3 variables.
上面,INTNX返回月初的SAS日期,即YearMonth之前的3个月。
请记住,联接表时,存储的值用于联接,而不是格式化的值(除非您明确将SAS日期转换为文本字符串)。
在SAS中,使用文本表示日期会使您的生活更加艰难,但特定日期表示'01MAR16'd除外,其中引号后面的d表示这是一个日期值。现在您不需要INPUT(STRIP(PUT()))
。
然后,您可以使用像intnx
这样的内置函数来增加或减少90天(或者您想要的任何其他间隔)。
data quote3;
calendaryearmonth = '01MAR16'd;
retentionyearmonth = intnx('day',calendaryearmonth,-90);
format retentionyearmonth yymmn6.;
run;
此外,我指出,calendaryearmonth
中的原始数据值没有与之相关的日期,它只是201603,然后你从中减去90天,这是相对未定义的,尽管你可以让它工作,但你也可能遇到不可预见的问题。我认为,从逻辑上讲,减去3个月而不是90天更有意义。但这更多的是一个问题定义,而不是编程。