p********r 发帖数: 1465 | 1 比如我现在有这样的一个数据
A B C D
1
7
3 3
2 5
5
5 3
4 5
5 8
2 3
1
用什么样的方法,可以把它变成
A B C D
1 3 3 5
7 5 4 8
3 5 5 3
2 5 2 1
多谢! |
k*******a 发帖数: 772 | 2 比较笨的方法。。。
data a;
input A 4. B 4. C 4. D 4. ;
datalines;
1
7
3 3
2 5
5
5 3
4 5
5 8
2 3
1
;
proc print;run;
data a;
set a;
if A ne . then ranka+1;
if B ne . then rankb+1;
if C ne . then rankc+1;
if D ne . then rankd+1;
run;
proc sql;
create table new as
select A, B, C, D
from
(select A, ranka from a where A ne . ),
(select B, rankb from a where B ne .) ,
(select C, rankc from a where C ne . ),
(select D, rankd from a where D ne . )
where ranka=rankb=rankc=rankd;
quit;
proc print data=new;run; |
R*********i 发帖数: 7643 | 3 data test;
input A 4. B 4. C 4. D 4. ;
datalines;
1
7
3 3
2 5
5
5 3
4 5
5 8
2 3
1
;
run;
data test1;
merge test(keep=a where=(a>.))
test(keep=b where=(b>.))
test(keep=c where=(c>.))
test(keep=d where=(d>.));
run;
However there would be a warning message. |
h******e 发帖数: 1791 | 4 lag或dif function行不行?没细想。
【在 p********r 的大作中提到】![](/moin_static193/solenoid/img/up.png) : 比如我现在有这样的一个数据 : A B C D : 1 : 7 : 3 3 : 2 5 : 5 : 5 3 : 4 5 : 5 8
|
s******y 发帖数: 352 | 5 DATA HAVE;
INFILE CARDS TRUNCOVER;
INPUT A B C D;
ROWID=_N_;
CARDS;
1
7
3 3
2 5
. 5
. 5 3
. . 4 5
. . 5 8
. . 2 3
. . . 1
;
RUN;
PROC TRANSPOSE DATA=HAVE OUT=HAVE1(WHERE=(NOT MISSING(COL1)));
BY ROWID;
VAR A B C D;
RUN;
PROC SORT DATA=HAVE1;
BY _NAME_ ROWID;
RUN;
DATA HAVE2;
SET HAVE1;
BY _NAME_;
ID+1-(FIRST._NAME_)*ID;
RUN;
PROC SORT DATA=HAVE2;
BY ID _NAME_ ;
RUN;
PROC TRANSPOSE DATA=HAVE2 OUT=WANT(DROP=_NAME_ ID);
BY ID ;
VAR COL1;
ID _NAME_;
RUN;
【在 p********r 的大作中提到】![](/moin_static193/solenoid/img/up.png) : 比如我现在有这样的一个数据 : A B C D : 1 : 7 : 3 3 : 2 5 : 5 : 5 3 : 4 5 : 5 8
|
s******y 发帖数: 352 | 6 Your solution is very nice.
I would do the same thing as you proposed, if number of variables are less
than 10.
As to the warning message, you can suppress the warnings by using option
mergenoby=NOWARN.
you can do something like this.
1. save the current option setting.
%let oldop=%sysfunc(getoption(mergenoby));
2. run your datasteps
data XXXX;
...
...
run;
3. reset the options.
options mergenoby=&oldop;
【在 R*********i 的大作中提到】![](/moin_static193/solenoid/img/up.png) : data test; : input A 4. B 4. C 4. D 4. ; : datalines; : 1 : 7 : 3 3 : 2 5 : 5 : 5 3 : 4 5
|
p********r 发帖数: 1465 | 7 多谢各位高手啊!
要是我有很多的variable呢?比如100个,V1 - V100;
加do loop吧?
可是我试了一下,那个merge (keep) 的语句里,好像不能用array做variable啊……
那应该怎么办呢? |
y****n 发帖数: 46 | 8 data a;
input A 4. B 4. C 4. D 4. ;
datalines;
1
7
3 3
2 5
5
5 3
4 5
5 8
2 3
1
;
run;
proc transpose data=a out=b prefix=n;
var a b c d;
run;
data c;
array xx{10} n1-n10;
set b;
rec=0;
do i=1 to 10;
if xx(i)>=0 then do;
new=xx(i);
rec+1;
output;
end;
end;
keep rec _name_ new;
run;
proc sort data=c;
by rec _name_;
run;
proc transpose data=c out=d(drop=_name_ rec);
by rec;
var new;
id _name_;
run; |
D******n 发帖数: 2836 | 9 macro
【在 p********r 的大作中提到】![](/moin_static193/solenoid/img/up.png) : 多谢各位高手啊! : 要是我有很多的variable呢?比如100个,V1 - V100; : 加do loop吧? : 可是我试了一下,那个merge (keep) 的语句里,好像不能用array做variable啊…… : 那应该怎么办呢?
|