z**o 发帖数: 149 | 1 有一个变量
a1
a2
a3
a4
a5
a6
.
.
.
想变成这样
a1 a2 a3 a4 a5
a6 a7 a8 a9 a10
. . . . .
. . . . .
从一个column 变成5个, 想了半天没想出来, 多谢大牛们! |
k*******a 发帖数: 772 | 2 可以用transpose,不过可能得先建立一些新的变量 |
k*******a 发帖数: 772 | |
i**z 发帖数: 194 | 4 transpose 然后重新命名
【在 z**o 的大作中提到】 : 有一个变量 : a1 : a2 : a3 : a4 : a5 : a6 : . : . : .
|
h********o 发帖数: 103 | 5 DATA ONE;
INPUT VAR $ @@;
CARDS;
A1 A2 A3 A4 A5 A6 A7 A8 A9 A10 A11 A12 A13 A14
;
DATA ONE;
SET ONE;
RETAIN ID ;
IF MOD(_N_, 5) = 1 THEN ID + 1;
RUN;
PROC TRANSPOSE DATA = ONE OUT = TWO (DROP = _NAME_ ID
RENAME = (COL1 = VAR1
COL2 = VAR2
COL3 = VAR3
COL4 = VAR4
COL5 = VAR5));
VAR VAR;
BY ID;
RUN;
PROC PRINT;
RUN;
Obs VAR1 VAR2 VAR3 VAR4 VAR5
1 A1 A2 A3 A4 A5
2 A6 A7 A8 A9 A10
3 A11 A12 A13 A14 |
h********o 发帖数: 103 | 6 This one is more clean.
=================
DATA ONE;
INPUT VAR $ @@;
CARDS;
A1 A2 A3 A4 A5 A6 A7 A8 A9 A10 A11 A12 A13 A14
;
DATA ONE;
SET ONE;
IF MOD(_N_, 5) = 1 THEN ID + 1;
RUN;
PROC TRANSPOSE DATA = ONE OUT = TWO (DROP = _NAME_ ID) PREFIX = VAR;
VAR VAR;
BY ID;
RUN; |
p***r 发帖数: 920 | 7 try my version, without transpose
DATA ONE;
INPUT VAR $ @@;
CARDS;
A1 A2 A3 A4 A5 A6 A7 A8 A9 A10 A11 A12 A13 A14
;
data two;
set one;
id= int((_n_-1)/5);
length v1-v5 $10.;
if mod(_n_,5)=1 then do;
v1='';
v2='';
v3='';
v4='';
v5='';
end;
if mod(_n_,5)=1 then v1=var;
if mod(_n_,5)=2 then v2=var;
if mod(_n_,5)=3 then v3=var;
if mod(_n_,5)=4 then v4=var;
if mod(_n_,5)=0 then v5=var;
if mod(_n_,5) ne 0 then
do;
retain v1 v2 v3 v4 v5;
end;
drop var;
run;
proc sort data=two ;
by id descending v1 descending v2 descending v3 descending v4 desending v5;
run;
proc sort data=two nodupkey;
by id ;
run; |
i**z 发帖数: 194 | 8 use proc iml
【在 h********o 的大作中提到】 : This one is more clean. : ================= : DATA ONE; : INPUT VAR $ @@; : CARDS; : A1 A2 A3 A4 A5 A6 A7 A8 A9 A10 A11 A12 A13 A14 : ; : DATA ONE; : SET ONE; : IF MOD(_N_, 5) = 1 THEN ID + 1;
|
s******r 发帖数: 1524 | 9 baozi pls.
data new;
merge old old(firstobs=2 rename=(id=id2) ) old(firstobs=3 rename=(id=id3))
old(firstobs=4 rename=(id=id4)) old(firstobs=5 rename=(id=id5))
;
if mod(_n_,5) ne 1 then delete;
run;
It would works fine with small dataset. If huge dataset, probably had better
to create 5 temp files and then merge them.
【在 z**o 的大作中提到】 : 有一个变量 : a1 : a2 : a3 : a4 : a5 : a6 : . : . : .
|
p***r 发帖数: 920 | 10 new version
data two;
set one end=eof;
id= int((_n_-1)/5);
length v1-v5 $10.;
if mod(_n_,5)=1 then do;
v1='';
v2='';
v3='';
v4='';
v5='';
end;
if mod(_n_,5)=1 then v1=var;
if mod(_n_,5)=2 then v2=var;
if mod(_n_,5)=3 then v3=var;
if mod(_n_,5)=4 then v4=var;
if mod(_n_,5)=0 then v5=var;
if mod(_n_,5) ne 0 then
do;
retain v1 v2 v3 v4 v5;
end;
drop var;
if mod(_n_,5) = 0 or eof;
run; |
o****o 发帖数: 8077 | 11 data old;
do x=1 to 100;
output;
end;
run;
*****************************************************
1:
data oldv/view=oldv;
set old;
id=mod(_n_-1, 5)+1;
group=floor((_n_-1)/5);
run;
proc transpose data=oldv out=new name=id prefix=a;
by group;
var x;
id id;
run;
******************************************************
2:
%let itv=5;
%let oldvar=x;
data new;
set old;
array a{&itv};
retain a1-a&itv;
j=mod(_n_-1, &itv)+1;
if j=1 then do k=1 to dim(a); a[k]=. ; end;
a[j]=&oldvar;
if j=5 then output;
keep a1-a&itv;
run;
【在 z**o 的大作中提到】 : 有一个变量 : a1 : a2 : a3 : a4 : a5 : a6 : . : . : .
|
p***r 发帖数: 920 | 12 Is there any other way to reset to missing instead of using
a[k] =.
?
【在 o****o 的大作中提到】 : data old; : do x=1 to 100; : output; : end; : run; : ***************************************************** : 1: : data oldv/view=oldv; : set old; : id=mod(_n_-1, 5)+1;
|
o****o 发帖数: 8077 | 13 call missing(of a1-a5)
【在 p***r 的大作中提到】 : Is there any other way to reset to missing instead of using : a[k] =. : ?
|
p***r 发帖数: 920 | 14 thanks, good call
【在 o****o 的大作中提到】 : call missing(of a1-a5)
|