我想在列中添加连续日期'日期'对于列' V1'中的每个唯一值。下面解释了我在做什么以及我得到了什么错误(无法估算日期)。
我有一张如下表所示的表格:
V1 Date
MS000328 04JAN16
MS000328 08JAN16
MS000328 12JAN16
MS000328 08FEB16
MS000328 09FEB16
MS000328 12MAR16
MS000328 15MAR16
MS000328 16APR16
MS000328 17APR16
MS000329 02JAN16
MS000329 16JAN16
MS000329 29JAN16
MS000329 03FEB16
MS000329 19FEB16
MS000329 21MAR16
MS000329 31MAR16
MS000329 11APR16
MS000329 19APR16
我想要的是这样的:
V1 Date
MS000328 01JAN16
MS000328 02JAN16
MS000328 03JAN16
MS000328 04JAN16
MS000328 05JAN16
MS000328 06JAN16
MS000328 07JAN16
MS000328 08JAN16
.
.
.
.
.
MS000329 01JAN16
MS000329 02JAN16
MS000329 03MAR16
.
.
MS000329 01APR16
MS000329 02APR16
.
.
MS000329 01MAR16
MS000329 02MAR16
.
.
MS000329 01APR16
MS000329 02APR16
.
.
.
我正在使用以下代码......
data want1 (keep = V1 Date);
set have;
by V1;
run;
data want;
set want1;
format Date date.;
Date = "1JAN2016"d;
do i = 0 to 365;
Date+1;
output;
end;
run;
结果日志显示错误...'文件空间不足
我做错了什么?
答案 0 :(得分:2)
如果您获得了ETS许可,那么PROC EXPAND
将为您做到这一点 - 这正是它的用途。
如果不这样做,则需要执行与代码中的操作略有不同的操作。 RETAIN
在这里很有帮助。如果你想要其他值,并且希望保存前进而不是向后(如我的代码所做)或者你想要做其他事情(比如',那么你需要做类似但略有不同的事情。最近的日期')。
data have;
input V1 $ Date :date7.;
datalines;
MS000328 04JAN16
MS000328 08JAN16
MS000328 12JAN16
MS000328 08FEB16
MS000328 09FEB16
MS000328 12MAR16
MS000328 15MAR16
MS000328 16APR16
MS000328 17APR16
MS000329 02JAN16
MS000329 16JAN16
MS000329 29JAN16
MS000329 03FEB16
MS000329 19FEB16
MS000329 21MAR16
MS000329 31MAR16
MS000329 11APR16
MS000329 19APR16
;;;;
run;
data want(rename=tempdate=date);
set have;
by v1;
retain lastDate;
drop date lastdate;
if first.v1 then lastDate = '31DEC2015'd; *lets us start on JAN 1;
do tempdate = lastDate+1 to date; *iterate from previous date + 1 to current date;
output;
end;
if last.V1 then do; *if last record, need to iterate until DEC31, I think;
do tempDate = date+1 to '31DEC2016'd;
output;
end;
end;
lastDate = date; *save current date for next iteration;
format tempDate date9.;
run;
答案 1 :(得分:0)
我怀疑上述过程会导致记忆问题......
我的答案仍然如下......
Data Have;
Input V1 $ Date Date9.;
Format Date Date9.;
Datalines;
MS000328 04JAN16
MS000328 08JAN16
MS000328 12JAN16
MS000328 08FEB16
MS000328 09FEB16
MS000328 12MAR16
MS000328 15MAR16
MS000328 16APR16
MS000328 17APR16
MS000329 02JAN16
MS000329 16JAN16
MS000329 29JAN16
MS000329 03FEB16
MS000329 19FEB16
MS000329 21MAR16
MS000329 31MAR16
MS000329 11APR16
MS000329 19APR16
;
Run;
/* Series of Dates */
Data Dates (Keep=Date);
Format Date Date9.;
Do i=0 To 365;
Date=IntNX('day','01JAN2016'd,i);
Output;
End;
Run;
/* Vars */
Proc SQL;
Create Table V1_IDs As Select Distinct V1 From Have Order By V1;
Quit;
/* series */
Proc SQL;
Create Table Want As Select * From V1_IDs,Dates;
Quit;
如果有任何问题,请告诉我......