BookmarkSubscribeRSS Feed
buttadam
Calcite | Level 5

Hello all,

I'm having a problem trying to make some code more efficient in PROC OPTMODEL.  The key difficulty of the code is that a component of the objective requires a linear interpolation of values in an existing table.  An abridged version of the code, which was working but very slow, is below:

proc optmodel printlevel=0;

           ..........

            impvar u{i in 1..&NumSims}=p*(1+r*raa+s*(1-raa))-q*(1+r*raa+s*(1-raa))^0.5;

            impvar y{i in 1..&NumSims}=

                if u<v[21] then w[21]

                else if u<v[20] then w[20]+(w[21]-w[20])*(u-v[20])/(v[21]-v[20])

                else if u<v[19] then w[19]+(w[20]-w[19])*(u-v[19])/(v[20]-v[19])

               ..........

               ..........

                else if u<v[2] then w[2]+(w[3]-w[2])*(u-v[2])/(v[3]-v[2])

                else w[1]+(w[2]-w[1])*(u-v[1])/(v[2]-v[1]))

                ;

            min obj = ........

            solve with nlp / opttol=0.00001 maxtime=2;

            .........

        quit;

I established that the long line of if / else if statements was slowing the program down to unacceptable levels and set out to remove the if statements but maintain the linear interpolation.  My proposed solution was to define some additional impvars (m and n) that could identify the upper and lower location of the linear interpolation without the if statements.  The revised code, with changes in red, is below.

        proc optmodel printlevel=0;

            ..........

            impvar u{i in 1..&NumSims}=p*(1+r*raa+s*(1-raa))-q*(1+r*raa+s*(1-raa))^0.5;

            impvar m{i in 1..&NumSims}=min(21,max(1,int(1+20*(1-u/v[1]))));

            impvar n{i in 1..&NumSims}=min(m+1,21);

            impvar y{i in 1..&NumSims}=w[m]+(w[n]-w[m])*(max(u,v[n])-v[m])/(v[n]-v[m]); 

            min obj = .........

            solve with nlp / opttol=0.00001 maxtime=2;

            ........

        quit;

Running this code gives an error "Subscript 1 may not depend on a variable".  Clearly you cannot reference an array element with another array element like I was trying to do with w[m], etc.  Is there any other way to run a linear interpolation in this way that avoids the use of if / else if statements and hence speeds things up?

Thanks in advance!

Adam.

1 REPLY 1
RobPratt
SAS Super FREQ

Questions about PROC OPTMODEL in SAS/OR are better suited for the Mathematical Optimization and Operations Research Community.  If you still need help, please post there.

hackathon24-white-horiz.png

The 2025 SAS Hackathon Kicks Off on June 11!

Watch the live Hackathon Kickoff to get all the essential information about the SAS Hackathon—including how to join, how to participate, and expert tips for success.

YouTube LinkedIn

What is Bayesian Analysis?

Learn the difference between classical and Bayesian statistical approaches and see a few PROC examples to perform Bayesian analysis in this video.

Find more tutorials on the SAS Users YouTube channel.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 1 reply
  • 1203 views
  • 1 like
  • 2 in conversation