How to iterate through files in SAS?

寵の児 提交于 2021-02-08 06:04:35

问题


I would like to iterate through files in a particular folder, and extract substrings of their files names. Is there an easy way to do this?

lib dir '.../folder';
#iterate through all files of dir and extract first five letters of file name;
#open files and do some processesing, aka data steps proc steps;

回答1:


Firstly, I'd point out that "dir" appears to be a misspelt libref, not a folder. If you are looking for files in a folder, you can use:

   %macro get_filenames(location);
    filename _dir_ "%bquote(&location.)";
    data filenames(keep=fname);
      handle=dopen( '_dir_' );
      if handle > 0 then do;
        count=dnum(handle);
        do i=1 to count;
          fname=subpad(dread(handle,i),1,5);/* extract first five letters */
          output filenames;
        end;
      end;
      rc=dclose(handle);
    run;
    filename _dir_ clear;
    %mend;

%get_filenames("c:\temp\");

If you are looking for datasets in a library, you can use:

proc sql;
create table datasets as
  select substr(memname,1,5) as dataset
  from dictionary.tables
  where libname='LIB'; /* must be uppercase */

either approach will produce a dataset of 'files' which can be subsequently 'stepped through'..



来源:https://stackoverflow.com/questions/21886069/how-to-iterate-through-files-in-sas

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