BookmarkSubscribeRSS Feed
hadesmr
Calcite | Level 5

Hi all,

Need your help again. I need to declare an impvar g to do the following calculation: if i = 1 then g[1]=0 else g=s*f[i-1]+s*g[i-1]; When i run the code, the log says "The Symbol 'g' is unknown". It looks like i can't reference g[i-1] on the same line when declaring g as impvar (because when i deleted s*g[i-1], the code works perfectly).

Can someone help me fix it? It did work in the Proc NLP.

Thanks a lot. really appreciate it.

HY

data cor;
input org dep;
cards;
11785895 7.42
2335492 7.58
2345245 7.58
2392912 7.53
12755890 7.63
2918402 7.67
2773183 7.68
2824198 7.65
12263433 7.53
2420611 7.53
2338870 7.63
2268830 7.74
2462173 7.74
10775045 7.8
2834195 7.75
2666271 7.55
2734586 7.65
11722258 7.6
2366007 7.71
;
run;


%let xx = .1;

proc optmodel;
set w;
number org{w};
number dep{w};
var s{w} >= 0 <= .8 init 1;
impvar f {i in w} = s * (1 - &xx);

impvar g {i in w} = if i = 1 then  0  else g = s*f[i-1] + s*g[i-1];

impvar ads {i in w} = g * org;
read data work.cor into w = [_n_] org dep;

max correlation = (card(w)*sum{i in w}(ads*dep) - (sum{i in w}(ads)) * (sum{i in w}(dep)))
     / (sqrt(card(w)*sum{i in w}(ads^2) - (sum{i in w}(ads))^2) * sqrt(card(w)*sum{i in w}(dep^2) - (sum{i in w}(dep))^2));
    con c {i in w diff {card(w)}: mod(i,8) ne 0}: s = s[i+1];
solve;
print s f g ads correlation;
quit;

5 REPLIES 5
RobPratt
SAS Super FREQ

Recursive IMPVAR is not supported until the next release of SAS/OR, due out later this year.  The correct syntax will be:

     impvar g {i in w} = if i = 1 then 0 else s*f[i-1] + s*g[i-1];

Until then, you can instead use an explicit variable and constraint.

hadesmr
Calcite | Level 5

Hi RobPratt,

Thanks for letting me know and correcting the code.

Can you show me how to use an explicit variable and constraint to acheive the same result (impvar g {i in w} = if i = 1 then 0 else s*f[i-1] + s*g[i-1];) before the newer verson of SAS is released later this year?

Much appreciative!

hadesmr
Calcite | Level 5

Looks like this code works. What do you think RobPratt? Anyway to make it moer efficient?

And what's the difference between declaring g as VAR and IMPVAR? Thanks!

var g {i in w} >= 0 init 1;

........

.......

con gg {i in w: i>=2}: g = s1*f[i-1] + s1*g[i-1];

RobPratt
SAS Super FREQ

You almost got it, but you still need to force g[1] = 0.  The following works:

     con gg {i in w}: g = if i = 1 then 0 else s1*f[i-1] + s1*g[i-1];

Or you could keep your version and then also include:

     fix g[1] = 0;

When you use IMPVAR, PROC OPTMODEL does the algebraic substitution for you, and the solver does not see an explicit variable or constraint.

hadesmr
Calcite | Level 5

Hi RobPratt,

I just sent you a private discussion because there is something i better not to disclose in the public forum. Thanks for your understanding.

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

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
  • 5 replies
  • 1350 views
  • 0 likes
  • 2 in conversation