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)
The argument to UW_acc should be an element of the set OBS. but you are passing OBS_BY, which is instead a subset of OBS. Same story for EP_incr and Time. What does your pat_opt function expect as input?
By the way, you can combine the READ DATA statements as follows:
read data work.pat_data into OBS=[_n_] pat_ID=pat_ID UW_acc=UW_pattern_acc EP_incr=EP_pattern_incr Time=Year;
SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!
Learn how to run multiple linear regression models with and without interactions, presented by SAS user Alex Chaplin.
Find more tutorials on the SAS Users YouTube channel.