---
title: Classic Generative Program
tags:  #[第10回大阪sas勉強会]  
author: [森岡裕[SASユーザー総会世話人]](https://www.docswell.com/user/6484025)
site: [Docswell](https://www.docswell.com/)
thumbnail: https://bcdn.docswell.com/page/YJ6Y934D7V.jpg?width=480
description: [第10回大阪sas勉強会]
published: October 21, 24
canonical: https://www.docswell.com/s/6484025/ZP242L-2024-10-21-161038
---
# Page. 1

![Page Image](https://bcdn.docswell.com/page/YJ6Y934D7V.jpg)

Classic Generative Program
2024-10-18
三木悠吾


# Page. 2

![Page Image](https://bcdn.docswell.com/page/GJ5GNLQ8J4.jpg)

三木 悠吾 (Yugo Miki)
所属：グラクソ・スミスクライン株式会社
有限会社三栄
ミハルラボ（仮）
GSKで働いていることが多いです。他はたまに。
今回の発表はGSKとして発表しているわけではありません。
【職務経験】
・研究系（NC-PK, IoT, Big Data, Sensing）
・開発系（NC-PK、DM、PG、SYS）
・社内SEなど
【最近やったゲーム】
・風来のシレン6
・崩壊3rd / 原神


# Page. 3

![Page Image](https://bcdn.docswell.com/page/LE3R2LVG75.jpg)

ことのおこり
三木がSASに初めて触れて1wほど、SAS社の研修を終えたころのこと。
S木「三木さん、これからこの会社のSASプログラムを作っていってもらう
ことになるんだけど、マクロパラメーターを大量に使うようなプログラム
を作成するのは禁止だよ。」
三木「はぁ。別にそれでいいですけど・・・。」
S木「・・・なぜかというと・・・・」
三木「・・・なるほど。だとすると、・・・・。」
というような経緯の結果、Generativeなプログラムが開発された。


# Page. 4

![Page Image](https://bcdn.docswell.com/page/8EDYZN8NEG.jpg)

「Generative」とは？
Chat GPTに聞いてみた。
「Generative」 = 「生成する」「生み出す」
 「Generative AI」とは、新しい情報やコンテンツを自動的に生成・
創出する能力を持つAI技術であり、多様な分野で革新的な応用が可能
です。
Chat GPT o1-miniの回答から一部省略


# Page. 5

![Page Image](https://bcdn.docswell.com/page/V7P6D58NJ8.jpg)

SASの「Generative」機能とは？
★Call execute
データステップ内で呼び出せる。
Executeの引数をデータステップ後に実行する。
★put statement
データステップで使えるfunction。
データの型変換だけでなく、外部ファイルへ出力することが可能。


# Page. 6

![Page Image](https://bcdn.docswell.com/page/2JV31PNYEQ.jpg)

Call execute 1
example1
1.
Call Executeの引数を現在のデータス
テップ終了直後に実行する。
2.
複数行でcallを書くと複数行生成される。
3.
各callの実行回数はコールされた回数＝
setされたデータのobs数だけ実行される。
data _null_;
call execute(“data _null_; put ‘Hello Generative
SAS!’; run; ”);
run;
NOTE: CALL EXECUTEが生成したラインです。
1 + data _null_; put &#039;Hello Generative SAS!&#039;; run;
Hello Generative SAS!
Example2
data _null_;
call execute(&quot;data _null_;&quot;);
call execute(&quot; put &#039;Hello Generative SAS!&#039;;&quot;);
call execute(&quot;run;&quot;);
run;
NOTE: CALL EXECUTEが生成したラインです。
1 + data _null_;
2 + put &#039;Hello Generative SAS!&#039;;
3 + run;
Hello Generative SAS!


# Page. 7

![Page Image](https://bcdn.docswell.com/page/5EG32PKW7L.jpg)

Call execute 2
Call executeはデータセット内で呼び出すこ
とで機能するため、エクセルなどの外部仕様
を読み込んでprogramをキックできる。
1.
上記のようなedit checkの仕様に対して、
チェックコードを生成して実行する。
Example3
data _null_;
set plan;
call execute(&#039;data _null_;&#039;);
call execute(&#039; set sashelp.&#039;||cats(dsn)||&#039;;&#039;);
call execute(&#039; where &#039; || cats(cond) ||&#039;;&#039;);
call execute(&#039; length msg $200.;&#039;);
call execute(&#039; msg = cat(&quot;ERR&quot;,&quot;OR:
#&quot;,&#039;||cats(id)||&#039;,&quot; Condition:&quot;,
cats(&quot;&#039;||cats(dsn,&quot;-&quot;,cond)||&#039;&quot;));&#039;);
call execute(&#039; put msg ;&#039;);
call execute(&#039;run;&#039;);
run;


# Page. 8

![Page Image](https://bcdn.docswell.com/page/4JQ5P2NQ7P.jpg)

Call execute 3
1. Executeがそれぞれのコードを作成し、実
行してくれている。チェックもかかってい
るよう。
NOTE: CALL EXECUTEが生成したラインです。
1 + data _null_;
2 + set sashelp.class;
3 + where age&gt;=12;
4 + length msg $200.;
5 + msg = cat(&quot;ERR&quot;,&quot;OR: #&quot;,1,&quot; Condition:&quot;,
cats(&quot;class-age&gt;=12&quot;));
6 + put msg ;
7 + run;
ERROR: #1 Condition:class-age&gt;=12
8 + data _null_;
9 + set sashelp.cars;
10 + where horsepower&gt;280;
11 + length msg $200.;
12 + msg = cat(&quot;ERR&quot;,&quot;OR: #&quot;,2,&quot; Condition:&quot;,
cats(&quot;cars-horsepower&gt;280&quot;));
13 + put msg ;
14 + run;
ERROR: #2 Condition:cars-horsepower&gt;280


# Page. 9

![Page Image](https://bcdn.docswell.com/page/K749YLGYE1.jpg)

Call execute 4
マクロ変数とは相性が悪い。というか直感的
に組み合わせにくいので、慣れるまではマク
ロ変数禁止！
実行される順番
1. マクロのdecode
2. Decodeされた状態でプログラムの実行
3. Executeで生成されたコードの実行
マクロ変数でいろいろ制御しようとすると苦
しむことになる。どうしても必要なら、
symput(x)とsymgetで対応すべし。
example
%macro work(id);
data _null_;
set file&amp;id.;
where filename contains “program”;
call execute(cats(‘%incl “’,filename,’”;’));
run;
%mend;
%work(1)


# Page. 10

![Page Image](https://bcdn.docswell.com/page/LJ1K6LDN7G.jpg)

Call execute 5
おすすめの使い方。
example
Sashelp.vmemberなどのメタデータとは相
性がいい。Libnameから見ることのできるす
べてのファイルに対して、特定の処理をかけ
たりするがよい。Dopenなどで取得してもい
いが、このあたりの関数はあまり直感的でな
く、仕様探して取り扱うよりはexecute +
metadataで回した方が格段に安易。
%macro work(dsn);
libname sdtmout xport “/libname/&amp;dsn..xpt”;
proc copy data=sdtm.&amp;dsn out=sdtmout;
select &amp;dsn.;
run;
%mend;
data _null_;
set sashelp.vmember;
where libname = “SDTM” and memtype = “DATASET”;
call execute(cats(‘%work(’, memname, ‘);’));
run;


# Page. 11

![Page Image](https://bcdn.docswell.com/page/GJWDWV8M72.jpg)

Put statement 1
Put statementの使い方
example
filename ext “/root/test.txt”;
①データ型変換など
②ログや外部ファイルへの出力
今回は②の使い方。
filenameで外部ファイルを定義し、そこへ
putで出力する。
data _null_;
file ext;
put “Hello World!”;
run;


# Page. 12

![Page Image](https://bcdn.docswell.com/page/4EZG548MJ3.jpg)

Put statement 2
ユーザー定義error, warningの使い方
Exampleのようにput で” ERROR :”と記載す
るとlog上にERROR, WARNINGが出力される。
★Tips
“ERR” “OR:”と書くと社内のlog check
で”ERROR”を抽出している場合、すり抜ける
ことが出来る。
２行目は”-” と記載すると行の頭のERRORや
WARNINGが消えて見やすくなる。
example
data _null_;
set sashelp.class;
if age &gt;= 16 then do;
put &quot;ERR&quot; &quot;OR: Age &gt;= 16 detected!&quot;;
put &quot;ERR&quot; &quot;OR- &quot; Name= &quot;, &quot; Sex= &quot;, &quot; Age= ;
end;
if age &lt; 12 then do;
put &quot;WAR&quot; &quot;NING: Age &lt; 12 detected!&quot;;
put &quot;WAR&quot; &quot;NING- &quot; Name= &quot;, &quot; Sex= &quot;, &quot; Age= ;
end;
run;


# Page. 13

![Page Image](https://bcdn.docswell.com/page/Y76Y93P5EV.jpg)

Combination
example
簡単！準自動化！！
1. Metadataから帳票情報読み込む。
2. 出力内容はマクロでひとまとめ
3. executeからkick→プログラムが出来てい
る。
4. プログラムは各プログラマーが手直しして
実行→完！
data plan;
length tnum t1 t2 $40.;
tnum = &quot;14.3.1&quot;; t1 = &quot;Overview of AEs&quot;; t2 = &quot;Safety&quot;;
output;
tnum = &quot;14.3.2&quot;; t1 = &quot;Summary of AEs by SOC and PT&quot;; t2
= &quot;Safety&quot;; output;
run;
%macro create_pg(id, tnum, t1, t2);
filename ext &quot;/root/test&amp;id..sas&quot;;
data _null_;
file ext;
put &quot;title1 &amp;tnum. ;&quot;;
put &quot;title2 &amp;t1. ;&quot;;
put &quot;title3 &amp;t2. ;&quot;;
run;
%mend;
data _null_;
set plan;
n = _n_;
call execute(cats(‘%create_pg(&#039;, n, &#039;,&#039;, tnum, &#039;,&#039;, t1,
&#039;,&#039;, t2, &#039;)&#039;));
run;


# Page. 14

![Page Image](https://bcdn.docswell.com/page/G75GNLKG74.jpg)

まとめ
Metadata、Program、結果（デー
タや統計量など）というフローは
Metadata
Call execute、put statementで実
装することが可能。
★What is Your Metadata？
解析仕様書、mockup、
Define.xml、QC記録、sashelpの
metadata・・・
Programs
Results


# Page. 15

![Page Image](https://bcdn.docswell.com/page/9J2Y5LWDJR.jpg)

Reference
 SAS Macro Language Magic: Discovering Advanced Techniques


# Page. 16

![Page Image](https://bcdn.docswell.com/page/DEYGKQLMEM.jpg)

End
 The real voyage of discovery consists not in seeking new landscapes,
but in having new eyes.
―― Marcel Proust


