Hi, I am trying to minimize a function (that I created with proc fcmp) per group of observations that are defined by a category column. I tried using this example (http://support.sas.com/kb/42/332.html) for by group processing with proc optmodel but I'm not sure if the way I'm indexing the observations is correct. 
 
This is my code:
options cmplib=work.funcs;
%let byvar=pat_ID;
proc optmodel cdigits=10 fdigits=10 pdigits=9;
	set OBS;
	str pat_ID {OBS};
	num UW_acc{OBS};
	num EP_incr{OBS};
	num Time{OBS};
	read data work.pat_data into OBS=[_n_] pat_ID=pat_ID;
	read data work.pat_data into OBS=[_n_] UW_acc=UW_pattern_acc;
	read data work.pat_data into OBS=[_n_] EP_incr=EP_pattern_incr;
	read data work.pat_data into OBS=[_n_] Time=Year;
	/* Collect values of BY variable into an index set */
	set BYSET = setof {i in OBS} &byvar.[i];
	put BYSET=;
	str by;
	/* variables */
	set OBS_BY = {i in OBS: &byvar.[i] = by};
	var a >=&LOW_A. <= &UP_A. init &INITA.;
	var b >=&LOW_B. <= &UP_B. init &INITB.;
	
	minimize FuncVal = pat_opt(a, b, UW_acc[OBS_BY], EP_incr[OBS_BY], Time[OBS_PY]);
	/*variables to store result*/
	num a_sol {BYSET};
	num b_sol {BYSET};
	do by = BYSET;
		put by=;
		solve with nlp / multistart=(maxstarts=&MAXSTART.) seed=&SEED. maxiter=&MAXITER. opttol=&OPTTOL. feastol=&FEASTOL.;
		print a b;
		a_sol[by] = a.sol;
		b_sol[by] = b.sol;
	end;
	create data work.solver_results from [by] a=a_sol b=b_sol;
run; 
proc print data=work.solver_results;
run;
quit;
TITLE;
 
When running the code I get "subscript 1 may not be a set" as an error for this line for each variable uw_acc, ep_incr and time.
minimize FuncVal = pat_opt(a, b, UW_acc[OBS_BY], EP_incr[OBS_BY], Time[OBS_PY]);
 
Let me know, if you need more info!
Thanks!
 
(I'm using SAS Version 9.4)