sasoptpy.actions.read_data

read_data(table, index, columns)[source]

Reads data tables inside Set and Parameter objects

Parameters
tablestring or swat.cas.table.CASTable

Table object or name to be read, case-insensitive

indexdict

Index properties of the table

Has two main members:

For a given set YEAR and column name year_no, the index dictionary should be written as:

>>> {'target': YEAR, 'key': 'year_no'}

If index is simply the row number, use ‘key’: so.N which is equivalent to the special _N_ character in the SAS language.

columnslist

A list of dictionaries, each holding column properties.

Columns are printed in the specified order. Each column should be represented as a dictionary with following fields:

  • targetsasoptpy.abstract.ParameterGroup

    Target parameter object to be read into

  • columnstring

    Column name to be read from

  • indexsasoptpy.SetIterator, optional

    Subindex for specific column, needed for complex operations

If the name of the sasoptpy.abstract.Parameter object is same as the column name, the following call is enough:

>>> p = so.Parameter(name='price')
>>> read_data(..., columns=[{'target': p}])

For reading a different column name, column field should be specified:

>>> {'target': p, 'column': 'price_usd'}

When working with ParameterGroup objects, sometimes a secondary loop is needed. This is achieved by using the index field, along with the sasoptpy.abstract.statement.ReadDataStatement.append() method.

Returns
rsasoptpy.abstract.statement.ReadDataStatement

Read data statement object, which includes all properties

Additional columns can be added using the sasoptpy.abstract.statement.ReadDataStatement.append() function.

Examples

Reading a regular set:

>>> with Workspace('test_workspace') as ws:
>>>     ITEMS = Set(name='ITEMS')
>>>     value = ParameterGroup(ITEMS, name='value', init=0)
>>>     get = VariableGroup(ITEMS, name='get', vartype=so.INT, lb=0)
>>>     read_data(
...         table="values",
...         index={'target': ITEMS, 'key': None},
...         columns=[{'target': value}])
>>> print(so.to_optmodel(w))
proc optmodel;
    set ITEMS;
    num value {ITEMS} init 0;
    var get {{ITEMS}} integer >= 0;
    read data values into ITEMS value;
quit;

Reading with row index:

>>> with so.Workspace('test_read_data_n') as ws:
>>>     ASSETS = so.Set(name='ASSETS')
>>>     ret = so.ParameterGroup(ASSETS, name='return', ptype=so.NUM)
>>>     read_data(
...         table='means',
...         index={'target': ASSETS, 'key': so.N},
...         columns=[{'target': ret}]
...     )
>>> print(so.to_optmodel(w))
proc optmodel;
    set ASSETS;
    num return {ASSETS};
    read data means into ASSETS=[_N_] return;
quit;

Reading with no index set and subindex:

>>> with so.Workspace('test_read_data_no_index_expression') as ws:
>>>     ASSETS = so.Set(name='ASSETS')
>>>     cov = so.ParameterGroup(ASSETS, ASSETS, name='cov', init=0)
>>>     with iterate(ASSETS, 'asset1') as asset1, iterate(ASSETS, 'asset2') as asset2:
>>>         read_data(
...             table='covdata',
...             index={'key': [asset1, asset2]},
...             columns=[
...                 {'target': cov},
...                 {'target': cov[asset2, asset1],
...                 'column': 'cov'}])
>>> print(so.to_optmodel(w))
proc optmodel;
    set ASSETS;
    num cov {ASSETS, ASSETS} init 0;
    read data covdata into [asset1 asset2] cov cov[asset2, asset1]=cov;
quit;

Reading a column with multiple indices:

>>> with so.Workspace(name='test_read_data_idx_col') as ws:
>>>     dow = so.Set(name='DOW', value=so.exp_range(1, 6))
>>>     locs = so.Set(name='LOCS', settype=so.STR)
>>>     demand = so.ParameterGroup(locs, dow, name='demand')
>>>     with iterate(locs, name='loc') as loc:
>>>         r = read_data(
...             table='dmnd',
...             index={'target': locs, 'key': loc}
...         )
>>>         with iterate(dow, name='d') as d:
>>>             r.append({
...                 'index': d,
...                 'target': demand[loc, d],
...                 'column': concat('day', d)
...             })
>>> optmodel_code = so.to_optmodel(ws)
proc optmodel;
    set DOW = 1..5;
    set <str> LOCS;
    num demand {LOCS, DOW};
    read data dmnd into LOCS=[loc] {d in DOW} < demand[loc, d]=col('day' || d) >;
quit;