本文主要是介绍xpt2sas(.xpt文件批量转为.sas7dbat),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
.xpt文件批量转为.sas7dbat
- 前言
- 一、文档目录举例
- 二、方法一(SAS EG 8.3环境下)
- 三、方法二(SAS9.4环境下)
前言
提供了两种不同编码环境下xport转sas的宏脚本xpt2sas,新建一个名为raw的文件夹,再内嵌一个名为xpt的文件夹,将需要转换的
xpt文件放置在xpt目录下,运行代码后,生成的.sas7dbat文件将批量出现在raw目录下
一、文档目录举例
示例:在如下文档目录前提下编码
二、方法一(SAS EG 8.3环境下)
只需要替换url中的file其他代码不用动
%macro xpt2sas;
%let url=%str(\\file\raw); \*file需要更换为指定目录如:\\10.10.1.95\filesever\project\task*\
libname raw "&url.";data _import;
length file line $200;
/* Define a filename pointing to the directory */
rc = filename("myDir", "&url.\xpt");
/* Open the directory, get directory id number */
dirId = dopen("myDir");
if dirId > 0 then do;/* dnum() : nb of files in directory */do i = 1 to dnum(dirId);/* Read the ith file name from directory */file = dread(dirId, i);line =cats("%","xpt2loc","(libref=raw,filespec='","&url.","\xpt\",file,"');");if length(file) > 0 then output;end;/* Close the open directory */rc = dclose(dirId);end;
drop rc dirId i file;
run;
data _null_;set _import end=eof;file "&url.\xpt\xpt2loc.sas" dlm="" lrecl=32000;put line;
run;
%include "&url.\xpt\xpt2loc.sas";
%mend;
%xpt2sas;
三、方法二(SAS9.4环境下)
%macro xpt2sas;
data _null_;call system("cd \file\raw\xpt"); \*file需要更换为指定目录如:D:\filesever\project\task*\
run;
filename _dir pipe 'ls -la *.xpt' ; /*linux:ls,window:dir*/data _import(keep=line);length _dirline $200;length _file $20;length line $200.;infile _dir recfm=v lrecl=200 truncover ;input _dirline $1-200 ; _file = scan(_dirline, 9, " ");line = '%xpt2loc(libref=raw ,filespec='||"'"||'\file\raw/xpt\'||strip(_file)||"'"||');';output;
run;filename outfile "\file\raw\xpt2lo.sas";data _null_;set _import end=eof;file outfile dlm="" lrecl=32000;put line;
run;
x "perl -i -pe 's/\n/\r\n/g' \file\raw\xpt2loc.sas";filename outfile clear;%mend;
%xpt2sas;libname raw '/file/raw/';
%include '/file/raw/xpt2loc.sas';
这篇关于xpt2sas(.xpt文件批量转为.sas7dbat)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!