%macro csv( data = _last_, /* SASデータセット名 */ file =, /* 出力ファイル名(参照名 or DOSファイル名) */ lrecl = 512, /* 出力レコードの最大長 */ n = yes, /* 1行目にデータと変数の個数を */ /* yes : 出力する */ /* no : 出力しない */ label = var, /* 2行目に変数名かラベルを出力 */ /* var : 変数名 */ /* label : ラベル */ /* no : 出力しない */ quote = no, /* 文字変数を複引用符で */ /* no : 囲まない */ /* yes : 囲む */ dlm = ',' /* デリミタ文字, TAB = '09'x */ ) ; /*-------------------------------------------------------*/ /* SAS dataset を csv 形式で出力する. */ /* */ /* note: */ /* 1. 変数の出力順は文字変数、次に数値変数の定義の順 */ /* 2. 出力フォーマットはSASデータセットの定義に従います */ /* 3. %csv の中で変数 _n_obs_と, _ を予約使用します */ /* */ /* author : 鈴木督久 */ /* */ /* 1995.10 Ver. 1.0 /初公開 */ /* 1996.04 Ver. 1.1 /オプション追加, n=, label=, quote=,dlm= */ /*-------------------------------------------------------*/ data _null_; file &file lrecl = &lrecl ; array tempc ( 2 ) $ 200 _temporary_ ; array tempn ( 1 ) _temporary_ ; %if &dlm = %str() %then %let dlm = ' ' ; %if %upcase( &dlm ) = TAB %then %let dlm = '09'x ; tempc( 2 ) = &dlm ; set &data nobs = _n_obs_ ; array chr ( * ) _character_ ; array num ( * ) _numeric_ ; tempn( 1 ) = sum( dim( chr ), dim( num ) ) ; %if %upcase( &n ) = YES %then %do ; if _n_ = 1 then put _n_obs_ tempn( 1 ) ; %end ; %if %upcase( &label ) = VAR %then %let f = vname ; %else %if %upcase( &label ) = LABEL %then %let f = label ; %else %goto nolabel ; if _n_ = 1 then do ; do _ = 1 to dim( chr ) ; call &f ( chr(_), tempc( 1 ) ) ; %if %upcase( "e ) = YES %then %do ; tempc(1) = quote( tempc(1) ) ; %end ; put tempc(1) + (-1) tempc(2) + (-1) @; end; do _ = 1 to dim( num ) ; call &f ( num(_), tempc( 1 ) ) ; %if %upcase( "e ) = YES %then %do ; tempc(1) = quote( tempc(1) ) ; %end ; put tempc(1) + (-1) tempc(2) + (-1) @; end; put + (-1) ' ' ; end ; %nolabel: do _ = 1 to dim( chr ) ; %if %upcase( "e ) = YES %then %do ; tempc(1) = quote( chr(_) ) ; put tempc(1) + (-1) tempc(2) + (-1) @ ; %end ; %else %do ; put chr(_) + (-1) tempc(2) + (-1) @ ; %end ; end; do _ = 1 to dim( num ) ; put num(_) + (-1) tempc(2) + (-1) @; end; put + (-1) ' ' ; run; %mend csv ;