我有2个日期,开始日期和结束日期。
我想知道前35天的日期,然后是每个后续的30天。
我有;
start end
22-Jun-15 22-Oct-15
9-Jan-15 15-May-15
我想要;
start end tik1 tik2 tik3 tik4
22-Jun-15 22-Oct-15 27-Jul-15 26-Aug-15 25-Sep-15
9-Jan-15 15-May-15 13-Feb-15 15-Mar-15 14-Apr-15 14-May-15
我对日期计算很好,但我真正的问题是创建一个变量并递增其名称。我决定把我的整个问题都包括在内,因为我认为在其背景下解释可能更容易。
答案 0 :(得分:2)
您可以通过以下逻辑解决问题:
1)确定要添加的列数。
2)根据需求计算列的值
class AverageTimeForFirstReferral < ActiveRecord::Base
class << self
def update_view_command
# executing the sql statement
self.connection.execute %Q( CREATE OR REPLACE VIEW average_time_for_first_referrals AS
SELECT u.id, u.created_at user_created_at, u.referrer_id, r.`created_at` referrer_created_at, TIMEDIFF('r.created_at','u.created_at') as Time_Diff
from users u inner join users r
on u.`referrer_id`= r.`id`
WHERE TIMEDIFF(r.created_at,u.created_at) > 0;)
end
end
end
替代方式(如果您不想使用proc sql)
data test;
input start end;
informat start date9. end date9.;
format start date9. end date9.;
datalines;
22-Jun-15 22-Oct-15
09-Jan-15 15-May-15
;
run;
/*******Determining number of columns*******/
data noc_cal;
set test;
no_of_col = floor((end-start)/30);
run;
proc sql;
select max(no_of_col) into: number_of_columns from noc_cal;
run;
/*******Making an array where 1st iteration(tik1) is increased by 35days whereas others are incremented by 30days*******/
data test1;
set test;
array tik tik1-tik%sysfunc(COMPRESS(&number_of_columns.));
format tik: date9.;
tik1 = intnx('DAYS',START,35);
do i= 2 to %sysfunc(COMPRESS(&number_of_columns.));
tik[i]= intnx('DAYS',tik[i-1],30);
if tik[i] > end then tik[i]=.;
end;
drop i;
run;
我的输出:
data test;
input start end;
informat start date9. end date9.;
format start date9. end date9.;
datalines;
22-Jun-15 22-Oct-15
09-Jan-15 15-May-15
;
run;
/*******Determining number of columns*******/
data noc_cal;
set test;
no_of_col = floor((end-start)/30);
run;
proc sort data=noc_cal;
by no_of_col;
run;
data _null_;
set noc_cal;
by no_of_col;
if last.no_of_col;
call symputx('number_of_columns',no_of_col);
run;
/*******Making an array where 1st iteration(tik1) is increased by 35days whereas others are incremented by 30days*******/
data test1;
set test;
array tik tik1-tik%sysfunc(COMPRESS(&number_of_columns.));
format tik: date9.;
tik1 = intnx('DAYS',START,35);
do i= 2 to %sysfunc(COMPRESS(&number_of_columns.));
tik[i]= intnx('DAYS',tik[i-1],30);
if tik[i] > end then tik[i]=.;
end;
drop i;
run;
答案 1 :(得分:0)
我倾向于选择长垂直结构。我会这样做:
data want;
set have;
tik=start+35;
do while(tik<=end);
output;
tik=tik+30;
end;
format tik mmddyy10.;
run;
如果你真的需要它,你可以在第二步中转置该数据集。