User Tools

Site Tools


gams:sort_a_list_of_set_elements_and_find_duplicate_entries

How do I sort a list of set elements and find double-entries?

Sometimes data from different sources is not sorted and contains duplicate entries, which may cause problems in a GAMS model.

Find below an example, which illustrates how to sort this data and remove duplicate entries. Finally a error message and a short report is written, if duplicate entries were found.

$onecho > s_org.txt
abc
def
ghi
abc
jkl
def
mno
pqr
abc
$offecho

$call sort s_org.txt | uniq > s_uniq.txt
$call sort s_org.txt | uniq -c | awk "$1>1 { print $2,$1 }" > s_dup.txt

set s /
$include s_uniq.txt
/;

$onempty
parameter s_dup(s) 'number of occurances of duplicate elements'/
$include s_dup.txt
/;
$offempty

display s;
abort$card(s_dup) 'Duplicates in s', s_dup;

Output:

...
----     34 SET s  

abc,    def,    ghi,    jkl,    mno,    pqr


----     35 Duplicates in s

----     35 PARAMETER s_dup  number of occurances of duplicate elements

abc 3.000,    def 2.000

**** Exec Error at line 35: User abort: execution halted
gams/sort_a_list_of_set_elements_and_find_duplicate_entries.txt · Last modified: 2008/06/26 14:21 by support