From: MICKEY_sas_beginner on
Hi,
I have a dataset A which stores the daily hours and completes for 20
weeks. Now I would like to calucate for the weekly cumulative cost by
a certain day (cum_week_cost).
For example, for week1, day2, the cum_week_cost will be
Hours(day1_WEEK1+day2_WEEK1)devided by the Cost of
(Day1_WEEK1+Day2_WEEK1)
For week2's day 2, cum_week_cost will be
Hours(day1_WEEK2+day2_WEEK2)devided by the Cost of
(Day1_WEEK2+Day2_WEEK2)
Below is my code, it doesn't work right the week_C and week_H are not
calucated by week but simply sum of all previous day.
Could you help me take a look?

Thanks!
Wei



DATA test;
set a;

do i=1 to 20;

if week=i then do;
RETAIN week_C 0 week_H 0;
week_C=week_C+ cost;
week_H=week_H+ Hours;
cum_week_cost= week_H/week_C ;
output;
end;
end;
run;
From: Reeza on
On May 27, 2:13 pm, MICKEY_sas_beginner <zengwei...(a)gmail.com> wrote:
> Hi,
> I have a dataset A which stores the daily hours and completes for 20
> weeks. Now I would like to calucate for the weekly cumulative cost by
> a certain day (cum_week_cost).
> For example, for week1, day2, the cum_week_cost will be
> Hours(day1_WEEK1+day2_WEEK1)devided by the Cost of
> (Day1_WEEK1+Day2_WEEK1)
> For week2's day 2, cum_week_cost will be
> Hours(day1_WEEK2+day2_WEEK2)devided by the Cost of
> (Day1_WEEK2+Day2_WEEK2)
> Below is my code, it doesn't work right the week_C and week_H are not
> calucated by week but simply sum of all previous day.
> Could you help me take a look?
>
> Thanks!
> Wei
>
> DATA test;
>   set a;
>
>        do i=1 to 20;
>
>  if week=i then do;
> RETAIN week_C 0 week_H 0;
>  week_C=week_C+ cost;
>  week_H=week_H+ Hours;
>  cum_week_cost= week_H/week_C   ;
>   output;
>         end;
>         end;
>     run;

Not sure if you're looking for cumulative weekly or for each week. If
each week use Group by Processing Instead.

proc means data=have out=data noprint;
by week;
out=want1 sum(week_h)=weekly_hours sum(week_c)=weekly_costs;
run;

data want2;
set want1;
cum_week_cost=weekly_hours/weekly_costs;
run;


If you want it for every week instead...

DATA test;

set a;
RETAIN week_C 0 week_H 0;
by week;

do i=1 to 20;

week_C=week_C+ cost;
week_H=week_H+ Hours;
cum_week_cost= week_H/week_C ;
if last.week output;
end;

run;

Not 100% sure of what you're looking for so not sure those will work.