sasoptpy.actions.create_data¶
-
create_data
(table, index, columns)[source]¶ Creates data tables from variables, parameters, and expressions
- Parameters
- tablestring
Name of the table to be created
- indexdict
Table index properties
This dictionary can be empty if no index is needed. It can have following fields:
- keylist
List of index keys. Keys can be string or
sasoptpy.abstract.SetIterator
objects
- setlist
List of sets that is being assigned to keys
- columnslist
List of columns. Columns can be
sasoptpy.abstract.Parameter
,sasoptpy.abstract.ParameterGroup
objects or dictionaries. If specified as a dictionary, each can have following keys:- namestring
Name of the column in output table
- expression
sasoptpy.core.Expression
Any expression
- expression
- indexlist or
sasoptpy.abstract.SetIterator
Index for internal loops
- indexlist or
The index field can be used when a subindex is needed. When specified as a list, members should be
sasoptpy.abstract.SetIterator
objects. See examples for more details.
Examples
Regular column:
>>> with so.Workspace('w') as w: >>> m = so.Parameter(name='m', value=7) >>> n = so.Parameter(name='n', value=5) >>> create_data(table='example', index={}, columns=[m, n]) >>> print(so.to_optmodel(w)) proc optmodel; num m = 7; num n = 5; create data example from m n; quit;
Column with name:
>>> with so.Workspace('w') as w: >>> m = so.Parameter(name='m', value=7) >>> n = so.Parameter(name='n', value=5) >>> create_data(table='example', index={}, columns=[ ... {'name': 'ratio', 'expression': m/n} ... ]) >>> print(so.to_optmodel(w)) proc optmodel; num m = 7; num n = 5; create data example from ratio=((m) / (n)); quit;
Column name with concat:
>>> from sasoptpy.util import concat >>> with so.Workspace('w') as w: >>> m = so.Parameter(name='m', value=7) >>> n = so.Parameter(name='n', value=5) >>> create_data(table='example', index={}, columns=[ ... {'name': concat('s', n), 'expression': m+n} ... ]) >>> print(so.to_optmodel(w)) proc optmodel; num m = 7; num n = 5; create data example from col('s' || n)=(m + n); quit;
Table with index:
>>> with so.Workspace('w') as w: >>> m = so.ParameterGroup( >>> so.exp_range(1, 6), so.exp_range(1, 4), name='m', init=0) >>> m[1, 1] = 1 >>> m[4, 1] = 1 >>> S = so.Set(name='ISET', value=[i**2 for i in range(1, 3)]) >>> create_data( ... table='example', ... index={'key': ['i', 'j'], 'set': [S, [1, 2]]}, ... columns=[m] ... ) >>> print(so.to_optmodel(w)) proc optmodel; num m {1..5, 1..3} init 0; m[1, 1] = 1; m[4, 1] = 1; set ISET = {1,4}; create data example from [i j] = {{ISET,{1,2}}} m; quit;
Index over Python range:
>>> with so.Workspace('w') as w: >>> s = so.Set(name='S', value=so.exp_range(1, 6)) >>> x = so.VariableGroup(s, name='x') >>> x[1] = 1 >>> create_data(table='example', ... index={'key': ['i'], 'set': so.exp_range(1, 4)}, columns=[x]) >>> print(so.to_optmodel(w)) proc optmodel; set S = 1..5; var x {{S}}; x[1] = 1; create data example from [i] = {1..3} x; quit;
Append column with index:
>>> from sasoptpy.util import iterate, concat >>> with so.Workspace('w', session=session) as w: >>> alph = so.Set(name='alph', settype=so.string, value=['a', 'b', 'c']) >>> x = so.VariableGroup([1, 2, 3], alph, name='x', init=2) >>> with iterate(so.exp_range(1, 4), name='i') as i: >>> c = create_data( ... table='example', ... index={'key': [i], 'set': [i.get_set()]}) >>> with iterate(alph, name='j') as j: >>> c.append( ... {'name': concat('x', j), ... 'expression': x[i, j], ... 'index': j}) >>> print(so.to_optmodel(w)) proc optmodel; set <str> alph = {'a','b','c'}; var x {{1,2,3}, {alph}} init 2; create data example from [i] = {{1..3}} {j in alph} < col('x' || j)=(x[i, j]) >; quit;
Multiple column indices:
>>> from sasoptpy.util import concat, iterate >>> with so.Workspace('w') as w: >>> S = so.Set(name='S', value=[1, 2, 3]) >>> T = so.Set(name='T', value=[1, 3, 5]) >>> x = so.VariableGroup(S, T, name='x', init=1) >>> with iterate(S, name='i') as i, iterate(T, name='j') as j: >>> create_data( ... table='out', ... index={}, ... columns=[ ... {'name': concat('x', concat(i, j)), 'expression': x[i, j], ... 'index': [i, j]}]) >>> print(so.to_optmodel(w)) proc optmodel; set S = {1,2,3}; set T = {1,3,5}; var x {{S}, {T}} init 1; create data out from {i in S, j in T} < col('x' || i || j)=(x[i, j]) >; quit;