TRANSPOSE的DATA步实现

时间:2023-03-09 06:19:01
TRANSPOSE的DATA步实现
 data a;
input name $ a b ;
cards;
x
x
x
y
y
y
;
run;
%macro transpose;
proc sql noprint ;
select count (distinct name) into:name_n from a;
select distinct name into: name_ separated by"|" from a;
quit;
%do i = %to &name_n;
%let _name = %scan("&name_",&i,"|");
proc sql noprint;
select count(a) into: na from a where name="&_name" ;
select name into:name_x from a where name="&_name";
select a into: aa separated by "|" from a where name="&_name";
select count(b) into: nb from a where name="&_name";
select name into:name_x from a where name="&_name";
select b into: bb separated by "|" from a where name="&_name";
quit;
data tmp;
set a(drop=name);
run;
proc sql noprint;
select name into:varlist separated by "|" from dictionary.columns
where libname="WORK" and memname="TMP";
quit;
data _null_ ;
nvar=count("&varlist","|")+;
call symput("nvar",nvar);
run;
%do j= %to &nvar;
%let _var=%scan(%bquote(&varlist),&j,"|");
data b&i.&j.(keep=name _name_ col: );
name="&_name";
_name_="&_var";
array _a[&na] ;
array _b[&nb] ;
do i = to &na;
_a[i]=scan("&aa",i,"|");
end;
do j = to &nb;
_b[j]=scan("&bb",j,"|");
end;
%do p= %to &&n&_var;
rename _&_var.&p=col&p;
%end;
proc sort nodupkey;by name col:;
run;
%end;
%end;
data wanted ;
set b:;
run;
%mend transpose;
%transpose

Just for fun.

yant07

相关文章