# Set reading from an excel sheet: parameter indexed over two sets - one of them previously loaded

**URL:** <https://discuss.ampl.com/t/set-reading-from-an-excel-sheet-parameter-indexed-over-two-sets-one-of-them-previously-loaded/1974>\
**Category:** Support\
**Tags:** general, errors-and-messages, data\
**Created:** [November 5, 2024, 8:32pm UTC](https://discuss.ampl.com/t/set-reading-from-an-excel-sheet-parameter-indexed-over-two-sets-one-of-them-previously-loaded/1974 "2024-11-05T20:32:16Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![vicmanr90](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.ampl.com/vicmanr90/32/55_2.png) [@vicmanr90](https://discuss.ampl.com/u/vicmanr90)\
**Post date:** [November 5, 2024, 8:32pm UTC](https://discuss.ampl.com/t/set-reading-from-an-excel-sheet-parameter-indexed-over-two-sets-one-of-them-previously-loaded/1974/1 "2024-11-05T20:32:16Z")

</div>

Hi people. I have an excel workbook with two different tables. In the first table, I have a costs parameter indexed over a set called product. In the another one, I have a parameter demand, indexed over two sets: the product set, and another set called T.

The first parameter is loaded successfully.

But, when I’m trying to load the other set (T-“Periods”) and parameter “Demand” I got several errors, I have read the book and the forum looking for a similar case, but I haven’t got an answer to my problem.

Some screenshots of the excel data:

 ![image](https://us1.discourse-cdn.com/flex019/uploads/ampl/original/1X/d377dfe64e3df319a9b190b7fa59ff51eb1eb1d7.png)

 ![image](https://us1.discourse-cdn.com/flex019/uploads/ampl/original/1X/13ab8f11148ba88ff23999e39d5254a3d55c4f0d.png)

Here’s my code:

````auto
reset;

set P;
set T;

param costos{P};
param demanda{P,T};

data;

table costos_prod IN "amplxl" "Book1.xlsx":
   P <- [Producto], costos~Costos;

table Demanda IN "amplxl" "Book1.xlsx":
   [T <- t~Periodo, p~Producto], demanda[i,t]~Dit;

load amplxl.dll;
read table costos_prod;
read table Demanda;

display costos;
display demanda; ```
````

---

<div class="post-metadata">

**Author:** ![4er](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.ampl.com/4er/32/530_2.png) [@4er](https://discuss.ampl.com/u/4er)\
**Post date:** [November 8, 2024, 9:37pm UTC](https://discuss.ampl.com/t/set-reading-from-an-excel-sheet-parameter-indexed-over-two-sets-one-of-them-previously-loaded/1974/2 "2024-11-08T21:37:14Z")

</div>

In the `Periodo` column of your second spreadsheet, each member of `T` appears several times. Thus it is not possible to use that spreadsheet to read the `Periodo` values into the set `T`.

For your example, where the times are given by numbers 1, 2, 3, . . . , you could define T instead like this:

```auto
param nT integer > 0;
set T = 1..nT;

```

Then for data you only need to read one integer, the value of `nT`. You can use a table statement like this to read `nT` from a spreadsheet:

```auto
table Periodos IN "amplxl" "Book1.xlsx":
    [], nT~nPeriodos;

```

In the spreadsheet, the range `Periodos` would be just two cells: one containing `nPeriodos`, and one right under it containing the number of periods.

---

<div class="post-metadata">

**Author:** ![vicmanr90](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.ampl.com/vicmanr90/32/55_2.png) [@vicmanr90](https://discuss.ampl.com/u/vicmanr90)\
**Post date:** [November 9, 2024, 3:06pm UTC](https://discuss.ampl.com/t/set-reading-from-an-excel-sheet-parameter-indexed-over-two-sets-one-of-them-previously-loaded/1974/3 "2024-11-09T15:06:30Z")

</div>

Thank you for your response dear Dr. Fourer. My doubt is, however, not yet resolved. What I have understood from the answer is that I have to create anoter table or range in excel just fot the `Periodo` set.

With that said, my doubt is how do I load the parameter `Demanda` (column Dit) to ampl. Would be the code below correct? Let us supose the `param nT ...` statement was added to the model.

```auto
. . .
table Periodos IN "amplxl" "Book1.xlsx":
    [], nT~nPeriodos;

table costos_prod IN "amplxl" "Book1.xlsx":
   P <- [Producto], costos~Costos;

table Demanda IN "amplxl" "Book1.xlsx":
   [t~Periodo, p~Producto], demanda[i,t]~Dit;

load amplxl.dll;
read table Periodos;
read table costos_prod;
read table Demanda;

display costos;
display demanda;

```

---

<div class="post-metadata">

**Author:** ![4er](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.ampl.com/4er/32/530_2.png) [@4er](https://discuss.ampl.com/u/4er)\
**Post date:** [November 10, 2024, 12:57am UTC](https://discuss.ampl.com/t/set-reading-from-an-excel-sheet-parameter-indexed-over-two-sets-one-of-them-previously-loaded/1974/4 "2024-11-10T00:57:12Z")

</div>

That looks good. Have you tried running it in AMPL?
