如何根据SAS的财政年度获得周值?

时间:2015-12-21 16:37:04

标签: sas

我有以下数据集,我需要根据财政年度(例如2013年4月到2014年3月)找到给定日期的周数。例如01AprXXX,应该是一年的第0周或第1周,因此明年3月的结果应该是52/53。我已经尝试过找出相同的方法(代码也在下面)。

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

。提前致谢。如果这个问题是多余的,请告诉我,在这种情况下我会尽早删除它,虽然我搜索这个概念但没有找到任何东西。

我也为我的英语道歉,这可能在语法上不正确。但我希望我能够表达我的观点。

数据

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

1 个答案:

答案 0 :(得分: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;