l*******n 发帖数: 13 | 1 ASK:
How to use sas to get data B from data A?
That is, for each observation in B, I just need the order of the number at each level of A
Thanks
Data A
A B
1 1
1 1
1 3
1 3
1 5
1 23
2 4
2 4
2 6
2 6
2 12
2 22
Data B
A B
1 1
1 1
1 2
1 2
1 3
1 4
2 1
2 1
2 2
2 2
2 3
2 4 |
l*******n 发帖数: 13 | 2 In other words,
how to calculate the order of a numeric vector
(1 , 2, 2,5, 8,...)
to
(1, 2, 2, 3,4,...)
Doing this with R is a snap, but how with sas? |
q**j 发帖数: 10612 | |
l*******n 发帖数: 13 | 4 data mydata;
input A B;
datalines;
1 1
1 1
1 3
1 3
1 5
1 23
2 4
2 4
2 6
2 6
2 12
2 22
;
proc rank data=mydata out=results ties=low;
by A;
var B;
ranks NewB ;
run;
proc print data=results;
by A;
run;
BUT i got this, why? |
q**j 发帖数: 10612 | |
l*******n 发帖数: 13 | 6 I do want to get the result like
1 1 2 2 3 3....
How to do that? |
q**j 发帖数: 10612 | 7 remove redundant values, rank, merge back. |
l*******n 发帖数: 13 | 8 it seems that SAS makes many things harder than expected.
I still feel afraid of using SAS even though I have many years of
programming experiences with java, c#,delphi,vb.net,php,r...
but what can we do if they do not go opensource? |
D******n 发帖数: 2836 | 9 use R
【在 l*******n 的大作中提到】 : it seems that SAS makes many things harder than expected. : I still feel afraid of using SAS even though I have many years of : programming experiences with java, c#,delphi,vb.net,php,r... : but what can we do if they do not go opensource?
|
o****o 发帖数: 8077 | 10 data mydata;
input A B;
datalines;
1 1
1 1
1 3
1 3
1 5
1 23
2 4
2 4
2 6
2 6
2 12
2 22
;
run;
proc freq data=mydata noprint order=internal;
table A*B/out=order(drop=COUNT PERCENT);
run;
data order;
set order; by A;
retain Order;
if first.A then Order=1; else Order+1;
run;
proc sql;
create table outdata as
select a.*, b.Order
from mydata as a join order as b
on a.A=b.A & a.B=b.B
order by a.A, a.B
;
quit; |
|
|
l*******n 发帖数: 13 | 11 you rock!
how many years have you been using SAS? :) |
m***6 发帖数: 884 | 12 What about just manually count?
data mydata;
input A B;
datalines;
1 1
1 1
1 3
1 3
1 5
1 23
2 4
2 4
2 6
2 6
2 12
2 22
;
run;
proc sort data = mydata; by A B; run;
data d; retain a b x; set mydata; by a b;
if _n_=1 then x=0; if first.b then x=x+1;
run; |
l*******n 发帖数: 13 | 13 Nice try, but the result is not what I want.
Actually, There may exist several stratification variables A1 A2...
A1 A2 B... |
x***x 发帖数: 3401 | 14 proc sort and followed by a data step utilizing _n_ variable |
x**m 发帖数: 941 | 15 Full code:
proc sql;
create table seq as
select distinct a, b
from A
order by a, b;
run;
data seq2;
set seq;
by a b;
I+1;
if first.a then I=1;
run;
proc sql;
create table B as
select x.a, y.i
from A as x, seq2 as y
where x.a=y.a and x.b=y.b;
quit; |