原始数据库结构:
转换之后的结构:
程序如下:
data test; input no x y z; datalines; 1 48 138 103 1 36 121 160 2 13 41 59 2 31 127 48 3 36 108 107 3 45 132 93 4 222 751 426 4 398 1056 574 ; run; %macro test(row,col,all); proc sort data=test;by no;run; proc sql noprint; select count(distinct no) into: tnum from test; quit; proc sql noprint; create table temp1 as select distinct no from test; quit; %do i=1 %to &tnum.; data _null_; set temp1; if _n_=&i. then call symputx('no',no); run; data test&no.; retain no x y x; set test; if no="&no."; run; data test&no.(drop=no x y z i j); set test&no. nobs=nobs; array all(&row.,&col.)$ a1-a&all.; array vars(*) no x y z; retain a1-a&all.; i+1; do j=1 to &col.; all(i,j)=vars(j); end; put a1-a&all.; if _n_=nobs then output; run; data test&no.; set test&no.; rename a1=no a2=x1 a3=y1 a4=z1 a6=x2 a7=y2 a8=z2; drop a5; run; proc datasets nolist; append base=temp2 data=test&no. force; delete test&no.; run; data result;set temp2;run; %end; proc datasets nolist;delete temp1 temp2;quit;run; %mend; %test(2,4,8);
方法多少有点缺陷,比如数组只能读取数值或字符,需要自己在宏内部rename变量名,等等。留待以后改进。
有道无术,术尚可进;有术无道,止于术也!