Do—Array Over Do Array

data temp;

input x1 x2 x3 x4$ x5$;

cards;

1 2 3 AA BB

2 3 4 AB CC

3 4 6 AC DD

4 5 6 AD EE

5 6 7 AE FF

6 7 8 AF GG

;

run;

  Array –  */

  array is group of variables */

data test;

set temp;

array firstA {3} x1-x3 ;

do i = 1 to 3;

if firstA{i} > 5 then firstA{i} = 10;

end;

drop i;

run;

  if we don’t know how many numeric variables we have and we don’t know name of the numeric variables */

  * – all */

  _character_ – all character variables */

  * – astrisk */

  dim – dimension */

data test;

set temp;

array firstA {*} _numeric_;

do i = 1 to dim(firstA);

if firstA{i} > 5 then firstA{i} = 10;

end;

drop i;

run;

  _all_ = all variables */

data temp;

  infile “C:/data/new.xls”; */

input x1 x2 x3 x4$ x5$;

cards;

1 2 3 AA BB

2 3 4 AB CC

3 4 5 AC DD

4 5 6 AD EE

5 6 7 AE FF

6 7 8 AF GG

;

run;

  data test; */

  set temp; */

  array cvars1 {*} _character_; */

  do z = 1 to dim(cvars1); */

  where cvars1{z} like “_A” or cvars1{z} like “A_” or  cvars1{z} like “_A”; */

  run; */

data test;

set temp;

array cvars (*) _character_;

array nvras (2) $ x6-x7;

do i = 1 to dim(cvars);

nvras{i} = substr(cvars{i},1,1);

end;

drop i;

run;

data test;

set temp;

array cvars (*) _numeric_;

array nvars {3} x6-x8 ;

do i = 1 to dim(cvars);

if cvars{i} > 5 then nvars{i} = 10;

end;

drop i;

run;

proc print data=test;

run;

  data abcd; */

  set temp; */

  array nvars (*) _numeric_ x1 x2 x3; */

  array pvars (*) px1 px2 px3; */

  array pvars1 (*) zx1 zx2 zx3; */

  array pctinc {3} _temporary_ (.1 , .1 ,.1);  */

  array zctinc {3} _temporary_ (.2 , .2 ,.2);  */

  do i = 1 to dim(nvars); */

  pvars{i} = nvars{i} * pctinc{i}; */

  pvars1{i} = nvars{i} * zctinc{i}; */

  end; */

  drop i; */

  run;   */

data abcd;

set temp;

array nvars (*) _numeric_;

array pvars (*) px1 px2 px3;

array pctinc {3} _temporary_ (5.4 , 5.4 ,1.3);

do i = 1 to dim(nvars);

pvars{i} = nvars{i} * pctinc{i};

end;

drop i;

run; 

  When the key word _TEMPORARY_ is used in a ARRAY statement, data elements are created  */

  but are not stored in the data file. */

  Suppose you are asked to create a flag in cases wherein sum of variables x1,x2 and x3 is greater than 10. */

data test;

set temp;

array nvars (*) x1-x3;

if sum(of nvars(*)) > 10 then flag =1;

else flag=0;

run;

data test;

set temp;

if sum(of x1-x3) > 10 then flag =1;

else flag=0;

run;

  DO OVER LOOP */

  The DO OVER loop is one of the most useful DO loops.  */

  It can be used with an array when indexing of the array is not needed. */

data test;

set temp;

array nvars _all_;

do over nvars;

if nvars > 3 then nvars = .;

end;

run;