Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

Arrays in SAS

3,575 views

Published on

Learning
Base SAS,
Advanced SAS,
Proc SQl,
ODS,
SAS in financial industry,
Clinical trials,
SAS Macros,
SAS BI,
SAS on Unix,
SAS on Mainframe,
SAS interview Questions and Answers,
SAS Tips and Techniques,
SAS Resources,
SAS Certification questions...

visit http://sastechies.blogspot.com

  • Be the first to comment

Arrays in SAS

  1. 1. SASTechies [email_address] http://www.sastechies.com
  2. 2. <ul><li>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. </li></ul><ul><li>data work.report; </li></ul><ul><li>input mon tue wed thr fri sat sun; </li></ul><ul><li>put _all_; </li></ul><ul><li>mon= 5 *(mon- 32 )/ 9 ; </li></ul><ul><li>tue= 5 *(tue- 32 )/ 9 ; </li></ul><ul><li>wed= 5 *(wed- 32 )/ 9 ; </li></ul><ul><li>thr= 5 *(thr- 32 )/ 9 ; </li></ul><ul><li>fri= 5 *(fri- 32 )/ 9 ; </li></ul><ul><li>sat= 5 *(sat- 32 )/ 9 ; </li></ul><ul><li>sun= 5 *(sun- 32 )/ 9 ; </li></ul><ul><li>put _all_; </li></ul><ul><li>cards; </li></ul><ul><li>100 103 100 107 90 65 56 </li></ul><ul><li>70 30 32 40 50 80 0 </li></ul><ul><li>; </li></ul><ul><li>run ; </li></ul><ul><li>data work.report; </li></ul><ul><li>set master.temps; </li></ul><ul><li>array wkday(7) mon tue wed thr fri sat sun; </li></ul><ul><li>do i=1 to 7; </li></ul><ul><li>wkday(i) =5*( wkday(i) -32)/9; </li></ul><ul><li>end; </li></ul><ul><li>run; </li></ul>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. 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. 4. <ul><li>data score2(drop=i); </li></ul><ul><li>array test{ 3 } _temporary_ ( 90 80 70 ); </li></ul><ul><li>array score{ 3 } s1-s3; </li></ul><ul><li>input id score{*}; </li></ul><ul><li>do i= 1 to 3 ; </li></ul><ul><li>if score{i}>=test{i} then </li></ul><ul><li>do; </li></ul><ul><li>NewScore=score{i}; </li></ul><ul><li>output; </li></ul><ul><li>end; </li></ul><ul><li>end; </li></ul><ul><li>datalines; </li></ul><ul><li>1234 99 60 82 </li></ul><ul><li>5678 80 85 75 </li></ul><ul><li>; </li></ul><ul><li>run ; </li></ul><ul><li>create temporary array elements for DATA step processing without creating new variables by specifying _TEMPORARY_ </li></ul><ul><li>Temporary array elements do not appear in the resulting data set. </li></ul><ul><li>Temporary array elements are useful when the array is only needed to perform a calculation. </li></ul><ul><li>You can improve performance time by using temporary array elements. </li></ul>SAS Techies 2009 11/13/09
  5. 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. 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. 7. <ul><li>data temps; </li></ul><ul><li>array temprg{ 2 , 5 } c1t1-c1t5 c2t1-c2t5; </li></ul><ul><li>input City1 $ c1t1-c1t5 / </li></ul><ul><li>City2 $ c2t1-c2t5; </li></ul><ul><li>do i= 1 to 2 ; </li></ul><ul><li>do j= 1 to 5 ; </li></ul><ul><li>temprg{i,j}=round(temprg{i,j}); </li></ul><ul><li>end; </li></ul><ul><li>end; </li></ul><ul><li>datalines; </li></ul><ul><li>Fairfax 89.5 65.4 75.3 77.7 89.3 </li></ul><ul><li>Frederic 73.7 87.3 89.9 98.2 35.6 </li></ul><ul><li>Fairfax 75.8 82.1 98.2 93.5 67.7 </li></ul><ul><li>Frederic 101.3 86.5 59.2 35.6 75.7 </li></ul><ul><li>; </li></ul><ul><li>run ; </li></ul>11/13/09 SAS Techies 2009

×