0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

CASActionオブジェクトの使い方

Posted at

SAS ViyaはAIプラットフォームです。PythonやJava、Rなどの言語を通して利用できます。そのSAS Viyaの中で使われているのがCASTableというテーブルオブジェクトです(CASはCloud Analytic Servicesの略です)。今回はそんなCASTableを抽象的に操作できるCASActionオブジェクトを紹介します。

ライブラリを読み込みます

swatがSAS Viyaを扱うためのライブラリになります。そして、サーバに接続します。

import swat
conn = swat.CAS('server-name.mycompany.com', 5570, 'username', 'password')

Fetchを取得

Fetchを使ってテーブル操作用オブジェクトを取得します。これがCASActionを継承して作られています。

fa = conn.Fetch()
type(fa)
# swat.cas.actions.table.Fetch
type(fa).__bases__
# (swat.cas.actions.CASAction,)

データを5件取得

例えばデータを5件取得する場合は次のようにしています。

fa(table=dict(name='data.iris', caslib='casuser'), to=5)

§ Fetch

sepal_length sepal_width petal_length petal_width species
0 5.1 3.5 1.4 0.2
1 4.9 3.0 1.4 0.2
2 4.7 3.2 1.3 0.2
3 4.6 3.1 1.5 0.2
4 5.0 3.6 1.4 0.2

ソート条件を追加します。sortbyを指定します。

fa(table=dict(name='data.iris', caslib='casuser'), to=5,
              sortby=['sepal_length', 'sepal_width'])

§ Fetch

sepal_length sepal_width petal_length petal_width species
0 4.3 3.0 1.1 0.1
1 4.4 2.9 1.4 0.2
2 4.4 3.0 1.3 0.2
3 4.4 3.2 1.3 0.2
4 4.5 2.3 1.3 0.3

条件を指定する

条件は取得時に指定する他、set_paramsを使うこともできます。

fa.set_params('table', dict(name='data.iris', caslib='casuser'),
              'to', 5)

そうすると条件が残ります。

fa
?.table.Fetch(table=dict(caslib='casuser', name='data.iris'), to=5)
fa()

§ Fetch

sepal_length sepal_width petal_length petal_width species
0 5.1 3.5 1.4 0.2
1 4.9 3.0 1.4 0.2
2 4.7 3.2 1.3 0.2
3 4.6 3.1 1.5 0.2
4 5.0 3.6 1.4 0.2

set_paramsは他にも渡し方が複数あります。

fa.set_params(('table', dict(name='data.iris', caslib='casuser')),
              ('to', 5))

fa.set_params({'table': dict(name='data.iris', caslib='casuser'),
               'to': 5})

fa.set_params(table=dict(name='data.iris', caslib='casuser'),
              to=5)

は同じです。

まとめ

CASTableを直接操作することもできますが。CASActionを使ってテーブル自体を引数としてプログラマブルに操作もできます。好みによりますが、使い方を覚えておいて損はないでしょう。

SAS for Developers | SAS

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?