2
6

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

SASテクニック ファイルの読み込み excel csv

Last updated at Posted at 2020-06-18

ファイルの読み込み

sasはexcelやcsvなどといったデータを読み込める
sasデータセットならlibnameするだけでokだが、
テキストファイルやexcelファイルはimportプロシジャやdataステップで読み込める
excelはimportやlibname使えるが、ライセンス等が必要
なければDDEで読み込むか、excelをあらかじめcsvへ変換しておいて読み込む

importプロシジャ

proc Import dbms = xlsxやcsv replace
  out = 出力データセット名
  datafile = "パス\ファイル名.拡張子" ;
  range = "シート名$範囲" ; %* excelなら *;
run;

getnamesとかオプション等いろいろあるが基本はこれ
範囲はA1:C99など
excelの拡張子がxlsとかならdbms = excelかexcelcsじゃないと動かなかった気がする...
※SAS/ACCESSのライセンスがないとexcelをimportできない
読み込みたいexcelファイルを他の人が開いているときにimportプロシジャ使うとエラーとなるので、サーバーなどでファイルを共有している場合には注意

dataステップ(csv,txtなどテキストファイルの読み込み)

とりあえずいつもコピってるやつ

data 出力データセット名 ;
  length VAR1 - VAR200 $10000;
  infile "パス\ファイル名.拡張子" dsd missover lrecl = 30000 firstobs = 1;
  input VAR1 - VAR200;
run;

列名がファイルの1行目にあるならfirstobs = 2とすればいい
importと違って、変数定義が細かくできるからこっちのほうがいいかも
importの方が記述は楽だけどね

DDE(excel読み込み)

これもいつもプログラムをコピってる

%* excelを開く(元々開いていて、読み取り専用になってもok) *;
options noxwait noxsync;
%sysexec "パス\ファイル名.xlsx" ;

filename EXL_DDE dde "Excel|[ファイル名.xlsx]シート名!範囲";
data 出力データセット名 ;
  length VAR1 - VAR200 $10000;
  infile EXL_DDE notab dlm = "09"x dsd missover lrecl = 30000 firstobs = 1;
  input VAR1 - VAR200;
run;

%* excel閉じる *;
filename AAA dde "excel|system" ;
data _NULL_;
  file AAA;
  put "[close()]" ;
run;
filename EXL_DDE clear;
filename AAA clear; 

https://www.sas.com/offices/asiapacific/japan/service/technical/faq/list/body/ba196.html
https://support.sas.com/documentation/cdl_alternate/ja/lestmtsref/68024/HTML/default/n1rill4udj0tfun1fvce3j401plo.htm
https://www.sas.com/offices/asiapacific/japan/service/technical/faq/list/body/pc074.html

2
6
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
6

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?