Tuesday, June 2, 2009

Using awk from GAMS to split set elements

I have a set with

ARG_BT_EX
ARG_BT_IM
ARG_BT_NT
ARG_BT_QC

Now I want to extract from this three sets: Country (ARG), Commodity (BT) and Other (EX,..).

This can be done easily with AWK:

$onecho > baseset.inc
ARG_BT_EX
ARG_BT_IM
ARG_BT_NT
ARG_BT_QC
ARG_BT_QP
$offecho

$onecho > gen.awk
BEGIN{
  FS="_"
}
{
  print $0 "." $1 "." $2 "." $3
}
$offecho

$call awk.exe -f gen.awk baseset.inc > extset.inc

set i(*) /
$include baseset.inc
/;

set imap(i,*,*,*) /
$include extset.inc
/;

alias (*,s1,s2,s3);

set country(*);
set commodity(*);
set other(*);

loop(imap(i,s1,s2,s3),
  country(s1) = yes;
  commodity(s2) = yes;
  other(s3) = yes;
);

execute_unload "sets.gdx",imap,i,country,commodity,other;

The AWK script will generate the mapping set imap:

ARG_BT_EX.ARG.BT.EX
ARG_BT_IM.ARG.BT.IM
ARG_BT_NT.ARG.BT.NT
ARG_BT_QC.ARG.BT.QC
ARG_BT_QP.ARG.BT.QP

while the rest of the GAMS code will extract the individual sets from this compound set.

AWK is included in the Windows versions of GAMS.