2015-12-21 74 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