2015-12-21 77 views
2

我有以下的数据集,我需要从基于财政年度给出的日期(例如2013年4月至2014年3月)查找周数。例如01AprXXX,应该是一年的第0或第1周,明年3月的最后一周应该是52/53。我尝试了一种方法来找出相同的结果(代码也在下面)。如何根据SAS中的财政年度计算周价值?

I am just curious to know if there is any better way in SAS to do this in SAS

。提前致谢。请让我知道这个问题是否是多余的,在这种情况下,我会尽早删除它,尽管我搜索了这个概念但没有找到任何东西。

同样我对我的英语表示歉意,可能在语法上不正确。但我希望我能够表达我的观点。

DATA

data dsn; 
format date date9.; 
input date date9.; 
cards; 
01Nov2015 
08Sep2013 
06Feb2011 
09Mar2004 
31Mar2009 
01Apr2007 
; 

run; 

CODE

data dsn2; 
set dsn; 
week_normal = week(date); 
dat2 = input(compress("01Apr"||year(date)),date9.); 
week_temp = week(dat2); 
format dat2 date9.; 

x1 = month(input(compress('01Jan'||(year(date)+1)),date9.)) ;***lower cutoff; 
x2 = month(input(compress("31mar"||year(date)),date9.)); ***upper cutoff; 
x3 = week(input(compress("31dec"||(year(date)-1)),date9.)); ***final week value for the previous year; 

if month(dat2) <= month(date) <= month(input(compress("31dec"||year(date)),date9.)) then week_f = week(date) - week_temp; 

else if x2 >= month(date) >= x1 then week_f = week_normal + x3 - week(input(compress("31mar"||(year(date)+1)),date9.)) ; 
run; 

RESULT

enter image description here

回答

4

INTCKINTNX是您最好的选择。您可以按照以下方式使用它们,也可以使用高级功能定义自己的间隔类型(会计年度);这在文档中有更多描述。

data dsn2; 
set dsn; 
week_normal = week(date); 
dat2 = intnx('month12.4',date,0); *12 month periods, starting at month 4 (so 01APR), go to the start of the current one; 
week_F = intck('week',dat2,date); *Can adjust what day this starts on by adding numbers to week, so 'week.1' etc. shifts the start of week day by one; 
format dat2 date9.; 
run; 
+0

谢谢,我是埃格尔知道,如何改变这种间隔类型?请你可以指点我的文档(如果它对你来说不是什么大问题)。我很想了解它的概念谢谢 – PKumar

+0

@KabirPradeep从[INTNX文档]开始(http://support.sas.com/documentation/cdl/en/lrdict/64316/HTML/default/viewer.htm#a000212700 .htm),然后一旦你明白了,请参阅[INTERVALDS = SYSTEM OPTION](http://support.sas.com/documentation/cdl/en/lrdict/64316/HTML/default/viewer.htm#a003252593。 HTM)。 – Joe

+0

非常感谢。 – PKumar