我正在尝试重新创建一些代码,它与http://support.sas.com/resources/papers/proceedings10/158-2010.pdf上的第9-10页中的代码相反。所以,我不想让桌子从宽到长,我希望它从长到宽。
Id Col1 Col2
1 Val1 A
1 Val2 B
2 Val1 C
2 Val3 D
3 Val2 E 转到:
Id X_Val1 X_Val2 X_Val3
1 A B .
2 C . D
3 . . E对我该怎么做有什么想法吗?我知道我应该使用一个数组,并尝试创建一个新列X_Val1,其中X_Val1 = cat('X',Val1),其中X是一些字符串。
发布于 2013-10-18 04:48:57
你需要先弄清楚你需要多少个变量。然后,您可以创建变量,使用数组,并分配值。
data test;
input id col1 $ col2 $;
datalines;
1 Val1 A
1 Val2 B
2 Val3 C
2 Val4 D
2 Val5 E
;
run;
/*Need to get the number of variables that need to be created*/
proc sql noprint;
select max(c)
into :arr_size
from
( select ID, count(*) as c
from test
group by id
);
quit;
/*Get rid of leading spaces*/
%let arr_size=%left(&arr_size);
%put &arr_size;
data test_t;
set test;
by id;
/*Create the variables*/
format SOME_X1 - SOME_X&arr_size $8.;
/*Create an array*/
array SOME_X[&arr_size];
/*Retain the values*/
retain count SOME_X:;
if first.id then do;
count = 0;
do i=1 to &arr_size;
SOME_X[i] = "";
end;
end;
count = count + 1;
SOME_X[count] = col2;
if last.id then
output;
keep id SOME_X:;
run;发布于 2013-10-18 19:12:38
我不知道你为什么要用PROC TRANSPOSE以外的任何东西来做这件事。
proc transpose data=have out=want prefix='X_';
by id;
id col1;
var col2;
run;https://stackoverflow.com/questions/19441358
复制相似问题