In reply to Ksharp.

Rick,
Sorry. Last code is not right. I missed one statement. Maybe this could work.
And I also need another condition to avoid the negative value of matrix.

proc iml;
/* first arg is row vector of sums of cols; second arg contains sums of rows */
start RandContingency2x2(_c, _r);
c = rowvec(_c);
r = colvec(_r);
tbl = j(nrow(r),ncol(c), 0);
if sum(c)^=sum(r) then
abort "sum of column is not equal with sum of row";

do i=1 to ncol(c)-1;
do j=1 to nrow(r)-1;
call randgen(a, "Hyper", sum(r), r[j], c[i]);
tbl[j,i]= a;
end;
tbl[j,i]= c[i] - tbl[+,i];
end;
tbl[,i]= r - tbl[,+];

return( tbl );
finish;

call randseed(1);
/* marginals from PROC FREQ example */
c = {12 10 13 }; /* sum of columns */
r = {15, 8,12}; /* sum of rows */
do i = 1 to 3;
tbl = RandContingency2x2(c, r);
print tbl;
end;
quit;