Reading the file folder on UNIX using SAS

拟墨画扇 提交于 2019-12-25 03:26:21

问题


I am trying to read the folder with zip files using Pipe Command. But I get error saying ls command not recognized. There are actually 2 zip files(ABC_*.zip) in the folder /PROD/
Can anybody help me in this?

%let extl_dir=/PROD/ ; 

filename zl pipe "ls &extl_dir.ABC_*.zip";

data ziplist_a;
    infile zl  end=last;
    length path $200 zipnm $50 filedt $15;
    input path $;
    zipnm=scan(path,-1,"/");
    filedt=scan(scan(path,-1,"_"),1,".");
    call symput('zip'||left(_n_), zipnm);
    call symput('path'||left(_n_), path);
    call symput('filedt'||left(_n_),filedt);
    if last then call symput('num_zip',_n_); 
    *call symput('flenm',filenm); 
run; 

回答1:


SAS has published a convenient macro to list files within a directory that does not rely upon running external commands. It can be found here. I prefer this approach as it does not introduce external sources of possible error such as user permissions, pipe permissions etc.

The macro uses datastep functions (through %sysfunc) and the commands can be called in the same manner from a datastep. Below is an example which extracts tile information.

%let dir = /some/folder;
%let fType = csv;
data want (drop = _:);
    _rc = filename("dRef", "&dir.");
    _id = dopen("dRef");
    _n = dnum(_id);
    do _i = 1 to _n;
        name = dread(_id, _i);
        if upcase(scan(name, -1, ".")) = upcase("&fType.") then do;
            _rc = filename("fRef", "&dir./" || strip(name));
            _fid = fopen("fRef");
            size = finfo(_fid, "File Size (bytes)");
            dateCreate = finfo(_fid, "Create Time");
            dateModify = finfo(_fid, "Last Modified");
            _rc = fclose(_fid);
            output;
        end; 
    end;
    _rc = dclose(_id);
run;


来源:https://stackoverflow.com/questions/28284819/reading-the-file-folder-on-unix-using-sas

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!