从 SAS 中不起作用的 CSV 文件导入单个值

Importing single value from a CSV file not working in SAS

我正在尝试使用从 CSV 文件中检索单个值的宏。我编写了一个 MACRO,如果只有 1 个 CSV 文件,它就可以正常工作,但是当我必须针对多个文件 运行 时,它不会提供预期的结果。如果有多个文件,它 returns 每次迭代中最后一个文件的值。

%macro reporting_import( full_file_route );


%PUT The Source file route is: &full_file_route;
%PUT ##############################################################;

PROC IMPORT datafile = "&full_file_route"
    out = file_indicator_tmp
    dbms = csv
    replace;
    datarow = 3;
RUN;

data file_indicator_tmp (KEEP= lbl);
    set file_indicator_tmp;
        if _N_ = 1;
        lbl = "_410 - ACCOUNTS"n;
run;

proc sql noprint ;
     select lbl
     into :file_indicator
     from file_indicator_tmp;
quit;

%PUT The Source Reporting period states: &file_indicator;
%PUT ##############################################################;

%mend;

这是我执行宏的地方。每个 excel 文件的完整路线都作为单独的记录存在于名为 "HELPERS.RAW_WAITLIST" 的数据集中。

data _NULL_;
set HELPERS.RAW_WAITLIST;
  call execute('%reporting_import('||filename||')');
run;

在我刚才的例子中 运行,一个文件包含 01-JUN-2015,另一个包含 02-JUN-2015。但是LOG文件中的代码returns是什么:

The Source file route is: <route...>\FOO1.csv
##############################################################
The Source Reporting period states: Reporting Date:02-JUN-2015  
##############################################################
The Source file route is: <route...>\FOO2.csv
##############################################################
The Source Reporting period states: Reporting Date:02-JUN-2015
##############################################################

有人知道为什么会这样吗?或者是否有更好的方法来解决这个问题?

更新:

如果我从 MACRO 中删除代码并 运行 为每个输入文件手动删除它,它将完美运行。所以一定和MACRO覆盖值有关。

我不太清楚你的文件之间的联系。但是,不是导入 CSV 文件然后搜索您的字符串,您不能使用 pipe command 将对 CSV 文件的 grep 搜索结果保存到数据集,然后只读入结果吗?

更新:

我尝试在本地复制您的问题,如果我设置 file_indicator 和下面的调用 symput 而不是您的 into :file_indicator:

,它对我有用
data file_indicator_tmp (KEEP= lbl);
  set file_indicator_tmp;
    if _N_ = 1;
    lbl = "_410 - ACCOUNTS"n;
data _null_ ;
  set file_indicator_tmp ;
  if _n_=1 then call symput('file_indicator',lbl) ;
run;

CALL EXECUTE 存在棘手的计时问题。当它调用宏时,如果该宏从数据集变量生成宏变量,则最好将宏调用包装在 %NRSTR() 中。这样 call execute 生成宏调用,但实际上并不执行宏。因此,请尝试将您的调用执行语句更改为:

call execute('%nrstr(%%)reporting_import('||filename||')');

我发布了更长的解释