我的工作需要从特定日期算起6至18个月。我有两个例子。 例1: 我的工作要求我计算客户的退款。这些退款一次只能是18个月,退款期限的最后日期是信件发送给客户的那一天。因此,如果退款信件设定为2012年8月28日发送,那么该期间的第一个日期是什么时候?此外,如果是8月29日,30日和31日(2012年8月28日 - 2012年8月31日)。我的计算结果表明,所有这些时间段都是2011年3月1日作为开始日期。我使用SAS作为程序工具。这是我使用的功能:
eighteen_months = intnx('month',Visit_date,18,'sameday')+1;
我所做的主要假设是1个月等于第一个月的第一个日期和最后一个日期以及同一个月(2012年1月1日至2012年1月31日)。随后,3个月等于第一个月的第一个日期和第3个月的最后日期(2012年1月1日至2012年3月31日或2012年4月1日至2012年6月30日)。以下是其他18个月的计算:
First Date Last Date
10/30/1999 4/29/2001
5/6/2006 11/5/2007
4/30/2012 10/29/2013
4/3/2018 10/2/2019
2/24/2024 8/23/2025
5/10/2026 11/9/2027
问题出现在我8月28日至31日的原始例子中,其中18个月实际上可以是4个不同的时间长度。
示例2: 我的工作也要求我做相反的事情,我有第一次约会,需要找出6至18个月的时间段。我最近被问到,如果第一个日期是2012年6月22日,那么6个月是多少?我在12/21/2012回复,并被告知它实际上是12/22/2012。关于如何在公司中计算,没有真正的文档/逻辑。这是我在SAS中的功能。
Six_months = intnx('month',Visit_date,6,'sameday')-1;
我正在尝试建立一个流程,并希望了解是否有人在一致的基础上运行这些流程,以及Excel / SAS中是否有更简单的方法。任何回复都将不胜感激。
答案 0 :(得分:2)
要从Excel中的最后日期获取第一个日期,请尝试此公式,其中最后日期为B2
=EDATE(B2,-18)+1
适用于您列出的6个示例,并且还将从2012年3月28日至2012年8月28日/ 28日回归
EDATE函数,正如您可能猜到的那样,可以用来添加或减去日期之后的几个月,如果该月内不存在该日,如2月30日,则默认为该日期的最后一天月
答案 1 :(得分:0)
许多数据库支持函数add_months
或dateadd
或类似的东西(包括Teradata)。要提前18个月或更晚,请执行以下操作:
select add_months(LastDate, -18), LastDate
我认为在问题之前回答。答案问题有点棘手,因为有多个日期。您可以使用以下技巧获得第一个和最后一个这样的日期:
select add_months(FirstDate, 18) as LastDate1,
add_months(FirstDate+1, 18) - 1 as LastDate2
也就是说,add_months返回的第一天与未来18个月的条件匹配(我很确定这一点,但Teradata不是100%正面)。第二个日期是第二天之后18个月的前一天。通常它与第一个日期相同,但并非总是如此。
答案 2 :(得分:0)
这是Lorin和Dan的蛮力方式
%MACRO DATES(YEAR, lib);
data post.try&lib.; set post.pact;
IF DTE_EFFECTIVE LE &year. LE DTE_END THEN DO ELIGMONTH= &year.; FORMAT ELIGMONTH MMDDYY10.; OUTPUT post.TRY&lib.; END;
run;
%MEND;
%DATES (YEAR='01JAN2000'D, LIB=a)
%DATES (YEAR='01FEB2000'D, LIB=b)
%DATES (YEAR='01MAR2000'D, LIB=c)
%DATES (YEAR='01APR2000'D, LIB=d)
%DATES (YEAR='01MAY2000'D, LIB=e)
%DATES (YEAR='01JUN2000'D, LIB=f)
%DATES (YEAR='01JUL2000'D, LIB=g)
%DATES (YEAR='01AUG2000'D, LIB=h)
%DATES (YEAR='01SEP2000'D, LIB=i)
%DATES (YEAR='01OCT2000'D, LIB=j)
%DATES (YEAR='01NOV2000'D, LIB=k)
%DATES (YEAR='01DEC2000'D, LIB=l)
%DATES (YEAR='01JAN2001'D, LIB=m)
%DATES (YEAR='01FEB2001'D, LIB=n)
%DATES (YEAR='01MAR2001'D, LIB=o)
%DATES (YEAR='01APR2001'D, LIB=p)
%DATES (YEAR='01MAY2001'D, LIB=q)
%DATES (YEAR='01JUN2001'D, LIB=r)
%DATES (YEAR='01JUL2001'D, LIB=s)
%DATES (YEAR='01AUG2001'D, LIB=t)
%DATES (YEAR='01SEP2001'D, LIB=u)
%DATES (YEAR='01OCT2001'D, LIB=v)
%DATES (YEAR='01NOV2001'D, LIB=w)
%DATES (YEAR='01DEC2001'D, LIB=x)
%DATES (YEAR='01JAN2002'D, LIB=y)
%DATES (YEAR='01FEB2002'D, LIB=z)
%DATES (YEAR='01MAR2002'D, LIB=aa)
%DATES (YEAR='01APR2002'D, LIB=bb)
%DATES (YEAR='01MAY2002'D, LIB=cc)
%DATES (YEAR='01JUN2002'D, LIB=dd)
%DATES (YEAR='01JUL2002'D, LIB=ee)
%DATES (YEAR='01AUG2002'D, LIB=ff)
%DATES (YEAR='01SEP2002'D, LIB=gg)
%DATES (YEAR='01OCT2002'D, LIB=hh)
%DATES (YEAR='01NOV2002'D, LIB=ii)
%DATES (YEAR='01DEC2002'D, LIB=jj)
%DATES (YEAR='01JAN2003'D, LIB=kk)
%DATES (YEAR='01FEB2003'D, LIB=ll)
%DATES (YEAR='01MAR2003'D, LIB=mm)
%DATES (YEAR='01APR2003'D, LIB=nn)
%DATES (YEAR='01MAY2003'D, LIB=oo)
%DATES (YEAR='01JUN2003'D, LIB=pp)
%DATES (YEAR='01JUL2003'D, LIB=qq)
%DATES (YEAR='01AUG2003'D, LIB=rr)
%DATES (YEAR='01SEP2003'D, LIB=ss)
%DATES (YEAR='01OCT2003'D, LIB=tt)
%DATES (YEAR='01NOV2003'D, LIB=uu)
%DATES (YEAR='01DEC2003'D, LIB=vv)
%DATES (YEAR='01JAN2004'D, LIB=ww)
%DATES (YEAR='01FEB2004'D, LIB=xx)
%DATES (YEAR='01MAR2004'D, LIB=yy)
%DATES (YEAR='01APR2004'D, LIB=zz)
%DATES (YEAR='01MAY2004'D, LIB=aa)
%DATES (YEAR='01JUN2004'D, LIB=bb)
%DATES (YEAR='01JUL2004'D, LIB=cc)
%DATES (YEAR='01AUG2004'D, LIB=dd)
%DATES (YEAR='01SEP2004'D, LIB=ee)
%DATES (YEAR='01OCT2004'D, LIB=ff)
%DATES (YEAR='01NOV2004'D, LIB=gg)
%DATES (YEAR='01DEC2004'D, LIB=hh)
%DATES (YEAR='01JAN2005'D, LIB=ii)
%DATES (YEAR='01FEB2005'D, LIB=jj)
%DATES (YEAR='01MAR2005'D, LIB=kk)
%DATES (YEAR='01APR2005'D, LIB=ll)
%DATES (YEAR='01MAY2005'D, LIB=mm)
%DATES (YEAR='01JUN2005'D, LIB=nn)
%DATES (YEAR='01JUL2005'D, LIB=oo)
%DATES (YEAR='01AUG2005'D, LIB=pp)
%DATES (YEAR='01SEP2005'D, LIB=qq)
%DATES (YEAR='01OCT2005'D, LIB=rr)
%DATES (YEAR='01NOV2005'D, LIB=ss)
%DATES (YEAR='01DEC2005'D, LIB=tt)
%DATES (YEAR='01JAN2006'D, LIB=uu)
%DATES (YEAR='01FEB2006'D, LIB=vv)
%DATES (YEAR='01MAR2006'D, LIB=ww)
%DATES (YEAR='01APR2006'D, LIB=xx)
%DATES (YEAR='01MAY2006'D, LIB=yy)
%DATES (YEAR='01JUN2006'D, LIB=zz)
%DATES (YEAR='01JUL2006'D, LIB=aaa)
%DATES (YEAR='01AUG2006'D, LIB=bbb)
%DATES (YEAR='01SEP2006'D, LIB=ccc)
%DATES (YEAR='01OCT2006'D, LIB=ddd)
%DATES (YEAR='01NOV2006'D, LIB=eee)
%DATES (YEAR='01DEC2006'D, LIB=fff)
%DATES (YEAR='01JAN2007'D, LIB=ggg)
%DATES (YEAR='01FEB2007'D, LIB=hhh)
%DATES (YEAR='01MAR2007'D, LIB=iii)
%DATES (YEAR='01APR2007'D, LIB=jjj)
%DATES (YEAR='01MAY2007'D, LIB=kkk)
%DATES (YEAR='01JUN2007'D, LIB=lll)
%DATES (YEAR='01JUL2007'D, LIB=mmm)
%DATES (YEAR='01AUG2007'D, LIB=nnn)
%DATES (YEAR='01SEP2007'D, LIB=ooo)
%DATES (YEAR='01OCT2007'D, LIB=ppp)
%DATES (YEAR='01NOV2007'D, LIB=qqq)
%DATES (YEAR='01DEC2007'D, LIB=rrr)
%DATES (YEAR='01JAN2008'D, LIB=sss)
%DATES (YEAR='01FEB2008'D, LIB=ttt)
%DATES (YEAR='01MAR2008'D, LIB=uuu)
%DATES (YEAR='01APR2008'D, LIB=vvv)
%DATES (YEAR='01MAY2008'D, LIB=www)
%DATES (YEAR='01JUN2008'D, LIB=xxx)
%DATES (YEAR='01JUL2008'D, LIB=yyy)
%DATES (YEAR='01AUG2008'D, LIB=zzz)
%DATES (YEAR='01SEP2008'D, LIB=aaaa)
%DATES (YEAR='01OCT2008'D, LIB=bbbb)
%DATES (YEAR='01NOV2008'D, LIB=cccc)
%DATES (YEAR='01DEC2008'D, LIB=dddd)
%DATES (YEAR='01JAN2009'D, LIB=eeee)
%DATES (YEAR='01FEB2009'D, LIB=ffff)
%DATES (YEAR='01MAR2009'D, LIB=gggg)
%DATES (YEAR='01APR2009'D, LIB=hhhh)
%DATES (YEAR='01MAY2009'D, LIB=iiii)
%DATES (YEAR='01JUN2009'D, LIB=jjjj)
%DATES (YEAR='01JUL2009'D, LIB=kkkk)
%DATES (YEAR='01AUG2009'D, LIB=llll)
%DATES (YEAR='01SEP2009'D, LIB=mmmm)
%DATES (YEAR='01OCT2009'D, LIB=nnnn)
%DATES (YEAR='01NOV2009'D, LIB=oooo)
%DATES (YEAR='01DEC2009'D, LIB=pppp)
%DATES (YEAR='01JAN2010'D, LIB=qqqq)
%DATES (YEAR='01FEB2010'D, LIB=rrrr)
%DATES (YEAR='01MAR2010'D, LIB=ssss)
%DATES (YEAR='01APR2010'D, LIB=tttt)
%DATES (YEAR='01MAY2010'D, LIB=uuuu)
%DATES (YEAR='01JUN2010'D, LIB=vvvv)
%DATES (YEAR='01JUL2010'D, LIB=wwww)
%DATES (YEAR='01AUG2010'D, LIB=xxxx)
%DATES (YEAR='01SEP2010'D, LIB=yyyy)
%DATES (YEAR='01OCT2010'D, LIB=zzzz)
%DATES (YEAR='01NOV2010'D, LIB=aaaaa)
%DATES (YEAR='01DEC2010'D, LIB=bbbbb)
%DATES (YEAR='01JAN2011'D, LIB=ccccc)
%DATES (YEAR='01FEB2011'D, LIB=ddddd)
%DATES (YEAR='01MAR2011'D, LIB=eeeee)
%DATES (YEAR='01APR2011'D, LIB=fffff)
%DATES (YEAR='01MAY2011'D, LIB=ggggg)
%DATES (YEAR='01JUN2011'D, LIB=hhhhh)
%DATES (YEAR='01JUL2011'D, LIB=iiiii)
%DATES (YEAR='01AUG2011'D, LIB=jjjjj)
%DATES (YEAR='01SEP2011'D, LIB=kkkkk)
%DATES (YEAR='01OCT2011'D, LIB=lllll)
%DATES (YEAR='01NOV2011'D, LIB=mmmmm)
%DATES (YEAR='01DEC2011'D, LIB=nnnnn)
%DATES (YEAR='01JAN2012'D, LIB=ooooo)
%DATES (YEAR='01FEB2012'D, LIB=ppppp)
%DATES (YEAR='01MAR2012'D, LIB=qqqqq)
%DATES (YEAR='01APR2012'D, LIB=rrrrr)
%DATES (YEAR='01MAY2012'D, LIB=sssss)
%DATES (YEAR='01JUN2012'D, LIB=ttttt)
%DATES (YEAR='01JUL2012'D, LIB=uuuuu)
%DATES (YEAR='01AUG2012'D, LIB=vvvvv)
%DATES (YEAR='01SEP2012'D, LIB=wwwww)
%DATES (YEAR='01OCT2012'D, LIB=xxxxx)
%DATES (YEAR='01NOV2012'D, LIB=yyyyy)
%DATES (YEAR='01DEC2012'D, LIB=zzzzz)
%DATES (YEAR='01JAN2013'D, LIB=aaaaaa)
%DATES (YEAR='01FEB2013'D, LIB=bbbbbb)
%DATES (YEAR='01MAR2013'D, LIB=cccccc)
%DATES (YEAR='01APR2013'D, LIB=dddddd)
%DATES (YEAR='01MAY2013'D, LIB=eeeeee)
%DATES (YEAR='01JUN2013'D, LIB=ffffff)
%DATES (YEAR='01JUL2013'D, LIB=gggggg)
%DATES (YEAR='01AUG2013'D, LIB=hhhhhh)
%DATES (YEAR='01SEP2013'D, LIB=iiiiii)
%DATES (YEAR='01OCT2013'D, LIB=jjjjjj)
%DATES (YEAR='01NOV2013'D, LIB=kkkkkk)
%DATES (YEAR='01DEC2013'D, LIB=llllll)
data post.final; set post.TRYa
post.TRYb
post.TRYc
post.TRYd
post.TRYe
post.TRYf
post.TRYg
post.TRYh
post.TRYi
post.TRYj
post.TRYk
post.TRYl
post.TRYm
post.TRYn
post.TRYo
post.TRYp
post.TRYq
post.TRYr
post.TRYs
post.TRYt
post.TRYu
post.TRYv
post.TRYw
post.TRYx
post.TRYy
post.TRYz
post.TRYaa
post.TRYbb
post.TRYcc
post.TRYdd
post.TRYee
post.TRYff
post.TRYgg
post.TRYhh
post.TRYii
post.TRYjj
post.TRYkk
post.TRYll
post.TRYmm
post.TRYnn
post.TRYoo
post.TRYpp
post.TRYqq
post.TRYrr
post.TRYss
post.TRYtt
post.TRYuu
post.TRYvv
post.TRYww
post.TRYxx
post.TRYyy
post.TRYzz
post.TRYaa
post.TRYbb
post.TRYcc
post.TRYdd
post.TRYee
post.TRYff
post.TRYgg
post.TRYhh
post.TRYii
post.TRYjj
post.TRYkk
post.TRYll
post.TRYmm
post.TRYnn
post.TRYoo
post.TRYpp
post.TRYqq
post.TRYrr
post.TRYss
post.TRYtt
post.TRYuu
post.TRYvv
post.TRYww
post.TRYxx
post.TRYyy
post.TRYzz
post.TRYaaa
post.TRYbbb
post.TRYccc
post.TRYddd
post.TRYeee
post.TRYfff
post.TRYggg
post.TRYhhh
post.TRYiii
post.TRYjjj
post.TRYkkk
post.TRYlll
post.TRYmmm
post.TRYnnn
post.TRYooo
post.TRYppp
post.TRYqqq
post.TRYrrr
post.TRYsss
post.TRYttt
post.TRYuuu
post.TRYvvv
post.TRYwww
post.TRYxxx
post.TRYyyy
post.TRYzzz
post.TRYaaaa
post.TRYbbbb
post.TRYcccc
post.TRYdddd
post.TRYeeee
post.TRYffff
post.TRYgggg
post.TRYhhhh
post.TRYiiii
post.TRYjjjj
post.TRYkkkk
post.TRYllll
post.TRYmmmm
post.TRYnnnn
post.TRYoooo
post.TRYpppp
post.TRYqqqq
post.TRYrrrr
post.TRYssss
post.TRYtttt
post.TRYuuuu
post.TRYvvvv
post.TRYwwww
post.TRYxxxx
post.TRYyyyy
post.TRYzzzz
post.TRYaaaaa
post.TRYbbbbb
post.TRYccccc
post.TRYddddd
post.TRYeeeee
post.TRYfffff
post.TRYggggg
post.TRYhhhhh
post.TRYiiiii
post.TRYjjjjj
post.TRYkkkkk
post.TRYlllll
post.TRYmmmmm
post.TRYnnnnn
post.TRYooooo
post.TRYppppp
post.TRYqqqqq
post.TRYrrrrr
post.TRYsssss
post.TRYttttt
post.TRYuuuuu
post.TRYvvvvv
post.TRYwwwww
post.TRYxxxxx
post.TRYyyyyy
post.TRYzzzzz
post.TRYaaaaaa
post.TRYbbbbbb
post.TRYcccccc
post.TRYdddddd
post.TRYeeeeee
post.TRYffffff
post.TRYgggggg
post.TRYhhhhhh
post.TRYiiiiii
post.TRYjjjjjj
post.TRYkkkkkk
post.TRYllllll
;run;