任何身体可以帮助我做这个循环吗?

问题描述 投票:-3回答:1

任何人都可以帮我看看这个循环中有什么问题?我想用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; 
sas
1个回答
0
投票
%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
© www.soinside.com 2019 - 2024. All rights reserved.