【问题标题】:Can any body help me with this do loop?任何人都可以帮我做这个循环吗?
【发布时间】:2017-12-22 08:47:15
【问题描述】:

谁能帮我看看这个do循环有什么问题?我想用 2、3、4 和 5 生成 outm。我需要创建一个 do 循环来处理它,但我失败了。

data Dmatrix5;
input x1 x2 x3 x4 x5;
cards;
0 . . . .
2 0 . . .
15 0 . .
4 3 5 0 .
5 8 7 6 0
;
run;%macro loop 
%Do outm=5%to2;
%let inm=5;
%let outm=%eval(&inm-1);
data Dmatrix&outm;
retain minv small large;
array cor{&inm,&inm} _temporary_;
do i=1 to &inm;
  set Dmatrix&inm;
  array a[&inm] x1--x&inm;
  array op[&outm];
  do j=1 to &inm;
    cor[i,j]=a[j];
  end;
end;`

do i=1 to &inm;
    if i>1 then do;
        do j=1 to i-1;
            m=cor{i,j};
            if i=2 & j=1 then do;
                minv=m;
                small=i; 
                large=j;
                delrc=j;
            end;
            else do;
                if minv > m then do;
                    minv=m;
                    delrc=i;
                    if i>j then do;
                        small=j;
                        large=i;
                    end;
                else do;
                        small=i; 
                        large=j;
                    end;
                end;    
            end;
        end;
    end;
end;

array out{&inm};
do i=1 to &inm;
    do j=1 to i;
        if large=i and small=j then out[j]=99999;
        else if large=j and small=i then out[j]=99999;
        else if j=i then out[j]=0; 
        else do;
            if small=i then do; 
                if small>j and large >j then do;
                    if cor[small,j]<cor[large,j] then out[j]=cor[small,j];
                    else out[j]=cor[large,j];
                end;
                else if small>j and large<j then do;
                    if cor[small,j]<cor[j,large] then out[j]=cor[small,j];
                    else out[j]=cor[j,large];
                end;    
            end;
            else if small=j then do;
                if small<i and large<i then do;
                    if cor[i,small]<=cor[i,large] then out[j]=cor[i,small];
                    else out[j]=cor[i,large];
                end;
                else if small<i and large>i then do;
                    if cor[i,small]<=cor[large,i] then out[j]=cor[i,small];
                    else out[j]=cor[large,i];
                end;
            end;
            else if large=j or large =i then 
                out[j]=99999;
            else  out[j]=cor[i,j];              
        end;
    end;
    n=1;
    do k=1 to &inm;
        if k^=delrc then do;
            op[n]=out[k];
            n=n+1;
        end;
    end;
    if delrc^=i then output;
  end;
  keep op1-op&outm;
run;

data Dmatrix&outm;
set Dmatrix&outm;
x1=op1;
x2=op2;
keep x1-x2;
run;

%end;
%Mend loop;

%loop; 

【问题讨论】:

  • “以下是为了满足发布此问题的要求”我不知道发布胡言乱语的要求,通常在其他问题上看不到。
  • @jps 对于新用户,您必须拥有一定数量的非代码。有充分的理由。当然,添加胡言乱语并不能解决这个问题。 -1 .
  • @Joe OP 应该提供有关错误的更多详细信息。 “我需要创建一个 do 循环来处理它,但我失败了。”不是对问题的良好描述。
  • 完全同意。

标签: sas


【解决方案1】:
%Do outm=5%to2;

永远不会迭代。您需要使用%BY -1 下降1。很多documentation

%macro example1;
  %local index;
  %do index = 5 %to 2 %by -1;
    %put &=index;
  %end;
%mend;

%example1

或者你可以自己计算一个降序索引。

%macro example2;
  %local iteration index;
  %do iteration = 1 %to 4;
    %let index = %eval(6-&iteration);
    %put &=index;
  %end;
%mend;

%example2

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-06-16
    • 2013-02-28
    • 2013-10-09
    • 1970-01-01
    • 2021-12-29
    • 1970-01-01
    相关资源
    最近更新 更多