---
title: How to Read Configuration Files with SAS
tags:  #[第5回大阪sas勉強会]  
author: [森岡裕[SASユーザー総会世話人]](https://www.docswell.com/user/6484025)
site: [Docswell](https://www.docswell.com/)
thumbnail: https://bcdn.docswell.com/page/8EDY3LWWEG.jpg?width=480
description: [第5回大阪SAS勉強会]小川良太
published: April 05, 24
canonical: https://www.docswell.com/s/6484025/KW12RR-2024-04-05-124410
---
# Page. 1

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

How to Read Configuration Files with SAS
24-MAY-2019
Ryota Ogawa
1


# Page. 2

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

⾃⼰紹介
⼩川良太です
神⼾のTRIというところでデータマネージャーをしてい
ます
主なお仕事：EDC、データクリーニング、SAS、
CDISC、Spot re、RPAなど
趣味：アニメ、ラノベ、電⼦マネー、ワークアウト
2


# Page. 3

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

みなさん
想像してみてください
3.1


# Page. 4

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

あなたはSASプログラマーです
3.2


# Page. 5

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

あなたはSASプログラマーです
職場の上司から汎⽤的に利⽤できるSASプログラムを開
発するよう指⽰がありました
3.2


# Page. 6

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

あなたはSASプログラマーです
職場の上司から汎⽤的に利⽤できるSASプログラムを開
発するよう指⽰がありました
そのプログラムは全てのプロジェクトで使⽤するため、
プロジェクトごとに設定が必要です
3.2


# Page. 7

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

あなたはSASプログラマーです
職場の上司から汎⽤的に利⽤できるSASプログラムを開
発するよう指⽰がありました
そのプログラムは全てのプロジェクトで使⽤するため、
プロジェクトごとに設定が必要です
開発は社内のCSVポリシーにしたがい検証を⾏うため、
本番移⾏後のソースコード修正は変更対応となりとても
めんどうです
3.2


# Page. 8

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

あなたはSASプログラマーです
職場の上司から汎⽤的に利⽤できるSASプログラムを開
発するよう指⽰がありました
そのプログラムは全てのプロジェクトで使⽤するため、
プロジェクトごとに設定が必要です
開発は社内のCSVポリシーにしたがい検証を⾏うため、
本番移⾏後のソースコード修正は変更対応となりとても
めんどうです
そこで設定はソースにハードコードしないで、設定ファ
イルとして外出しすることにしました
3.2


# Page. 9

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

ここで問題です
4.1


# Page. 10

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

設定ファイルにはどんなフォーマッ
トを使⽤しますか？
4.2


# Page. 11

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

XML？
example.xml（適当）
1 &lt;breakfast_menu&gt;
2
&lt;food&gt;
3
&lt;name&gt;Belgian Waffles&lt;/name&gt;
4
&lt;price&gt;$5.95&lt;/price&gt;
5
&lt;description&gt;Two of our famous Belgian Waffles with plenty of real maple syrup&lt;/
6
&lt;calories&gt;650&lt;/calories&gt;
7
&lt;/food&gt;
8
&lt;food&gt;
9
&lt;name&gt;Strawberry Belgian Waffles&lt;/name&gt;
10
&lt;price&gt;$7.95&lt;/price&gt;
11
&lt;description&gt;Light Belgian waffles covered with strawberries and whipped cream&lt;/
12
&lt;calories&gt;900&lt;/calories&gt;
13
&lt;/food&gt;
14 &lt;/breakfast_menu&gt;
4.3


# Page. 12

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

JSON？
example.json（適当）
1 {
2
&quot;menu&quot;: {
3
&quot;id&quot;: &quot;file&quot;,
4
&quot;value&quot;: &quot;File&quot;,
5
&quot;popup&quot;: {
6
&quot;menuitem&quot;: [
7
{&quot;value&quot;: &quot;New&quot;, &quot;onclick&quot;: &quot;CreateNewDoc()&quot;},
8
{&quot;value&quot;: &quot;Open&quot;, &quot;onclick&quot;: &quot;OpenDoc()&quot;},
9
{&quot;value&quot;: &quot;Close&quot;, &quot;onclick&quot;: &quot;CloseDoc()&quot;}
10
]
11
}
12
}
13 }
4.4


# Page. 13

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

YAML？
example.yml（適当）
1 - martin:
2
name: Martin D&#039;vloper
3
job: Developer
4
skills:
5
- python
6
- perl
7
- pascal
8 - tabitha:
9
name: Tabitha Bitumen
10
job: Developer
11
skills:
12
- lisp
13
- fortran
14
- erlang
4.5


# Page. 14

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

プロパティファイル？
example.properties（適当）
1
2
3
4
site=www.google.com
name=DefaultAppName
topic=Properties
category=core-java
4.6


# Page. 15

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

違う！！
4.7


# Page. 16

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

Windowsなら〜
4.8


# Page. 17

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

Windowsなら〜
INIファイルでしょ！
4.8


# Page. 18

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

本⽇のお題
INIファイルをSASで読み込ん
でみる
5


# Page. 19

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

INIファイルとは？
Windowsで使⽤されるテキストベースの設定ファイル
パラメータは「キー」と「値」を持ち、等号で区切っ
て定義する
パラメータはセクションでグループ分けすることがで
きる
コメントの開始⽂字にはセミコロンを使⽤する
6.1


# Page. 20

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

こんなファイルです（適当）
example.ini
1
2
3
4
5
6
7
8
username = noha
password = plain_text
salt = NaCl
[server_1]
interface=eth0
ip=127.0.0.1
document_root=/var/www/example.org
6.2


# Page. 21

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

INIファイルをSASで読み込ん
でみる
7.1


# Page. 22

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

INIファイルを準備します
Setting.ini
7.2


# Page. 23

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

Windows APIを呼び出すためのカタログファイルを作成します
1
2
3
4
5
6
7
8
9
10
11
12
13
14
filename SASCBTBL catalog &quot;WORK.WIN32API.FUNCTION.SOURCE&quot;;
data _null_;
input;
file SASCBTBL;
put _infile_;
datalines4;
routine GetPrivateProfileStringA
module
= KERNEL32
minarg
= 6
maxarg
= 6
stackpop = called
returns = short
;
7.3


# Page. 24

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

Windows APIを呼び出すためのカタログファイルを作成します
（続き）
1
2
3
4
5
6
7
8
9
10
11
12
13
14
arg 1
arg 2
arg 3
arg 4
arg 5
arg 6
;;;;
run;
input
input
input
output
input
input
char
char
char
char
num
char
format
format
format
format
format
format
=
=
=
=
=
=
$cstr200.;
$cstr200.;
$cstr200.;
$cstr200.;
pib4.;
$cstr200.;
*lpAppName;
*lpKeyName;
*lpDefault;
*lpReturnedString;
*nSize;
*lpFileName;
libname OUT &quot;C:\output&quot;;
proc copy in = WORK out = OUT memtype = catalog;
select WIN32API;
run;
7.4


# Page. 25

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

MODULEN関数を使⽤して、Windows APIのGetPrivateProfileStringを呼び出
すマクロを定義
1 libname LIB &quot;C:\output&quot; access = readonly;
2
3 filename SASCBTBL catalog &quot;LIB.WIN32API.FUNCTION.SOURCE&quot;;
4
5 %macro ReadFromIniFile(AppName =, KeyName =, FileName =);
6
data TMP;
7
attrib SECTION length = $20;
8
attrib KEY
length = $20;
9
attrib VALUE
length = $200;
10
SECTION = &quot;&amp;AppName.&quot;;
11
KEY
= &quot;&amp;KeyName.&quot;;
12
VALUE
= &quot;&quot;;
13
rc = modulen (&quot;GetPrivateProfileStringA&quot;, SECTION, KEY, &quot;&quot;, VALUE,
14
200, &quot;&amp;FileName.&quot;);
15
run;
7.5


# Page. 26

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

MODULEN関数を使⽤して、Windows APIのGetPrivateProfileStringを呼び出
すマクロを定義（続き）
1
proc datasets nolist;
2
append base = INI data = TMP;
3
delete TMP(memtype = data);
4
run;
5
quit;
6 %mend;
7.6


# Page. 27

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

INIファイルの値をデータセット化して、マクロ変数に⼊
れます
1
2
3
4
5
6
7
8
9
10
%let iniFilePath = C:\output\Setting.ini;
%ReadFromIniFile(FileName = &amp;iniFilePath.,
%ReadFromIniFile(FileName = &amp;iniFilePath.,
%ReadFromIniFile(FileName = &amp;iniFilePath.,
%ReadFromIniFile(FileName = &amp;iniFilePath.,
AppName
AppName
AppName
AppName
=
=
=
=
Setting,
Setting,
Setting,
Setting,
KeyName
KeyName
KeyName
KeyName
=
=
=
=
StudyId);
InputPath);
OutputPath);
Debug);
data _null_;
set INI;
call symputx(KEY, VALUE, &quot;G&quot;);
run;
7.7


# Page. 28

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

個⼈的なユースケース
8.1


# Page. 29

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

試験名の定義
1 data AE;
2
set AE;
3
attrib STUDYID length = $10;
4
STUDYID = symget(&quot;StudyId&quot;);
5 run;
8.2


# Page. 30

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

フォルダパスの設定
1 libname IN v9 &quot;&amp;InputPath.&quot; access = readonly;
2 libname OUT v9 &quot;&amp;OutputPath.&quot; access = readonly;
8.3


# Page. 31

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

デバッグモードの切り替え
1 data _null_;
2
/*
*/
3
if
symget(&quot;Debug&quot;)
4
call execute(&quot;options
5
end;
6
/*
*/
7
else if symget(&quot;Debug&quot;)
8
call execute(&quot;options
9
end;
10 run;
リリースモード
デバッグモード
= &quot;0&quot; then do;
nomprint nomprintnest nomlogic nomlogicnest nosymbolgen;&quot;)
= &quot;1&quot; then do;
mprint mprintnest mlogic mlogicnest symbolgen;&quot;);
本当はDOSUBLでしたかったけど、
システムオプションは動かなかった・・・
8.4


# Page. 32

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

補⾜
9.1


# Page. 33

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

MODULEN関数
外部ルーチンを呼び出し、数値を返します
構⽂
MODULEN(&lt; control&gt;, module, argument-1, argument-2 ..., argumentn)
引数
control
コントロール⽂字列です(省略可能)
module
使⽤する外部モジュールの名前を指定します
argument-1, argument-2, ..., argument-n
要求されたルーチンに渡す引数を指定します
9.2


# Page. 34

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

GetPrivateProfileString関数
INIファイル内の指定されたセクションから⽂字列を取得します
構⽂
DWORD GetPrivatePro leString(
LPCTSTR lpAppName,
LPCTSTR lpKeyName,
LPCTSTR lpDefault,
LPTSTR lpReturnedString,
DWORD nSize,
LPCTSTR lpFileName
);
引数
lpAppName
キー名を含むセクションの名前
lpKeyName
関連付けられている⽂字列を取得するキーの名前
lpDefault
デフォルトの⽂字列
lpReturnedString
取得した⽂字列を受け取るバッファへのポインタ
nSize
lpReturnedStringパラメーターが指すバッファーのサイズ（⽂字数）
lpFileName
INIファイルの名前
9.3


# Page. 35

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

おしまい
10


