BookmarkSubscribeRSS Feed
vioravis
Calcite | Level 5

Hi Rob,

I am not sure if doing interpolation outside OPTMODEL would work since the calculation of sum of squared errors is done based on the interploated values. So the interpolated values depend on the decision variables x[1] and x[2]. If not interpolation, just the nearest approximation would be sufficient.

Thank you.

Ravi

vioravis
Calcite | Level 5

Hi Rob,

Thanks for your reply. I have a couple of questions on this:

1. First, the do loop I have showed contains more statements than that I showed you:

For example, the following one has two statements that needs to be updated simultaneously with each run of the do loop.

do j = 2 to n;

  updatedRisk = sum(calculatedRisk[1:j-1]*newRisk[(n-j+2😞n]);

  actualRisk = calculatedRisk + updatedRisk 

end;

Is there a way to capture multiple statements as shown inside do loop???

2. Some of the variables declared using IMPVAR will be updated later in the code as I have shown with actualRisk in the do loop?? How do I accomplish this since you mention that formula has to be included in the declaration itself?? In my case, I will declare using IMPVAR once and again update the same variable later in the code. Is it feasible??

Thank you.

Ravi

RobPratt
SAS Super FREQ

1. Yes, you can have multiple statements within a DO loop.

2. No, you cannot update the formula for an IMPVAR after the declaration.  I gave correct syntax for the updatedRisk IMPVAR declaration in the previous reply.  I think you just need a second IMPVAR statement for actualRisk.

I'm not sure, but it looks to me like you might be trying to calculate your objective function by writing a DO loop.  In OPTMODEL, objectives are instead declared with a single algebraic expression.  In your case, you might need to use the SUM operator with multiple-dimensional indices, something like:

sum {i in 1..n, j in 1..i, k in 1..j} mynum[i,j,k] * myvar[i,j,k].

If you write out your objective function on paper as a single algebraic expression, the conversion to the corresponding OPTMODEL code will be immediate.

vioravis
Calcite | Level 5

Hi Rob,

The main problem for us stems from the fact that there is no single equation for the objective funtion but a series of calculations that culminates in one.

I tried the DO loop implementation as follows but getting an error that "A variable declaration may not be a nested statement".

do j = 2 to n;

        impvar updatedRisk{i in j} =  sum {i in 1..j-1} calculatedRisk * newRisk[n-j+1+i];

        impvar newRisk2{i in j} = newRisk1{i in j} + actualRisk{i in j};

end;

Does it mean that an impvar statement cannot be used within in a DO loop??? Please clarify.

Thank you.

Ravi


RobPratt
SAS Super FREQ

That is correct.  Declarations (IMPVAR, VAR, NUM, etc.) cannot be nested in other statements (DO, FOR, etc.).

See http://support.sas.com/documentation/cdl/en/ormpug/63975/HTML/default/viewer.htm#ormpug_optmodel_sec....

vioravis
Calcite | Level 5

Hi Rob,

I think I mixed up the code I gave in the previous post. The reason I am using DO loops is that the two lines of code within them are interdependent.  I have given the code containing the idea below (this doesn't run):

do j = 2 to n;

        impvar updatedRisk{i in j} =  sum {i in 1..j-1} calculatedRisk * newRisk[n-j+1+i];

        impvar calculatedRisk{i in j} = updatedRisk{i in j} + actualRisk{i in j};

end;

As shown here, calculatedRisk is updated each iteration in the second line and the updated one is used in the first line. Given the fact that we cannot use impvar inside a DO loop, is there a way to implement this dependency within OPTMODEL?

Thank you

Ravi


RobPratt
SAS Super FREQ

You cannot have this circular dependency with IMPVAR.  Everything on the right-hand side has to be declared before you can declare anything that depends on it.  It sounds like you instead need to declare explicit variables (VAR) instead of implicit variables (IMPVAR), and then you can use constraints (CON) to enforce the equalities you want to hold.  Something like this:

var updatedRisk {2..n};
var calculatedRisk {2..n};

con mycon1 {j in 2..n}: updatedRisk =  sum {i in 1..j-1} calculatedRisk * newRisk[n-j+1+i];

con mycon1 {j in 2..n}: calculatedRisk = updatedRisk + actualRisk;

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

Register now!

Multiple Linear Regression in SAS

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.

Discussion stats
  • 21 replies
  • 2563 views
  • 0 likes
  • 2 in conversation