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 ;

 

Transpose with Array

by Irina 14. April 2007 07:58

data product_transpose;
set product_transpose_200110_200201;
array product

ms_cards
salary
l_miuazim
miuazim
num_hk
sum_hk 
loan
misgeret_ashrai
osher
pasiv;

array name{10} $ 16;
do i=1 to dim(product);
name[i]=vname(product[i]);
amount=product [i];
varname=name[i];
month_lag=month_lag6;
output;
end;
keep amount varname month_lag;
run;


proc sql;
select distinct
year into: period
separated by '+'
from example;
quit;

 

%macro trans;
%do i=1 %to 2;
DATA example (DROP=I year x y year1 year2 z);
;
ARRAY years {2} year1- year2;
ARRAY xs {2} x1-x2;
ARRAY ys {2} y1-y2;
DO I=1 TO 2 UNTIL (LAST.b);
SET dug;
BY b;
years {I}=year;
x_%scan(&period,&i,+)=xs{I};

xs{I}=x;
ys{I}=y;
END;

run;
%end;
%mend;
%trans;

 

About the author

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


Send mail Email

Blogroll

    Disclaimer

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

    © Copyright 2012

    Sign in

    eXTReMe Tracker