Arrays vs Transpose

by Irina 28. May 2007 11:41
To transpose your data (turning variables into observations or turning observations into variables), you can use either PROC TRANSPOSE or array processing within a DATA step.
           data a;
 	   input id  date total cod flounder haddock perch tuna;
 	   cards;
 	  1  1983	75	3.00	8.20	1.30	 .90	10.00
	  1  1984	80	4.60	8.80	2.60	1.40	11.20
	  1  1985	90	5.00	9.30	4.10	2.20	10.00
	  2  1983	100	6.50	12.20	7.90	4.00	15.70
	  2  1984	125	8.00	9.50	8.90	4.10	15.80
	  2  1985	134	11.20	15.20	22.70	4.90	26.20
	  2  1986	152	24.20	34.90	32.80	10.30	33.30
	  2  1997	177	22.60	41.70	31.90	15.90	58.70
	 ;
	run;

A Simple Transposition:

proc

sort data=a;by id date;run;

proc transpose prefix=total data=a out=b;

by id;

var total;

id date;

run;

Result:

 

 id NAME OF FORMER VARIABLE total1983 total1984 total1985 total1986 total1997
1 1 total 75 80 90 . .
2 2 total 100 125 134 152 177

Transposing Two Variables.

Become more complex to use

proc transpose.And the DATA step has much more flexibility in this case .

Example :

proc sql noprint ;
select max (c) into :dim
from (select count(*) as c from a group id) ;
quit ;

data ll (keep= id date _total: _flounder: ) ;
retain id ;
do _n_ = 1 by 1 until (last.id) ;
set a ;
by id ;
array _total [&dim] ;
array _flounder[&dim];
_total [_n_] = total ;
_flounder [_n_]=flounder;
end ;
run ;

 

Related posts

Add comment


(will show your Gravatar icon)  





Live preview

7/30/2010 8:22:05 AM

 

About the author

Irina Spivak Irina Spivak
Team Leader at G-Stat. More...


Send mail Email

Authors

Blogroll

    Disclaimer

    The opinions expressed herein are my own personal opinions and do not represent my employer's view in anyway.

    © Copyright 2010

    Sign in

    eXTReMe Tracker