多维数组-设置为数组中的表数据

时间:2019-05-22 09:28:23

标签: arrays sas

我对多维数组有疑问。我创建了一个表中两种数据之间的计算。 表BT_MATRYCA中的示例数据。 然后我从SUMA_RAZEM的值(从TYP:VAL_B的行)中划分出从COL1到COL17的TYP数据:

命令“ put d_st [i,j] =;”物有所值,但我需要使用此计算创建表(多维,使用X和Y)。我该怎么办?

data BT_MATRYCA;
infile DATALINES dsd missover;
input NAME $ TYP $ COL1 COL2 COL3 COL4 COL5 COL6 COL7 COL8 COL9 COL10 COL11 COL12 COL13 COL14 COL15 COL16 COL17 SUMA_RAZEM;
CARDS;
A1, VAL_A, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 500
A1, VAL_B, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 600
B1, VAL_A, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 12, 13, 14, 15, 16, 16, 20, 550
B1, VAL_B, 1, 20, 3, 20, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 800
C1, VAL_A, 20, 2, 3, 4, 5, 6, 7, 8, 9, 10, 30, 12, 13, 14, 15, 16, 17, 900
C1, VAL_B, 1, 2, 3, 20, 5, 6, 7, 8, 02, 10, 11, 12, 30, 14, 15, 16, 17, 780
;run;


data t3;
array m[6,18] _temporary_;
array n[6,18] _temporary_;
array d_st[6,18] _temporary_;
call missing(of d_st[*]);

if _n_ = 1 then do;
   do i = 1 by 1 until(z1);
      set BT_MATRYCA (where=(TYP = 'VAL_A')) end = z1;
      array c[18] COL1--SUMA_RAZEM;
      do j = 1 to 18;
         m[i, j] = c[j];
      end;
   end;

   do i = 1 by 1 until(z2);
      set BT_MATRYCA (where=(TYP = 'VAL_B')) end = z2;
      array v[18] COL1--SUMA_RAZEM;
      do j = 1 to 18;
         n[i,j] = v[j];
      end;
  end;
end;

do i = 1 to 6;
   do j = 1 to 18;
        IF m[i,18] ne 0 then
            d_st[i,j] = coalesce((n[i,j] / m[i,18]),0);
        ELSE
            d_st[i,j] = 0;
   end;
end;

do i = 1 to 6;
   do j = 1 to 18;
      put d_st[i,j] =;
   end;
end;
stop;
run;

`

1 个答案:

答案 0 :(得分:1)

不清楚您要问什么,但这听起来好像您想要这样的东西:

data t3 wide(keep = x1-x6) long(keep = x y z);
array m[6,18] _temporary_;
array n[6,18] _temporary_;
array d_st[6,18] _temporary_;
array _x[6] x1-x6;

put d_st[1,1]=;

if _n_ = 1 then do;
   do i = 1 by 1 until(z1);
      set BT_MATRYCA (where=(TYP = 'VAL_A')) end = z1;
      array c[18] COL1--SUMA_RAZEM;
      do j = 1 to 18;
         m[i, j] = c[j];
      end;
   end;

   do i = 1 by 1 until(z2);
      set BT_MATRYCA (where=(TYP = 'VAL_B')) end = z2;
      array v[18] COL1--SUMA_RAZEM;
      do j = 1 to 18;
         n[i,j] = v[j];
      end;
  end;
end;

do i = 1 to 6;
   do j = 1 to 18;
        IF m[i,18] ne 0 then
            d_st[i,j] = coalesce((n[i,j] / m[i,18]),0);
        ELSE
            d_st[i,j] = 0;
   end;
end;

/* Note switching of inner and outer loops*/
do j = 1 to 18;
  do i = 1 to 6;
    _x[i] = d_st[i,j];
    x = i;
    y = j;
    z = d_st[i,j];
    output long;
  end;
  output wide;
end;

stop;
run;