Quartz | Level 8

## Question on Matrix written or expression

Hi,

On SAS proc optmodel,

If I ahve already defined raw_quantity_1[j,m] and raw_quantity1_1[m,q], they are in different constraints.

The relationship between them is sum {q in Price_intervals} raw_quantity1_1[m,q] = raw_quantity_1[1,m]

and sum {q in Price_intervals} raw_quantity2_1[m,q] = raw_quantity_1[2,m]

and sum {q in Price_intervals} raw_quantity3_1[m,q] = raw_quantity_1[3,m]

But raw_quantity_1[1,m], raw_quantity_1[2,m] and raw_quantity_1[3,m] should all be expressed as raw_quantity_1[j,m]

Is there any constraint that I can define like that?

Thanks and sorry I am not able to upload any file.

6 REPLIES 6
SAS Super FREQ

## Re: Question on Matrix written or expression

I would recommend not hard-coding the numbers into your variable names.  If you rename and reindex your variables, you can then express the desired constraints compactly as:

con Mycon {j in JSET, m in MSET}:

sum {q in Price_intervals} raw_quantity_jmq[j,m,q] = raw_quantity_jm[j,m];

This approach provides better separation between model and data, shortens the code, and reduces the likelihood of copy-and-paste errors.

Quartz | Level 8

## Re: Question on Matrix written or expression

Thank you Rob!

May I ask you if not rename or reindex the variables, how could I write them?

Thank you!

SAS Super FREQ

## Re: Question on Matrix written or expression

If you want to keep the original variable names and indices, you could use the SAS macro language, like this:

%do j = 1 %to 3;

con Mycon&j {m in MSET}:

sum {q in Price_intervals} raw_quantity&j._1[m,q] = raw_quantity_1[&j,m];

%end;

To use this approach, you would need to wrap at least this part in a macro (using %macro and %mend).  But my first recommendedation is simpler and more efficient, especially if you have a lot more than three values of j.

Quartz | Level 8

## Re: Question on Matrix written or expression

Hi Rob!

Thanks!

I just wanna try the first way, like below:

%macro cons1;

%do j = 1 %to 3;

con Mycon1&j {j in SUPPLIERS, m in RAW_MATERIALS}:

SUM {q in PRICE_INTERVALS} raw_quantity&j_1[m,q] = raw_quantity_1[&j,m];

%end;

%mend cons1;

%cons1;

With the error message:

! raw_quantity&j_1[m,q] = raw_quantity_1[&j,m];

-

22

76

WARNING: Apparent symbolic reference J_1 not resolved.

ERROR 22-322: Syntax error, expecting one of the following: !!, (, *, **, +, -, .., /, <=, <>, =, ><,

>=, BY, CROSS, DIFF, ELSE, INTER, SYMDIFF, TO, UNION, [, ^, ||.

ERROR 76-322: Syntax error, statement will be ignored.

%cons1;

SAS Super FREQ

## Re: Question on Matrix written or expression

You need the dot after the &j (as in my code).  Also, you should not use j in SUPPLIERS as a constraint index if you are using the macro loop with j.

Quartz | Level 8

## Re: Question on Matrix written or expression

got it~!

Thanks

Discussion stats
• 6 replies
• 1020 views
• 0 likes
• 2 in conversation