SAS - 从condition1循环到condition2

时间:2018-04-04 19:35:18

标签: macros sas do-loops sas-studio

我希望有一个清理我所拥有的一些混乱数据的程序,我希望为我正在进行的项目的资产和负债方面做这个。

我的问题是有一种方法可以使用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.

1 个答案:

答案 0 :(得分:3)

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

请注意,宏逻辑需要位于宏内部。你不能在&#34; open&#34;中使用它。代码。

%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)