SASTechies [email_address] http://www.sastechies.com
A SAS  array  is a temporary grouping of SAS variables under a single name. An array exists only for the duration of the DATA step.  data  work.report;  input mon tue wed thr fri sat sun; put _all_; mon= 5 *(mon- 32 )/ 9 ;  tue= 5 *(tue- 32 )/ 9 ; wed= 5 *(wed- 32 )/ 9 ; thr= 5 *(thr- 32 )/ 9 ; fri= 5 *(fri- 32 )/ 9 ;  sat= 5 *(sat- 32 )/ 9 ; sun= 5 *(sun- 32 )/ 9 ;  put _all_; cards; 100 103 100 107 90 65 56 70 30 32 40 50 80 0 ; run ; data work.report; set master.temps;  array wkday(7) mon tue wed thr fri sat sun;   do i=1 to 7; wkday(i) =5*( wkday(i) -32)/9; end;  run;  SAS Techies  2009 ARRAY  array-name { dimension }   elements ;   where  array-name  specifies the name of the array  dimension  describes the number and arrangement of array elements  elements  lists the variables to include in the array.  array  sales  {4};  array  sales  {4} qtr1 qtr2 qtr3 qtr4;  array sales  {*}  qtr1 qtr2 qtr3 qtr4;  array sales{4}  qtr1- qtr4 ;  array goal{4} g1 g2 g3 g4 ( 9000 9300 9600 9900 );  11/13/09
SAS Techies  2009 SAS Data Set Hrd.Fitclass  11/13/09 Name  Weight1 Weight2 Weight3  Weight4 Weight5 Weight6  Alicia  69.6  68.9  68.8  52.6  52.6  51.7  Brenda 68.6  67.6  67.0  67.6  66.6  66.0
data  score2(drop=i); array test{ 3 }  _temporary_  ( 90   80   70 ); array score{ 3 } s1-s3; input id score{*}; do i= 1  to  3 ; if score{i}>=test{i} then do; NewScore=score{i}; output; end; end; datalines; 1234  99 60 82 5678  80 85 75 ; run ; create temporary array elements for DATA step processing without creating new variables by specifying  _TEMPORARY_   Temporary array elements do not appear in the resulting data set.  Temporary array elements are useful when the array is only needed to perform a calculation.  You can improve performance time by using temporary array elements. SAS Techies  2009 11/13/09
SAS Techies  2009 array new{ r , c } x1-x12;  array month{3,4}  x1 x2 x3 x4 x5 x6 x7 x8 x9 x10 x11 x12 ;  Array Q{4}; do i=1 to 4;   Total=0; do j=1 to 3;   Q(i)=total+mon(i,j);   end;   end;   11/13/09                                                   array new{12}  x1 x2 x3 x4 x5 x6 x7 x8 x9 x10 x11 x12 ;
SAS Techies  2009 data finance.quarters(drop=i j);  set finance.monthly;  array m(4,3) month1-month12; array Qtr(4);  do i=1 to  dim( Qtr ) ; do j=1 to 3;  qtr(i)+m(i,j); end;  end; run;  11/13/09
data  temps; array temprg{ 2 , 5 } c1t1-c1t5 c2t1-c2t5; input City1 $ c1t1-c1t5 / City2 $ c2t1-c2t5; do i= 1  to  2 ; do j= 1  to  5 ; temprg{i,j}=round(temprg{i,j}); end; end; datalines; Fairfax 89.5 65.4 75.3 77.7 89.3 Frederic 73.7 87.3 89.9 98.2 35.6 Fairfax 75.8 82.1 98.2 93.5 67.7 Frederic 101.3 86.5 59.2 35.6 75.7 ; run ; 11/13/09 SAS Techies  2009

Arrays in SAS

  • 1.
  • 2.
    A SAS array is a temporary grouping of SAS variables under a single name. An array exists only for the duration of the DATA step. data work.report; input mon tue wed thr fri sat sun; put _all_; mon= 5 *(mon- 32 )/ 9 ; tue= 5 *(tue- 32 )/ 9 ; wed= 5 *(wed- 32 )/ 9 ; thr= 5 *(thr- 32 )/ 9 ; fri= 5 *(fri- 32 )/ 9 ; sat= 5 *(sat- 32 )/ 9 ; sun= 5 *(sun- 32 )/ 9 ; put _all_; cards; 100 103 100 107 90 65 56 70 30 32 40 50 80 0 ; run ; data work.report; set master.temps; array wkday(7) mon tue wed thr fri sat sun; do i=1 to 7; wkday(i) =5*( wkday(i) -32)/9; end; run; SAS Techies 2009 ARRAY array-name { dimension } elements ; where array-name specifies the name of the array dimension describes the number and arrangement of array elements elements lists the variables to include in the array. array sales {4}; array sales {4} qtr1 qtr2 qtr3 qtr4; array sales {*} qtr1 qtr2 qtr3 qtr4; array sales{4} qtr1- qtr4 ; array goal{4} g1 g2 g3 g4 ( 9000 9300 9600 9900 ); 11/13/09
  • 3.
    SAS Techies 2009 SAS Data Set Hrd.Fitclass 11/13/09 Name Weight1 Weight2 Weight3 Weight4 Weight5 Weight6 Alicia 69.6 68.9 68.8 52.6 52.6 51.7 Brenda 68.6 67.6 67.0 67.6 66.6 66.0
  • 4.
    data score2(drop=i);array test{ 3 } _temporary_ ( 90 80 70 ); array score{ 3 } s1-s3; input id score{*}; do i= 1 to 3 ; if score{i}>=test{i} then do; NewScore=score{i}; output; end; end; datalines; 1234 99 60 82 5678 80 85 75 ; run ; create temporary array elements for DATA step processing without creating new variables by specifying _TEMPORARY_ Temporary array elements do not appear in the resulting data set. Temporary array elements are useful when the array is only needed to perform a calculation. You can improve performance time by using temporary array elements. SAS Techies 2009 11/13/09
  • 5.
    SAS Techies 2009 array new{ r , c } x1-x12; array month{3,4} x1 x2 x3 x4 x5 x6 x7 x8 x9 x10 x11 x12 ; Array Q{4}; do i=1 to 4; Total=0; do j=1 to 3; Q(i)=total+mon(i,j); end; end; 11/13/09                                                   array new{12} x1 x2 x3 x4 x5 x6 x7 x8 x9 x10 x11 x12 ;
  • 6.
    SAS Techies 2009 data finance.quarters(drop=i j); set finance.monthly; array m(4,3) month1-month12; array Qtr(4); do i=1 to dim( Qtr ) ; do j=1 to 3; qtr(i)+m(i,j); end; end; run; 11/13/09
  • 7.
    data temps;array temprg{ 2 , 5 } c1t1-c1t5 c2t1-c2t5; input City1 $ c1t1-c1t5 / City2 $ c2t1-c2t5; do i= 1 to 2 ; do j= 1 to 5 ; temprg{i,j}=round(temprg{i,j}); end; end; datalines; Fairfax 89.5 65.4 75.3 77.7 89.3 Frederic 73.7 87.3 89.9 98.2 35.6 Fairfax 75.8 82.1 98.2 93.5 67.7 Frederic 101.3 86.5 59.2 35.6 75.7 ; run ; 11/13/09 SAS Techies 2009

Editor's Notes

  • #3 SASTechies.com Sharad C Narnindi - Attic Technologies 2005
  • #4 SASTechies.com Sharad C Narnindi - Attic Technologies 2005
  • #5 SASTechies.com Sharad C Narnindi - Attic Technologies 2005
  • #6 SASTechies.com Sharad C Narnindi - Attic Technologies 2005
  • #7 SASTechies.com Sharad C Narnindi - Attic Technologies 2005
  • #8 SASTechies.com Sharad C Narnindi - Attic Technologies 2005