SAS指南从临时文件夹中读取最新文件

时间:2018-10-29 17:50:02

标签: import sas

我的任务是从门户网站导入最新上传的文件。我设法使用spicial import addon将所有必要文件上传到临时lib。

我的代码如下:

%let Location_from1=\\*****\DavWWWRoot\sites\IDR\; %let Location_to1=%sysfunc(pathname(work));

然后我使用插件并将几个文件导入到临时文件夹中,例如:

/u00/tmp/SAS_work9B07000013FF_asukr01lv/SAS_work3786000013FF_asukr01lv/*****1.xlsx /u00/tmp/SAS_work9B07000013FF_asukr01lv/SAS_work3786000013FF_asukr01lv/*****2.xlsx /u00/tmp/SAS_work9B07000013FF_asukr01lv/SAS_work3786000013FF_asukr01lv/*****3.xlsx

现在我需要找出最新的并将其导入到我的工作中。我该怎么办?

1 个答案:

答案 0 :(得分:1)

从该路径看来,您正在使用* nix。在这种情况下,您可以使用Shell脚本获取最新文件:

filename latest pipe "cd &Location_to1.; ls -t1 | egrep 'xlsx$' | head -n 1";

data _null_;
    infile latest truncOver;
    input fileName $200.;
    call symput('latestFile', strip(fileName));
run;

基于Bruno_SAS代码的与系统无关的解决方案:

%macro getLatestFile(dirName=, extType=, out=);
    %let extType = %upcase(&extType);

    * Get the list of all files with the last modification date;
    data allFiles;
        length msg $ 256 fileRef $ 8 dirName $ 1024 fileName $ 256 fullName $ 1281;

        rc_check=fileexist("&dirName");

        if rc_check=0 then
            do;
                putlog "ERROR: &sysMacroname &dirName does not exist";
                stop;
            end;

        rc_assign=fileName(fileRef, "&dirName");

        dirId=dopen(fileRef);

        if dirId=0 then
            do;
                msg=sysmsg();
                putlog "ERROR: &sysMacroname could not open &dirName as directory";
                putlog msg;
                rc_assign=fileName(fileRef);
                stop;
            end;

        n_files=dnum(dirId);

        do i=1 to n_files;
            fileName=dread(dirId, i);

            if upcase(scan(fileName, -1, "."))="&extType" then
                do;
                    dirName="&dirName";
                    fullName=catx("/", dirName, fileName);
                    n_match + 1;
                    rc_fnOpen=fileName('current', "&dirName" || fileName);
                    fileId=fOpen('current');
                    lastChanged=input(fInfo(fileId, 'Last Modified'), datetime.);
                    format lastChanged datetime.;
                    rc_close=fClose(fileId);
                    rc_fnClose=fileName('current');
                    output;
                end;
        end;

        dirId=dclose(dirId);
        rc_assign=fileName(fileRef);
        msg=catx(" ", "NOTE: &sysMacroname found", n_match, 
            "files that match &extType");
        putlog msg;
        keep dirName fileName fullName lastChanged;
    run;

    * Keep only the latest file;
    proc SQL outobs=1;
    create table &out. as
        select fullName
        from allFiles
        order by lastChanged desc
    ;
    quit;

%mend;

%getLatestFile(dirName= %sysFunc(pathName(work)), extType=xlsx, out=latest);
相关问题