w*****y 发帖数: 15 | 1 我有个数据,A,B两列是原始数据,C和D列是我希望生成的数据
A B C D
abc 01 1 1
abc 02 1 1
abc 01 2 2
abc 02 2 2
abc 03 2 2
abc 01 3 0
abc 01 4 0
abc 01 5 1
abc 02 5 1
def 01 1 0
def 01 2 3
def 02 2 3
def 03 2 3
def 04 2 3
def 01 3 1
def 02 3 1
ghi 01 1 4
ghi 02 1 4
ghi 03 1 4
ghi 04 1 4
ghi 05 1 4
ghi 01 2 2
ghi 02 |
p********a 发帖数: 5352 | 2 data df1;
set df;
by a;
retain c;
if first.a and b="01" then C=1;
if not first.a and b="01" then c+1;
run;
proc sql;
create table df2 as
select df1.*,g.D from df1 left join
(select a,c,count(*)-1 as D from df1
group by a,c) g
on df1.a=g.a and df1.c=g.c;
quit;
proc print data=df2;
run; |
w*****y 发帖数: 15 | 3 多谢长工!我去试试。
【在 p********a 的大作中提到】 : data df1; : set df; : by a; : retain c; : if first.a and b="01" then C=1; : if not first.a and b="01" then c+1; : run; : proc sql; : create table df2 as : select df1.*,g.D from df1 left join
|
i**f 发帖数: 1195 | 4 版大还是牛
我今天下午看到这个帖子了,在sas里小run了一下,只弄出个c,老想着能不能同一个
step算出d,未果,赶时间就先走了
【在 p********a 的大作中提到】 : data df1; : set df; : by a; : retain c; : if first.a and b="01" then C=1; : if not first.a and b="01" then c+1; : run; : proc sql; : create table df2 as : select df1.*,g.D from df1 left join
|
w*****y 发帖数: 15 | 5 原来是版大!我这瞎墨眼大晕头。。。失敬失敬。
再请教个提外话,像俺这样的初学者,用sas解决数据问题的时候,总是先有像我今天
的问题这样的逻辑,有没有什么可以推荐的教材/书籍,比较直观的以例子的方式教菜
鸟编程的啊?
再次感谢!!! |