Since the data are sorted by ID, you can read each ID twice. In the first pass, accumulate total pre-costs and total post_costs. In the second pass, retrieve those totals and output the results.
In the absence of sample data in the form of a working DATA step, this code is untested:
data want;
set have (in=firstpass) have (in=secondpass);
by id ;
array _cost_sums {2} _temporary_;
if first.id then call missing(of _cost_sums{*});
if firstpass then _cost_sums{pre_post_period}+cost;
if secondpass;
pre_cost_sums=_cost_sums{1};
post_cost_sums=_cost_sums{2};
run;