Proc Luaを用いたオブジェクト指向プログラミング

目的

  1. データセットの情報を簡便に取得するために作成.

メソッドの定義

  1. datasetclass.new(_ds):コンストラクタの作成
  2. datasetclass.nobs:レコードの数
  3. datasetclass.nvars:変数の数
  4. datasetclass.meta:データセットのメタ情報

Sample Program (Library作成)

In [5]:
proc lua;
  submit;
    -- prototype
    datasetclass={}
    datasetclass.nobs = function(self)
      local dsid = sas.open(self.ds)
      local nobs = sas.nobs(dsid)
      sas.close(dsid)
      return nobs
    end
    
    datasetclass.nvars = function(self)
      local dsid = sas.open(self.ds)
      local nvars = sas.nvars(dsid)
      sas.close(dsid)
      return nvars
    end

    datasetclass.meta = function(self)
      local i = 1
      local meta={}
      local dsid = sas.open(self.ds)
      for i = 1, sas.nvars(dsid) do
        meta[i]=table.tostring(sas.varinfo(dsid,i))
      end
      sas.close(dsid)
      return meta
    end
    
    -- new method (constructor)
    datasetclass.new = function(_ds)
      local obj = {}
      obj.ds = _ds
      setmetatable(obj,{__index=datasetclass})
      return obj
    end
    
  endsubmit;
run;
Out[5]:

155  ods listing close;ods html5 file=stdout options(bitmap_mode='inline') device=png; ods graphics on / outputfmt=png;
NOTE: Writing HTML5 Body file: STDOUT
156
157 proc lua;
158 submit
158! ;
159 -- prototype
160 datasetclass={}
161 datasetclass.nobs = function(self)
162 local dsid = sas.open(self.ds)
163 local nobs = sas.nobs(dsid)
164 sas.close(dsid)
165 return nobs
166 end
167
168 datasetclass.nvars = function(self)
169 local dsid = sas.open(self.ds)
170 local nvars = sas.nvars(dsid)
171 sas.close(dsid)
172 return nvars
173 end
174
175 datasetclass.meta = function(self)
176 local i = 1
177 local meta={}
178 local dsid = sas.open(self.ds)
179 for i = 1, sas.nvars(dsid) do
180 meta[i]=table.tostring(sas.varinfo(dsid,i))
181 end
182 sas.close(dsid)
183 return meta
184 end
185
186 -- new method (constructor)
187 datasetclass.new = function(_ds)
188 local obj = {}
189 obj.ds = _ds
190 setmetatable(obj,{__index=datasetclass})
191 return obj
192 end
193
194 endsubmit;
195 run;
NOTE: Resuming Lua state from previous PROC LUA invocation.
NOTE: PROCEDURE LUA used (Total process time):
real time 0.00 seconds
cpu time 0.01 seconds

196 ods html5 close;ods listing;

197

Sample Program (テスト実行)

In [4]:
proc lua;
  submit;
  
    -- main(1)
    local sampleds1=datasetclass.new("sashelp.class")
    print("nobs=",sampleds1:nobs())
    print("nvars=",sampleds1:nvars())
    for key, val in pairs(sampleds1:meta()) do
      print(key, val)
    end
    
    -- main(2)
    local sampleds2=datasetclass.new("sashelp.air")
    print("nobs=",sampleds2:nobs())
    print("nvars=",sampleds2:nvars())
    for key, val in pairs(sampleds2:meta()) do
      print(key, val)
    end
    
  endsubmit;
run;
Out[4]:

129  ods listing close;ods html5 file=stdout options(bitmap_mode='inline') device=png; ods graphics on / outputfmt=png;
NOTE: Writing HTML5 Body file: STDOUT
130
131 proc lua;
132 submit
132! ;
133
134 -- main(1)
135 local sampleds1=datasetclass.new("sashelp.class")
136 print("nobs=",sampleds1:nobs())
137 print("nvars=",sampleds1:nvars())
138 for key, val in pairs(sampleds1:meta()) do
139 print(key, val)
140 end
141
142 -- main(2)
143 local sampleds2=datasetclass.new("sashelp.air")
144 print("nobs=",sampleds2:nobs())
145 print("nvars=",sampleds2:nvars())
146 for key, val in pairs(sampleds2:meta()) do
147 print(key, val)
148 end
149
150 endsubmit;
151 run;
NOTE: Resuming Lua state from previous PROC LUA invocation.
nobs= 19
nvars= 5
1 table: 00007F7DAD700320=
{
["fmt_dec"]=0
["type"]="C"
["label"]=""
["fmt_width"]=0
["informat"]=""
["name"]="Name"
["infmt_dec"]=0
["format"]=""
["infmt_width"]=0
["length"]=8
}
2 table: 00007F7DAD718840=
{
["fmt_dec"]=0
["type"]="C"
["label"]=""
["fmt_width"]=0
["informat"]=""
["name"]="Sex"
["infmt_dec"]=0
["format"]=""
["infmt_width"]=0
["length"]=1
}
3 table: 00007F7DAD70BB20=
{
["fmt_dec"]=0
["type"]="N"
["label"]=""
["fmt_width"]=0
["informat"]=""
["name"]="Age"
["infmt_dec"]=0
["format"]=""
["infmt_width"]=0
["length"]=8
}
4 table: 00007F7DAD71D7A0=
{
["fmt_dec"]=0
["type"]="N"
["label"]=""
["fmt_width"]=0
["informat"]=""
["name"]="Height"
["infmt_dec"]=0
["format"]=""
["infmt_width"]=0
["length"]=8
}
5 table: 00007F7DAD70A900=
{
["fmt_dec"]=0
["type"]="N"
["label"]=""
["fmt_width"]=0
["informat"]=""
["name"]="Weight"
["infmt_dec"]=0
["format"]=""
["infmt_width"]=0
["length"]=8
}
nobs= 144
nvars= 2
1 table: 00007F7DAD715500=
{
["fmt_dec"]=0
["type"]="N"
["label"]=""
["fmt_width"]=0
["informat"]=""
["name"]="DATE"
["infmt_dec"]=0
["format"]="MONYY"
["infmt_width"]=0
["length"]=8
}
2 table: 00007F7DAD71B860=
{
["fmt_dec"]=0
["type"]="N"
["label"]="international airline travel (thousands)"
["fmt_width"]=0
["informat"]=""
["name"]="AIR"
["infmt_dec"]=0
["format"]=""
["infmt_width"]=0
["length"]=8
}
NOTE: PROCEDURE LUA used (Total process time):
real time 0.00 seconds
cpu time 0.00 seconds

152 ods html5 close;ods listing;

153