---
title: CSSによるODS RTFのスタイル設定
tags:  #[第10回大阪sas勉強会]  
author: [森岡裕[SASユーザー総会世話人]](https://www.docswell.com/user/6484025)
site: [Docswell](https://www.docswell.com/)
thumbnail: https://bcdn.docswell.com/page/Y76Y93X1EV.jpg?width=480
description: [第10回大阪sas勉強会]
published: October 21, 24
canonical: https://www.docswell.com/s/6484025/KJ4X4J-2024-10-21-172825
---
# Page. 1

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

CSSによるODS RTFのスタイル設定
第10回 大阪SAS勉強会
中松 建


# Page. 2

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

目次
はじめに
基本的な使用方法
応用編
スタイル設定の再現
動的な罫線の設定方法
検証メモ
まとめ


# Page. 3

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

はじめに
Q. SASで出力のスタイル設定はどこで行うのがよい？
プロシージャ内
外部ファイル・設定
A. コーディングのベターなプラクティスは 外部ファイル・設定
→ ロジック・構造とスタイルの分離
html5でtableのrules属性の廃止など
コードや構造が短くシンプルになる
作業や責任の分担がしやすい
1


# Page. 4

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

SASでのスタイル設定
スタイルの外部設定は基本的に proc template
スタイル設定できますか？ 教えられますか?
設定する機会があまりない
設定方法に関する資料がそれほど多くない
他で役に立たないので、習得のモチベーションが低い
CSSは外部化の選択肢のひとつだが、日本語でまとまった資料はない
英語でも ODS RTFを対象にしたものはない
2


# Page. 5

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

CSS(Cascading Style Sheet)とは
スタイルを定義するためのルールベースの記法
Webページやアプリケーションの文字の色や大きさ、背景、配置の設定に使用される
SAS9より前、実用的には9.4から使用可能
SASに加えてweb関連を分かる人が少なく、あまり使用されていない
→ スタイル設定の標準的な技術であり、生成AIで扱いやすい
学習コストを抑えつつ、スクレイピングやxml関連など知識を他にも活かせる
3


# Page. 6

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

基本的な使用方法
* Check DOM(Document Object Model) ;
filename test temp ;
ods rtf file = test dom ;
proc report data = sashelp.class ;
run ;
ods rtf close ;
* Apply to RTF;
ods rtf file = &quot;xxx.rtf&quot; cssstyle = &quot;style.css&quot; ;
proc report data = sashelp.class ;
run ;
ods rtf close ;
ods trace dom でもOK
セルの値も出力されるので、確認時には適宜データセットを空にする
4


# Page. 7

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

DOMサンプル
&lt;table class=&quot;table&quot;&gt;
&lt;colgroup&gt;
&lt;col type=&quot;char&quot;&gt;
&lt;col type=&quot;char&quot;&gt;
&lt;col type=&quot;num&quot;&gt;
&lt;col type=&quot;num&quot;&gt;
&lt;col type=&quot;num&quot;&gt;
&lt;/colgroup&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th class=&quot;header&quot; index=&quot;1&quot;&gt;Name&lt;/th&gt;
&lt;th class=&quot;header&quot; index=&quot;2&quot;&gt;Sex&lt;/th&gt;
&lt;th class=&quot;header&quot; index=&quot;3&quot;&gt;Age&lt;/th&gt;
&lt;th class=&quot;header&quot; index=&quot;4&quot;&gt;Height&lt;/th&gt;
&lt;th class=&quot;header&quot; index=&quot;5&quot;&gt;Weight&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td class=&quot;data&quot; type=&quot;char&quot; unformatted-type=&quot;char&quot; index=&quot;1&quot; name=&quot;_c1_&quot; data-name=&quot;_c1_&quot;&gt;Alfred&lt;/td&gt;
&lt;td class=&quot;data&quot; type=&quot;char&quot; unformatted-type=&quot;char&quot; index=&quot;2&quot; name=&quot;_c2_&quot; data-name=&quot;_c2_&quot;&gt;M&lt;/td&gt;
&lt;td class=&quot;data&quot; type=&quot;num&quot; unformatted-type=&quot;num&quot; index=&quot;3&quot; name=&quot;_c3_&quot; data-name=&quot;_c3_&quot;&gt;
14&lt;/td&gt;
&lt;td class=&quot;data&quot; type=&quot;num&quot; unformatted-type=&quot;num&quot; index=&quot;4&quot; name=&quot;_c4_&quot; data-name=&quot;_c4_&quot;&gt;
69&lt;/td&gt;
&lt;td class=&quot;data&quot; type=&quot;num&quot; unformatted-type=&quot;num&quot; index=&quot;5&quot; name=&quot;_c5_&quot; data-name=&quot;_c5_&quot;&gt;
112.5&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;
5


# Page. 8

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

生成AIへの指示例
DOMにスタイルを設定するためのCSSを作成します。
tableの上と下に線
ヘッダーのセルには下線
ヘッダーの文字は太字
を、以下のDOMに対して行うためのCSSを作成してください。
スタイルの設定対象はクラスが設定されているエレメントで、クラスを含めて指定し
てください。
--{DOM}
6


# Page. 9

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

応用編
7


# Page. 10

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

スタイル設定の再現
ODS機能とREPORTプロシジャを用
いた解析帳票の作成(SAS ユーザー
総会2017)
のデータ・コードを使用し、設定例を再
現
8


# Page. 11

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

再現用SASコード
options nodate nonumber papersize = &quot;A4&quot; orientation = portrait topmargin = 3cm bottommargin = 3cm leftmargin = 2.5cm rightmargin= 2.5cm ;
ods escapechar = &#039;~&#039;;
title1 &#039;Table 9.9.9 Subgroup Analysis, MMRM for Percent Change from Baseline in VAS by Age Group&#039; ;
title2 &#039;Full Analysis Set&#039; ;
footnote1 &#039;Page ~{pageof}&#039; ;
ods rtf file = out notoc_data cssstyle = css dom ;
proc report data = sample.out missing split = &#039;|&#039; spanrows ;
column (&quot;&quot; AGEGR1N AGEGR1X TRTPN TRT_VISITN TRT_VISIT LSMEAN SE)
(&quot;95% CI~{super[a]}&quot; LOWER UPPER)
(&quot;Comparison to Placebo&quot; C_LSMEAN C_SE(&quot;CI~{super[a]}&quot; C_LOWER C_UPPER))
;
define AGEGR1N
/ order noprint ;
define AGEGR1X
/ order &#039;age&#039; ;
define TRTPN
/ order noprint ;
define TRT_VISITN / order noprint ;
define TRT_VISIT / &#039;Treatment|Visit&#039; ;
define LSMEAN
/ &#039;LS Mean&#039; ;
define SE
/ &#039;SE&#039; ;
define LOWER
/ &#039;Lower&#039; ;
define UPPER
/ &#039;Upper&#039; ;
define C_LSMEAN
/ &#039;LS Mean&#039; ;
define C_SE
/ &#039;SE&#039; ;
define C_LOWER
/ &#039;Lower&#039; ;
define C_UPPER
/ &#039;Upper&#039; ;
compute after _page_ ;
line &#039;[a] 95% confidence interval&#039; ;
endcomp ;
run ;
ods rtf close ;
9


# Page. 12

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

CSS例
:root{
font-size:8pt;
font-family: &#039;Courier&#039;;
}
.header{
vertical-align:bottom;
text-align: resolve(&#039;%head_align&#039;) ;
}
.header:not(:empty){border-bottom-width: 1pt;}
td.data:not(:empty){padding-left: resolve(&#039;%col_margin&#039;) ;}
.systemtitle, .systemtitle2,.systemtitle3,
.linecontent{text-align:left;}
.systemfooter{text-align:right;}
.linecontent:first-child{border-top-width:1pt;}
.table{
border-color:black;
border-style:solid;
border-top-width:1pt;
width: 100% ;
}
.data:empty, th.data{border-top-width: 1pt;}
td.data:not(:empty){border-top-width:resolve(&#039;%group_line&#039;);}
/*
macro sample for dynamic property
%macro col_margin ;
%if &quot;%scan(&amp;_val_, 1)&quot; = &quot;Active&quot; or &quot;%scan(&amp;_val_, 1)&quot; = &quot;Placebo&quot;
%then 0 ;
%else 0.4cm ;
%mend ;
*/
.table.header, .table.data{
padding: 0 ;
margin: 0 ;
}
.data{vertical-align:top;}
10


# Page. 13

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

動的な罫線の設定方法
モダンCSS :has()など
後続兄弟結合子 th.data ~ td.data
動的プロパティで_row_の参照
セルのルール + 動的プロパティで_val_の参照: style.css
マクロでCSSの:nth-child()を生成: css_macro.sas
従来どおり call define
11


# Page. 14

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

検証メモ
cssclassによるクラス追加はrtfでは使用できない(htmlでもログのdomには出て
こないことに注意)
CSSは独自の規格のようで、いわゆるモダンCSSと呼ばれる機能や非表示化や優先
設定なども使用できない
擬似クラス(Pseudo-Class)は有用だが、rtfでは後続兄弟結合子「~」など一部うま
く機能しないものもある
→ おそらくrtfで存在する要素にはクラスが指定されている、その要素にしかスタイ
ルの指定は効かない
SASのバージョンによりDOMが変わる場合があるので注意
12


# Page. 15

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

noprintの列は擬似クラスでの参照はできない
noprintの代わりにCSS側での非表示化も不可
:before によるフォント情報などの埋め込みもおそらく不可
first-xxはあるがlast-xxは使用できない、nth-childは下層すべてが対象になる
が、first-xxでは前方の一部しか対象にならない
表全体の下線はフットノートの形式にもよるが、例のような形式で罫線にnth-child
を使わずフットノートなしの場合、空のlineでの対応が簡単
プロパティでのマクロ変数やマクロの使用で、セレクタが不自由な点はいくらかカバ
ー可能
動的プロパティでは _col_ や _row_ はうまく動作せず、確認できたのは _val_ の
み
proc reportで特定の複数列にタイトルをつけるような場合、cssで _val_ の値を
扱う場合 (&quot;&quot; col1 col2) のように、空でもタイトルの指定が必要
13


# Page. 16

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

まとめ
テーブル全体の設定であれば、十分対応可能
列・行への設定は番号を指定する形で可能
罫線など動的な設定は、CSSの生成や動的プロパティが必要
リファレンス：ODS and Cascading Style Sheets
使用可能なCSSの機能は限定的ではあるものの
生成AIにより学習コストが低い(高精度な回答が得られる)
プロシージャ内指定からの次のステップ
proc templateに関して詳しい人がいない
既存の資産や参照プログラムがない
というような場合には有用では？
14


# Page. 17

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

配布先
https://github.com/k-nkmt/sas_study_group/tree/main/Osaka_10th
├─prg
│ │ css_macro.sas : CSS関連マクロ
│ │ css_report.sas : レポート用コード
│ │ style.css : サンプルCSS
│ │
│ ├─data : テスト用データ
│ └─report: サンプル出力
├─.vscode : marp用設定
└─slide
theme.css : スライドのカスタムテーマ
slide.md : スライドのソース
15


# Page. 18

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

その他資料など
スライドはMarpとカスタムテーマにより作成、CSSのサンプルとして同梱
SAS単体でUUID(バージョン3,5)を生成する処理
バイナリ関連関数によりUUIDバージョン3, 5を生成
Intro to SAS Notes 日本語訳
University of Florida College of Public Health and Health
Professionsが公開しているSASのテキストの日本語訳
Githubアカウント: https://github.com/k-nkmt
e-mail: ken-nakamatsu@knworx.com
16


