Fluorite | Level 6

## How do I use alldiff for the results of a constraint?

Hi there,

I am unable to use alldiff within a constraint. My intention is to constrain all results from difcolrow to be different from each other. I am using SAS Studio (web based).

Find below a code with an example using >=-3 in the difcolrow constraint to illustrate what I mean:

``````proc optmodel;

set column={'1','2','3','4'};
set row={'1','2','3','4'};

var x{column} >=1 <=4 integer;
var y{row} >=1 <=4 integer;

con alldiff(x);
con alldiff(y);
con difcolrow{i in column}: x[i]-y[i]>=-3;

solve; print x y difcolrow.body; quit;``````

code results

As you can see the difcolrow has several results equal to zero. What I want is all this difcolrow values to be also all different from each other.

I tried too use alldiff in the constraint in different ways, but I got continuous code errors:

``con difcolrow{i in column}: alldiff(x[i]-y[i]);``

It is possible to do it using alldiff? Any other idea about how to do it? Thanks a lot.

1 ACCEPTED SOLUTION

Accepted Solutions
SAS Super FREQ

## Re: How do I use alldiff for the results of a constraint?

Here's a workaround if you are not using the latest release:

``````var diff {column} integer;
con diffcon {i in column}: diff[i] = x[i] - y[i];
con difcolrow: alldiff(diff);``````
4 REPLIES 4
SAS Super FREQ

## Re: How do I use alldiff for the results of a constraint?

``con difcolrow: alldiff({i in column} (x[i]-y[i]));``
Fluorite | Level 6

## Re: How do I use alldiff for the results of a constraint?

I tried your solution and I still get some code errors:

```1          OPTIONS NONOTES NOSTIMER NOSOURCE NOSYNTAXCHECK;
59
60         proc optmodel;
61
62         set column={'1','2','3','4'};
63         set row={'1','2','3','4'};
64
65         var x{column} >=1 <=4 integer;
66         var y{row} >=1 <=4 integer;
67
68         con alldiff(x);
69         con alldiff(y);
70         con difcolrow: alldiff({i in column} (x[i]-y[i]));
_    _   _
22   79  621
200
ERROR 22-322: Expecting a name.

ERROR 79-322: Expecting a {.

ERROR 621-782: Subscript 1 must be a string, found a number.

ERROR 200-322: The symbol is not recognized and will be ignored.

_
525
ERROR 525-782: The symbol 'i' is unknown.

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

70       ! con difcolrow: alldiff({i in column} (x[i]-y[i]));
_
22
ERROR 22-322: Syntax error, expecting one of the following: !!, *, **, +, ',', -, .., /, :, <>, ><, BY, CROSS, DIFF, ELSE, INTER,
SYMDIFF, TO, UNION, ^, ||, }.

71
72         solve;```

Maybe ALLDIFF can only be applied to VAR and not to the result of operations made with VARS?

SAS Super FREQ

## Re: How do I use alldiff for the results of a constraint?

Here's a workaround if you are not using the latest release:

``````var diff {column} integer;
con diffcon {i in column}: diff[i] = x[i] - y[i];
con difcolrow: alldiff(diff);``````
Fluorite | Level 6

## Re: How do I use alldiff for the results of a constraint?

This workaround worked perfectly. Thanks a lot.

I am using SAS Studio, web based. I guess it is not the latest version:

Release: 3.6 (Enterprise Edition)

Supported browsers:
• Microsoft Internet Explorer 11
• Mozilla Firefox 21+
• Apple Safari 6.0+ (on Apple OS X)

Build date: Apr 20, 2017 3:45:55 PM
SAS Mid-tier release: Apr 18, 2017 2:38:00 PM
Java Version: 1.7.0_111
SAS release: 9.04.01M4P11092016
SAS platform: Linux LIN X64 3.10.0-693.2.2.el7.x86_64
Site name: SAS ONDEMAND FOR ACADEMICS
Site number: 70094220