## Cumulative Data Step With Varying Lenght

Occasional Contributor
Posts: 7

# Cumulative Data Step With Varying Lenght

Dear all,

I have a following problem. Suppose I have data in form of:

Failing for (in months) First Date of Fail
11 20APR2011
7 25OCT2011
2 08APR2011
3 13APR2011
3 26APR2011
6 02MAY2011
7 03MAY2011
6 11MAY2011

What I'm trying to achieve is to have a cumulative sum of 'Failings' by month and year, i.e. get columns in a form of
Year Month No. of Failed
2011 4 x
2011 5 y
etc.

It is easy to obtain cumulative sum of the column 'First Date of Fail', however I do not know how to incorporate the column 'Failing for (in months)' into the cumulative some (i.e. for the first observation, it will be counted as a fail for 11 months in the row including the first month of the fail)

Could anyone suggest an appropriate code or procedure in SAS?

Thank you,

Simi
Posts: 4,736

## Re: Cumulative Data Step With Varying Lenght

Hope below code is appropriate.

data have;
infile datalines truncover dlm=' ';
input NoOfFailure:8. DateFirstFailure:date9.;
format DateFirstFailure date9.;
datalines;
11 20APR2011
7 25OCT2011
2 08APR2011
3 13APR2011
3 26APR2011
6 02MAY2011
7 03MAY2011
6 11MAY2011
;
run;

Data V_FailureByMonth(keep=YearMonthFailure) / view=V_FailureByMonth;
set have;
format YearMonthFailure yymon7.;
do i=0 to (NoOfFailure-1);
YearMonthFailure=intnx('month',DateFirstFailure,i,'b');
output;
end;
run;

proc freq data=V_FailureByMonth noprint;
table YearMonthFailure / outcum out=want(keep=YearMonthFailure cum_freq);
run;

proc print data=want;
run;

HTH
Patrick
Occasional Contributor
Posts: 7

## Re: Cumulative Data Step With Varying Lenght

Thanks a lot Patrick, that's exactly what I was after!!!
Discussion stats
• 2 replies
• 149 views
• 0 likes
• 2 in conversation