Union of letters of each elements between two sets

Problems with syntax of GAMS
Post Reply
danielagallo53
User
User
Posts: 1
Joined: 1 year ago

Union of letters of each elements between two sets

Post by danielagallo53 »

Hello. I need help.
How do I create a set to whose elements were added the first 4 letters of another set and the first 2 letters of the elements of another set? For example, I have 2 sets: one is composed of years and the other of quarters:

SET TY /2019*2040/;
SET TQ /Q1*Q4/;

And I need another set that is the Cartesian product of both, but in a single value for each element:

SET T /2019Q1*2019Q4,2020Q1*2020Q4… 2040Q1*2040Q4/;

In Stata for example, I would do this:

Global year “2019 2020 2021 … 2040 “
Global quarter “Q1 Q2 Q3 Q4”

Foreach y of global year {
Foreach q of global quarter{
Global T = ‘y’ ‘q’

}}

How can I did this in GAMS?
Last edited by danielagallo53 1 year ago, edited 2 times in total.
User avatar
bussieck
Moderator
Moderator
Posts: 1043
Joined: 7 years ago

Re: Union of letters of each elements between two sets

Post by bussieck »

In GAMS this is not possible. GAMS does not have ways to create labels from other labels. Usually, there is no need for this. A 2-dim set tyq(ty,qy) with elements 2019.Q1, 2019.Q2, ... 2040.Q3,2040.Q4 works in all places and there is usually no necessity to have a 1-dim set 2019Q1,2019Q2,...,2040Q3,2040Q4. You should think twice why you need this. If the outcome is still that you do need it, then you have to use an "outside" program to get this concatenation of labels. Here are two solutions one with GAMS, the other with embedded Python (see https://www.gams.com/latest/docs/UG_Emb ... ode_Python):

Code: Select all

$onEchoV > concat.gms
SET TY /2019*2040/;
SET TQ /Q1*Q4/;
file ftyq / tyq.gms /;
loop((ty,tq), put ftyq ty.tl:0 tq.tl:0 /);
$offEcho
$call.checkErrorLevel gams concat lo=3 gdx=tyq
SET TY, TQ;
$gdxIn tyq
$load ty tq
SET TYQ /
$include tyq.gms
/;

Code: Select all

SET TY /2019*2040/;
SET TQ /Q1*Q4/;
SET TYQ(*);
$onEmbeddedCode Python:
gams.set('TYQ', [ y+q for y in gams.get('TY') for q in gams.get('TQ') ])
$offEmbeddedCode TYQ
display TYQ;
-Michael
danielagallo53
User
User
Posts: 1
Joined: 1 year ago

Re: Union of letters of each elements between two sets

Post by danielagallo53 »

it works!! thank u so much :D
Post Reply