[英]Calculating number of months between 2 dates from SAS INTCK to Python
[英]Number of months between two dates SAS
我有一堆看起來像這樣的數據點數據。 樣品:
Date_1 Date_2
10FEB2010:00:00:00 02/07/08
10FEB2010:00:00:00 03/10/08
10FEB2010:00:00:00 04/07/08
24MAY2010:00:00:00 05/09/08
24MAY2010:00:00:00 06/11/08
24MAY2010:00:00:00 07/11/08
我想從月初開始對兩個日期編制索引,然后在名為“month_from”的新列中獲取介於兩者之間的月數。 我想將新列“month_from”添加到我的示例數據集中,所以它看起來像這樣:
Date_1 Date_2 month_from
10FEB2010:00:00:00 02/07/08 -24
10FEB2010:00:00:00 03/10/08 -23
10FEB2010:00:00:00 04/07/08 -22
24MAY2010:00:00:00 05/09/08 -24
24MAY2010:00:00:00 06/11/08 -23
24MAY2010:00:00:00 07/11/08 -22
我最初的嘗試是:
month_from=intck('month.1','Date_1'd,'Date_2'd);
我使用正確的功能嗎?
如何將變量“month_from”添加到樣本數據集中?
這似乎做你正在尋找的...無法判斷你是否需要將date_1和date_2從字符轉換為數字(日期)..所以我也包括了該代碼。
(也沒有費心去測試INTCK日期變量是否需要在當月1日才能給出正確的結果)
data have;
length date_1 $18 date_2 $8;
infile datalines delimiter=',';
input date_1 $ date_2 $;
datalines;
10FEB2010:00:00:00,02/07/08
10FEB2010:00:00:00,03/10/08
10FEB2010:00:00:00,04/07/08
24MAY2010:00:00:00,05/09/08
24MAY2010:00:00:00,06/11/08
24MAY2010:00:00:00,07/11/08
;
data want (drop=date_1 date_2 dt);
set have;
dt=input(date_1,datetime.);
date1=datepart(dt);
date2=input(date_2,mmddyy10.);
month_from=intck('month',intnx('month',date1,0,'b'),intnx('month',date2,0,'b'));
format date1 date2 mmddyy10.;
run;
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.