简体   繁体   English

sas导入文件夹中的最新csv文件

[英]sas import latest csv file in folder

I have a folder that that has a file added to it each day as below 我有一个文件夹,每天都有一个文件添加到它,如下所示

Z:\\old\\stock110813.csv Z:\\old\\stock120813.csv Z:\\old\\stock130813.csv Z:\\old\\stock140813.csv Z:\\ old \\ stock110813.csv Z:\\ old \\ stock120813.csv Z:\\ old \\ stock130813.csv Z:\\ old \\ stock140813.csv

  1. I would like to import the latest file into SAS dynamically ie. 我想动态地将最新文件导入SAS,即。 searches the folder for the latest date 在文件夹中搜索最新日期
  2. or would like sas to make a copy of the latest and change the name & location of the file 或者希望sas制作最新的副本并更改文件的名称和位置

I have been searching the web for days testing little bits of code but am struggling therefore any help would be appreciated. 我一直在网上搜索几天测试一些代码,但我正在努力,因此任何帮助将不胜感激。

cheers John 欢呼约翰

If the date is predictable (ie, today's date) then you can do: 如果日期是可预测的(即今天的日期),那么您可以:

%let date=%sysfunc(today,dateformat.); *whatever dateformat you need;
proc import file="z:\old\stock&date..csv"... ;
run;

If not, then your best bet is to use a pipe with the directory listing to see the most recent filename. 如果没有,那么最好的办法是使用目录列表中的管道来查看最新的文件名。 Something like this (directory logic depends on your server/OS/etc.) 像这样的东西(目录逻辑取决于你的服务器/操作系统/等)。

filename mydir pipe 'dir /b /od /a-d z:\old\';

data myfiles;
infile mydir;
input @1 filename $32.;
call symput('filename',filename);
run;

proc import file="z:\old\&filename." ... ;

Do you want to use the system date or the date in the filename to determine what's the newest file? 是否要使用系统日期或文件名中的日期来确定最新文件是什么? If you want to use the create of modify date, check out the foptname function to determine them. 如果要使用创建修改日期,请查看foptname函数以确定它们。 This code looks at the date in the filename. 此代码查看文件名中的日期。 This code works without the need for X command authorization. 此代码无需X命令授权即可运行。

data newestFile (keep=newestFile);
 format newestFile $20.;
 retain newestFile newestDate;
 rc = filename("dir","z:\old\");
 did = dopen("dir");
 /* loop through file and subdirectories in a directory */
 do i = 1 to dnum(did); 
  csvFile = dread(did,i);   
  rc=filename("fid",cats("z:\old\",csvFile));
  sdid=dopen("fid");
  /*check if date in name is newest if it is a file  */
  if sdid le 0 then do;
   csvFileDate = input(substr(csvFile,6,6),ddmmyy6.);
   if csvFileDate gt newestDate then do;
    newestDate = csvFileDate;
    newestFile = csvFile;
   end;
  end;
 else rc = dclose(sdid);
 end;
 rc = dclose(did);
 /* move and rename file with latest date to newestFile.csv */
 rc = rename(cats("z:\old\",newestFile), "z:\new\newestFile.csv",'file');
run;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM