2

我正在寻找一个程序来清理我拥有的一些混乱数据,我希望为我正在从事的项目的资产和负债方面做到这一点。

我的问题是有一种方法可以使用 do 循环来使用清理数据首先清理资产然后清理负债。像这样的东西:

%do %I = Asset %to Liability;

%assetorliability= I ;

proc sort data = &assetorliability;
by price;
run;

data want&assetorliability;
set &assetorliability;
if _N_ < 50000;
run;

实际的脚本很长,所以单个宏可能不是理想的解决方案,但这个循环会很棒。

TIA。

编辑:该程序包含一些宏,收到的错误如下:

%let list =Asset Liability;
%do i=1 %to %sysfunc(countw(&list,%str( )));

%let next=%scan(&list,&i,%str( ));

%Balance;

%end;

在宏中,数据步骤以平衡和列表命名,以允许每个场景。错误是:

13221  %let list =Asset Liability;
13222  %do i=1 %to %sysfunc(countw(&list,%str( )));
ERROR: The %DO statement is not valid in open code.
13223
13224  %let next=%scan(&list,&i,%str( ));
WARNING: Apparent symbolic reference I not resolved.
WARNING: Apparent symbolic reference I not resolved.
ERROR: A character operand was found in the %EVAL function or %IF condition where a numeric
       operand is required. The condition was: &i
ERROR: Argument 2 to macro function %SCAN is not a number.
ERROR: The %END statement is not valid in open code.
4

1 回答 1

3

%do语句不如数据步do语句灵活。要遍历值列表,您需要将列表放入宏变量中并在%do循环中使用索引变量。

请注意,宏逻辑需要在宏内部。您不能在“开放”代码中使用它。

%macro do_over(list);
%local i next;
%do i=1 %to %sysfunc(countw(&list,%str( )));
  %let next=%scan(&list,&i,%str( ));
  proc sort data = &next ;
    by price;
  run;

  data want&next ;
  ...
%end;
%mend do_over ;
%do_over(Asset Liability)
于 2018-04-04T19:40:42.437 回答