The code below illustrates two ways to solve this problem by using PROC OPTMODEL. Both ways use a binary variable for each element, along with a linear constraint that forces exactly k elements to be selected. The first approach uses the LSO solver (new in SAS Optimization 8.2) to maximize the product of selected elements. This way is straightforward but not guaranteed to find an optimal solution. The second approach uses a log transformation to linearize the product and then calls the MILP solver. As long as the maximum product is positive, this method is exact and scales much better than brute force.

%let p = 5; /* number of variables */
%let NObs = 6; /* number of observations */
data have(drop=i j);
call streaminit(123);
array x[&p];
do i = 1 to &NObs;
do j = 1 to dim(x);
x[j] = rand("Integer", -3, 9); /* SAS 9.4M4 */
end;
output;
end;
;

proc print data=have; run;

%let k = 3;

/* LSO solver */
proc optmodel printlevel=0;
/* declare parameters and read data */
num p = &p;
num k = &k;
set OBS;
set ELEMENTS = 1..p;
num x {OBS, ELEMENTS};
read data have into OBS=[_N_] {j in ELEMENTS} < x[_N_,j]=col('x'||j) >;
num i_this;

/* Select[j] = 1 if element j is selected; 0 otherwise */
var Select {ELEMENTS} binary;

/* maximize product of selected elements */
/* max Product = prod {j in ELEMENTS} (if Select[j] = 1 then x[i_this,j] else 1);*/
max Product = prod {j in ELEMENTS} (x[i_this,j] * Select[j] + (1 - Select[j]));

/* select exactly k elements */
con Cardinality:
sum {j in ELEMENTS} Select[j] = k;

/* FOR loop is serial, but COFOR loop is parallel */
set SELECTED = {j in ELEMENTS: Select[j].sol > 0.5};
num maxMul {OBS};
cofor {i in OBS} do;
put i=;
i_this = i;
solve with LSO;
print {j in SELECTED} x[i,j];
maxMul[i] = prod {j in SELECTED} x[i,j];
end;
print maxMul;
quit;

/* MILP solver */
proc optmodel printlevel=0;
/* declare parameters and read data */
num p = &p;
num k = &k;
set OBS;
set ELEMENTS init 1..p;
num x {OBS, ELEMENTS};
read data have into OBS=[_N_] {j in ELEMENTS} < x[_N_,j]=col('x'||j) >;
num i_this;

/* Select[j] = 1 if element j is selected; 0 otherwise */
var Select {ELEMENTS} binary;

/* maximize product of selected elements */
/* max Product = prod {j in ELEMENTS} (x[i_this,j] * Select[j] + (1 - Select[j]));*/
/* linearize by taking log */
max LogProduct = sum {j in ELEMENTS} log(abs(x[i_this,j])) * Select[j];

/* select exactly k elements */
con Cardinality:
sum {j in ELEMENTS} Select[j] = k;

/* to get a positive product, select an even number of negative elements */
var Y >= 0 integer;
con PositiveProduct:
sum {j in ELEMENTS: x[i_this,j] < 0} Select[j] = 2 * Y;

/* FOR loop is serial, but COFOR loop is parallel */
set SELECTED = {j in ELEMENTS: Select[j].sol > 0.5};
num maxMul {OBS};
cofor {i in OBS} do;
put i=;
i_this = i;
ELEMENTS = {j in 1..p: x[i_this,j] ne 0}; /* log(0) is undefined */
solve;
print {j in SELECTED} x[i,j];
maxMul[i] = prod {j in SELECTED} x[i,j];
end;
print maxMul;
quit;