【问题标题】:SAS DATA STEP with variable number of data-set unknown a priori具有可变数量数据集的 SAS DATA STEP 先验未知
【发布时间】:2016-03-24 15:23:44
【问题描述】:

我正在使用 SAS 9.3 对几个 CSV 数据集进行一些分析。 我正在寻找自动加载对 SAS 说的文件 查看特定目录并加载到单独的数据集每个文件。

我是 SAS 编码的新手,如果我忽略了一些重要的点,请原谅我。

我要使用的代码是这样的:

filename filelist pipe 'dir "C:\my_path\*.csv" /b';

data file_list ; *** here I have the list of files to be read
    length file_name $256 ;
    infile filelist length=reclen ;
    input file_name $varying256. reclen ;
run;

*** HERE I MISS HOW TO DYNAMICALLY LOAD A NUMBER OF FILES NOT KNOWN BEFORE;

*** I should find a way to say: set file_list;
proc import datafile="C:\my_path\"||file_name; *** I know that in this way doesn't work but It was just to show my idea of doing it.
    out = file_name
    dbms = csv
    replace;
    getnames = yes;
run;

非常感谢您的帮助! 请随时完全编辑解决此任务的方法。

收到建议后,我修改了代码,但还是不行……

filename filelist pipe 'dir "C:\my_path\*.csv" /b';

data file_list ; *** here I have the list of files to be read
    length file_name $256 ;
    infile filelist length=reclen ;
    input file_name $varying256. reclen ;
run;

%MACRO load_my_csv_file(name_file=);

proc import    datafile="C:\my_path\"||&name_file
    out = &name_file
    dbms = csv
    replace;
    getnames = yes;
run;
%MEND load_my_csv_file;

data _NULL_ ;
    set file_list;
    call execute('%load_my_csv_file(name_file='||file_name||')');
run;

但它不起作用!

【问题讨论】:

  • 不工作到底是什么意思?

标签: sas


【解决方案1】:

将 PROC IMPORT 制作成带有 DATAFILE 和 OUT 参数的宏。然后在数据 FILE_LIST 中使用 CALL EXECUTE 调用它。

【讨论】:

  • 感谢您的建议!我会做并发布代码!
【解决方案2】:

试试这个:

/*get the filepath of the folder you want to import from*/
%let folderpath = your_file_path;
filename files pipe "%str(dir %"&folderpath.%" /B) " lrecl=5000;

/*create a dataset containing the names of all the files in that directory*/
data list_of_files;
    infile files truncover;
    input file $255.;
run;

/*select those into a macro variable to loop through*/
proc sql noprint;
    select distinct file into: files_to_import
    separated by " "
    from list_of_files;
quit;

/*loop through the macro variable and import all the files*/
%macro csv_importer;

    %do i = 1 %to %sysfunc(countw(&files_to_import.));
    %let this_file = %scan(&files_to_import., &i., " ");

        proc import datafile = "&folderpath.\&this_file."
            out = dataset&i.
            dbms = csv replace;
            getnames = yes;
        run;

    %end;

%mend csv_importer;

%csv_importer;

【讨论】:

    【解决方案3】:

    您没有在宏中正确使用参数值来生成有效的 SAS 语法。您不能在过程的选项值内使用连接运算符 (||)。而是在适当的位置展开宏变量的值,以便生成的代码是该过程的有效语法。此外,您可能会发现需要在宏中添加另一个参数来处理物理文件名不是用于 SAS 数据集的有效名称的情况。例如,您的文件名可能以 .csv 结尾,但您不想在 SAS 数据集的名称中包含 .csv

    %MACRO load_my_csv_file(name_file=,dsname=);
    proc import datafile="C:\my_path\&name_file"
      out = &dsname
      dbms = csv
      replace
    ;
      getnames = yes;
    run;
    %MEND load_my_csv_file;
    

    然后你可以这样称呼它:

    data _NULL_ ;
      set file_list;
      dsname = scan(file_name,-2,'./\');
      call execute(cats('%load_my_csv_file(name_file=',file_name,',dsname=',dsname,')'));
    run;
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-04-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-05-27
      • 1970-01-01
      相关资源
      最近更新 更多