Способ SAS перебирать таблицу вне шага данных

Dec 02 2020

Мне интересно, как лучше всего выполнить цикл макросов над таблицей данных за пределами шага данных, например, для чтения файлов из таблицы haveи выполнения сложного анализа для каждого из файлов.

Предположим, у нас есть таблица, haveсодержащая набор имен файлов и других метаданных:

N  filename  purpose
1  foo.xls   Blue team data
2  bar.xls   Read team data

Я думал о чем-то вроде

%local lines current_file current_purpose;

proc sql noprint;
   select count(*) into: lines from have;
quit;

%do I=1 %to &lines.;
   %put --- Process file number &I. ---;
   data _null_;
      set have;
      if _n_=&I. then do;
        call symput('current_file',filename);
        call symput('current_purpose',purpose);
      end;
   run;
   %put --- &current_file. contains &purpose.;
   /* Here comes the actual analysis */ 
%end;

Это как это сделать? На мой взгляд, это не самый простой способ.

Связанные вопросы:

  • SAS loop through datasets
  • SAS let statement: refer to a cell value?

Ответы

3 Tom Dec 02 2020 at 20:46

So if you defined a macro name ANALYSIS with input parameters FILENAME and PURPOSE.

%macro analysis(filename,purpose);
  /* Here comes the actual analysis */ 
  title &purpose ;
  proc import datafile="&filename" ....
%mend;

Then you can use a data step to generate one call to the macro for each observation. You can use CALL EXECUTE, but I find it clearer and easier to debug to just write the code to a file and then %INCLUDE it. Especially when the parameter name matches the variable name in the metadata being used to drive the code generation.

So this step :

filename code temp;
data _null_;
   set have;
   file code;
   put '%analysis(' filename= ',' purpose= :$quote. ')' ;
run;

Will generate a program like:

%analysis(filename=foo.xls,purpose="Blue team data")
%analysis(filename=bar.xls,purpose="Red team data")

Which you can then run using

%include code / source2;