2014-04-07 64 views
1

我有一組數據集,有一組用戶的註冊日期和註冊日期。我想以編程方式查找每個用戶在這些日期之間的哪些月份,而不必在任何月份進行硬編碼等。我只想要每個月登記的數字摘要,所以如果這使得它更快,那麼多更好。哪個月份包含在日期範圍內?

E.g.我有一些像

User-+-From-------+-To----------------- 
A + 11JAN2011 + 15MAR2011 
A + 16JUN2011 + 17AUG2011 
B + 10FEB2011 + 12FEB2011 
C + 01AUG2011 + 05AUG2011 

我想是這樣

Month---+-Registrations 
JAN2011 + 1 (A) 
FEB2011 + 2 (AB) 
MAR2011 + 1 (A) 
APR2011 + 0 
MAY2011 + 0 
JUN2011 + 1 (A) 
JUL2011 + 1 (A) 
AUG2011 + 2 (AC) 

注意我不需要括號中的比特;那只是爲了澄清我的觀點。

感謝您的任何幫助。

回答

1

一個簡單的方法是構造一箇中間數據集,然後構造PROC FREQ。

data have; 
informat from to DATE9.; 
format from to DATE9.; 
input user $ from to; 
datalines; 
A  11JAN2011 15MAR2011 
A  16JUN2011 17AUG2011 
B  10FEB2011 12FEB2011 
C  01AUG2011 05AUG2011 
;;;; 
run; 

data int; 
set have; 
_mths=intck('month',from,to,'d'); *number of months after the current one (0=current one). 'd'=discrete=count 1st of month as new month; 
do _i = 0 to _mths; *start with current month, iterate over months; 
    month = intnx('month',from,_i,'b'); 
    output; 
end; 
format month MONYY7.; 
run; 

proc freq data=int; 
tables month/out=want(keep=month count rename=count=registrations); 
run; 

您可以通過在do循環中執行該操作來消除_mths步驟。

+0

太好了。誰知道它會如此簡單。謝謝。 – seestevecode

相關問題