概述
代码如下:
===============================================================================================================
%let g_outfile = "E:OutFile.csv" ;
%macro CreateTable;
data record( keep = fieldtype startline endline ) field( keep = field1-field6 fieldtype ) ;
%local EndBlock;
%let EndBlock = _%substr(%sysfunc(ranuni(0),9.7),3);
retain startfield startcolumn startline ;
retain readstart 0 ;
retain readfirst 0 ;
retain readfield 0 ;
retain readstat 0 ;
retain startline 0 ;
retain endline 2 ;
retain curline 0 ;
retain fileline 1 ;
infile &g_outfile _infile_= memfile END = endoffile missover dlm = ',' ;
input (field1-field6) ($) ;
fileline+1 ;
curline+1 ;
if endoffile = 1 then
do ;
endline = curline ;
output record ;
goto &EndBlock ;
end ;
if readstart = 0 then
do ;
readstart = 1 ;
readstat = 1 ;
readfirst = 1 ;
readfield = 0 ;
fieldtype+1 ;
startline = fileline ;
output field ;
goto &EndBlock ;
end ;
len = length( memfile )-1 ;
if len = 0 then
do ;
readfield = 1 ;
readstat = 0 ;
endline = curline-1 ;
output record ;
if readfirst eq 1 then readfirst = 0 ;
startline = fileline ;
startline+1 ;
goto &EndBlock ;
end ;
if readfield = 1 then
do ;
readfield = 0 ;
readstat = 1 ;
fieldtype+1 ;
output field ;
goto &EndBlock ;
end ;
if readstat = 1 then goto &EndBlock ;
&EndBlock.:
run ;
proc sort data = record ;
by fieldtype ;
run ;
proc sort data = field ;
by fieldtype ;
run ;
data mergetable ;
merge field record ;
by fieldtype ;
run ;
data _null_ ;
set mergetable ;
call symput( "recordcount" , _n_ ) ;
run ;
%macro appendfield ;
%do i = 1 %to &recordcount ;
%global v&i ;
data _null_ ;
set mergetable ;
if _n_ = &i ;
%do j = 1 %to 6 ;
call symput( "v&j" , field&j ) ;
%end ;
put startline = ;
put recordcnt = ;
call symput( "linestart" , startline ) ;
call symput( "lineend" , endline ) ;
run ;
data Table&i ;
infile &g_outfile firstobs = &linestart obs = &lineend missover dsd ;
input (&v1 &v2 &v3 &v4 &v5 &v6) ($) ;
run ;
data Table&i.(drop = count);
set Table&i ;
COUNTCNT = int( count ) ;
run ;
%end ;
%mend appendfield ;
%appendfield
%mend CreateTable ;
%CreateTable ;
===============================================================================================================
遗留问题:
1、整型变量名要求全部相同(上代码中是COUNT),对变量名不同的问题目前还没有想到好的办法。还请各位大侠看是否有好的点子一同分享;
2、变量个数最大值要求在一定范围内(这个问题应该不大);
xmok77
金钱 +100
奖励 2009-3-7 11:07:41
最后
以上就是专注台灯为你收集整理的python 读取sas数据集_[求助]批量将文本文件中数据导入SAS数据集的全部内容,希望文章能够帮你解决python 读取sas数据集_[求助]批量将文本文件中数据导入SAS数据集所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
发表评论 取消回复