## loop over two sets

Problems with syntax of GAMS
yanzhiping
User
Posts: 85
Joined: 11 months ago

### loop over two sets

Does anyone know how to loop over two sets in GAMS?
I have a code as following, but it is not working.

set
A /a,b,c/;
A1 /a1,b1/;
table
B(A, A1)
a.a1 1
a.b1 1
b.a1 2
b.b1 3
c.a1 2
c.b1 3;
parameter
C(A,A1)
;
loop((A,A1),
C(A,A1) =ceil(B(A, A1)/2);
);

Display C;

yanzhiping
User
Posts: 85
Joined: 11 months ago

### Re: loop over two sets

I made a mistake. I think it works.

User
Posts: 55
Joined: 1 year ago

### Re: loop over two sets

Loop part is correct, declarations are not. I took the code to correct syntax (other ways are also possible):

sets
A /a,b,c/
A1 /a1,b1/;
parameter
B(A, A1) /
a.a1 1
a.b1 1
b.a1 2
b.b1 3
c.a1 2
c.b1 3/;
parameter
C(A,A1)
;
loop((A,A1),
C(A,A1) =ceil(B(A, A1)/2);
);

Display C;

User
Posts: 55
Joined: 1 year ago

### Re: loop over two sets

Besides: the loop is actually unnecesary.

C(A,A1) =ceil(B(A, A1)/2);

will just do the trick

yanzhiping
User
Posts: 85
Joined: 11 months ago

### Re: loop over two sets

If we have an example as below, how can we simply the loop below?

Code: Select all

``````set Year
dset;
parameter A(Year);

Loop((Year),
If (ord(Year)<card(Year),
A(Year) = (card(Year)-1)*card(dset) - (ord(Year)-1)*card(dset);
Else
A(Year) = 0;
);
);``````
It seems the following doesn't work.

Code: Select all

``A(Year\$ord(Year)<card(Year)) = (card(Year)-1)*card(dset) - (ord(Year)-1)*card(dset);``

User
Posts: 55
Joined: 1 year ago

### Re: loop over two sets

this is the correct way:

A(Year)\$(ord(Year)<card(Year)) = (card(Year)-1)*card(dset) - (ord(Year)-1)*card(dset);

yanzhiping
User
Posts: 85
Joined: 11 months ago

### Re: loop over two sets

thanks, this works for me.