python 读取sas数据集_[求助]批量将文本文件中数据导入SAS数据集

代码如下:

===============================================================================================================

%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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值