This appears to be the best so far.
proc iml;
*This is the weight matrix for one month;
begw={ -8,
6,
13,
19};
print begw;
*This is the weight matrix for another month;
endw={ -2,
-3,
1,
12};
print endw;
*These would be the permnos for weight matrix in month 1;
begp={3,1,2,9};
*These would be the permnos for weight matrix in month 2;
endp={3,2,4,7};
print begp;
print endp;
*This merges permnos and weights;
beg=begp||begw;
end=endp||endw;
print beg;
print end;
*This eliminates rows from beg that do not contain permnos in end;
newbeg=beg[t(loc(element(beg[,1],end[,1]))),];
*This creates a matrix of permnos in end not included in beg with zeros as weights;
addbeg=t(setdif(end[,1],beg[,1]))||j(nrow(t(setdif(end[,1],beg[,1]))),1,0);
*This creates a new beg matrix;
finalbeg=newbeg//addbeg;
call sortndx(endx, end, 1);
endsort=end[endx,];
call sortndx(begx,finalbeg, 1);
begsort=finalbeg[begx,];
print newbeg;
print addbeg;
print finalbeg;
print endsort;
print begsort;
quit;
run;