我有这个SAS数据集:
symbol date type
1010 01/01/2012 .
1010 01/02/2012 .
1010 01/03/2012 .
1010 01/04/2012 .
1010 01/05/2012 0
1010 01/06/2012 .
1010 01/07/2012 .
1202 02/26/2013 .
1202 02/27/2013 .
1202 02/28/2013 0
1202 03/01/2013 .
1202 03/02/2013 .
1202 03/03/2013 .
1202 03/04/2013 .
数据集按符号和日期分组,其中符号为公司。
我想创建另一个变量USING SAS
来计算Type = 0之前的天数,从-1开始,在Type = 0之后,从每个公司的1开始,所以新的列或变量将是这样的< / p>
symbol date type count
1010 01/01/2012 . -4
1010 01/02/2012 . -3
1010 01/03/2012 . -2
1010 01/04/2012 . -1
1010 01/05/2012 0 0
1010 01/06/2012 . 1
1010 01/07/2012 . 2
1202 02/26/2013 . -2
1202 02/27/2013 . -1
1202 02/28/2013 0 0
1202 03/01/2013 . 1
1202 03/02/2013 . 2
1202 03/03/2013 . 3
1202 03/04/2013 .
非常感谢任何帮助。 感谢
答案 0 :(得分:0)
这是一个解决方案,我做了几个假设:
Count是类型值0的日期与当前记录日期之间的天数。
/* create source data */
data have;
input symbol date :mmddyy10. type;
format date date9.;
datalines;
1010 01/01/2012 .
1010 01/02/2012 .
1010 01/03/2012 .
1010 01/04/2012 .
1010 01/05/2012 0
1010 01/06/2012 .
1010 01/07/2012 .
1202 02/26/2013 .
1202 02/27/2013 .
1202 02/28/2013 0
1202 03/01/2013 .
1202 03/02/2013 .
1202 03/03/2013 .
1202 03/04/2013 .
;
run;
/* sort data, making type value 0 the first record per symbol */
proc sort data=have;
by symbol descending type date;
run;
/* calculate difference in days between records */
data want;
set have;
by symbol;
retain date_zero;
if first.symbol then do;
count=0; /* set first record to zero */
date_zero=date; /* store date for calculations */
end;
else count=date-date_zero; /* numbers of days from first record */
drop date_zero;
run;
/* sort data back to original order */
proc sort data=want;
by symbol date;
run;