Reversing the order of the set in the loop statement in GAMS

GAMS loop statement is controlled by a set. The loop statements starts from the first element in the set up to the last element. There is no command in GAMS that will make loop work backward so that the last element of the set is started first. There is a trick to make loop work backward on the set.

Let us assume that we want to print 1 to 12 in the reverse order.  Then, the following code prints into an Excel file the numbers from 12 to 1, each in an individual cell:

 

file output/output.csv/;
put output;
sets i /1*12/;
alias(i,j);
loop(j,
loop(i$(ord(i)=13-ord(j)),
put i.tl/;););

The output from this code is:

12
11
10
9
8
7
6
5
4
3
2
1

For further information about the loop statement, see GAMS On-line Documentation


Dr Muhammad Al-Salamah