You will have to use a COMPUTE block. There have been many previous forum postings on using the COMPUTE block. You will have to use slightly different techniques, depending on whether terr_id is numeric or character. The basic model for the COMPUTE block will be:
COMPUTE AFTER TERR_ID;
. . . assignment and other statements go here . . .
Investigation of previous forum postings and the PROC REPORT documentation should help you figure out what you want to do.