2013-03-21 123 views
1

我是SAS新手,想知道如何最有效地列出开始日期和结束日期之间的月份和年份,以及开始和结束日期本身。我读过关于INTCK和INTNX函数,时间序列数据的EXPAND函数,甚至CALENDAR FILL,但我不确定如何将它们用于此特定目的。由于下拉自动填充功能,使用Excel中的小数据集可以轻松完成此任务,但由于数据集的大小,我需要在SAS中找到一种方法。任何建议将不胜感激。谢谢!填写日期范围内的月份和年份?

数据集是在现在这个样子组织了庞大的文本文件:

ID        Start     End 
1000    08/01/2012         12/31/2012 
1001    07/01/2010         05/31/2011 
1002    04/01/1990         10/31/1991 

但输出应该是这样的结尾:

ID  MonthYear 
1000 08/12 
1000 09/12 
1000 10/12 
1000 11/12 
1000 12/12 
1001 07/10 
1001 08/10 
1001 09/10 
1001 10/10 
1001 11/10 
1001 12/10 
1001 01/11 
1001 02/11 
1001 03/11 
1001 04/11 
1001 05/11 
1002 04/90 
1002 05/90 
1002 06/90 
1002 07/90 
1002 08/90 
1002 09/90 
1002 10/90 
1002 11/90 
1002 12/90 
1002 01/91 
1002 02/91 
1002 03/91 
1002 04/91 
1002 05/91 
1002 06/91 
1002 07/91 
1002 08/91 
1002 09/91 
1002 10/91 

回答

5
data want2; 
    set have; 
    do i = 0 to intck('month',start,end); 
     monthyear=intnx('month',start,i,'b'); 
     output; 
     end; 
    format monthyear monyy.; 
    keep id monthyear; 
    run; 
+0

是的,这个作品很棒!非常感谢,@ user2196220! – user2072931 2013-03-22 00:37:52

1

这将这样的伎俩。 PROC EXPAND可能更有效率,但我认为它需要大量所需的观察值,而不是开始/结束组合(尽管你可以得到这个结果,我想)。

data have; 
informat start end MMDDYY10.; 
input ID   Start    End; 
datalines; 
1000  08/01/2012   12/31/2012 
1001  07/01/2010   05/31/2011 
1002  04/01/1990   10/31/1991 
;;;; 
run; 

data want; 
set have; 
format monthyear MMYYS5.; *formats the numeric monthyear variable with your desired format; 
monthyear=start;    *start with the initial observation; 
output;      *output it; 
do _t = 1 by 1 until (month(monthyear)=month(end)); *iterate until end; 
    monthyear = intnx('month',monthyear,1,'b');  *go to the next start of month; 
    output;           *output it; 
end; 
run; 
+0

这工作太,太感谢你了,@Joe!解释有帮助。 – user2072931 2013-03-22 00:37:21

相关问题