If you run your program, you should probably see something like this in the log:
NOTE: The query requires remerging summary statistics back with the original data.
Which means that your result will have detail data in your output data.
Still knowing exactly what you want, but I assume you want to sum two periods together in each output record.
Try to have some mechanism that maps two periods together. Since I don't know what kind of periods we're talking about, I can't give a complete solution. One way if you could create a SAS format that maps periods pairwise to a single value, and apply it in the SELECT clause. Then you could do like this:
/* Create format before this step */
proc sql;
create table table&i as
select x , put(idx,MyPeriodFmt.) as id, z, sum(y) as tot_y
from database
group by x, calculated id, z;
quit;
If our periods are dates or months you can probably us some SAS functions to group periods direclty in the SQL.
Conclussion, no need for looping and merging data afterwards, whic will probably save a lot of processing time.
/Linus
Data never sleeps