全部版块 我的主页
论坛 数据科学与人工智能 数据分析与数据科学 SAS专版
7697 1
2013-03-18
请教大家,在同一张数据表中retain语句等如何实现下面逻辑关系.如何用SAS实现即:
end1 用户A/B的A列观测数;
end2 用户A/B的B列观测数;
if first.id then do;
for i = 1 to end1
   for j = 1 to end2
   /*如果这里A列的变量值小于等于B列的变量观测值时*/
        if A(i)<=B(j) then do 提取出A_date存放在result_A,B_date存放在result_B中;
        /*否则按照j变量循环直到满足条件跳出,然后在按照变量i循环*/
   end;
end;
多谢各位高手了哈~
期望结果如下:结果是result_A和result_B列
ID

A_date

A

B_date

B

result_A

result_B

A

2011/1/1

10

2011/1/1

10

2011/1/1

2011/1/1

A

2011/2/1

20

2011/2/3

20

2011/2/1

2011/2/3

A

2011/3/1

40

2011/3/6

30

2011/3/1

2011/4/20

A

2011/4/1

40

2011/4/20

50

2011/4/1

2011/4/20

A

2011/5/1

50

2011/5/23

60

2011/5/1

2011/5/23

A

2011/6/1

60

2011/7/10

70

2011/6/1

2011/7/10

A

2011/7/1

70

2011/8/11

80

2011/7/1

2011/8/11

A

2011/8/1

80

2011/8/1

2011/8/11

B

2010/3/4

100

2010/3/3

100

2010/3/4

2010/3/3

B

2010/4/4

200

2010/4/3

100

2010/4/4

2010/5/3

B

2010/5/4

300

2010/5/3

300

2010/5/4

2010/5/3

B

2010/6/4

400

2010/8/2

400

2010/6/4

2010/8/2

B

2010/7/4

500

2010/8/9

500

2010/7/4

2010/8/9

B

2010/8/4

600

2010/9/1

600

2010/8/4

2010/9/1




二维码

扫码加我 拉你入群

请注明:姓名-公司-职位

以便审核进群资格,未注明则拒绝

全部回复
2013-3-19 07:11:03
Try this.

data a;
infile datalines missover;
input ID $ A_date : $10. A  B_date : $10. B;
datalines;
A 2011/1/1 10 2011/1/1         10       
A 2011/2/1 20 2011/2/3         20
A 2011/3/1 40 2011/3/6         30
A 2011/4/1 40 2011/4/20 50
A 2011/5/1 50 2011/5/23 60
A 2011/6/1 60 2011/7/10 70
A 2011/7/1 70 2011/8/11 80
A 2011/8/1 80
B 2010/3/4 100 2010/3/3 100
B 2010/4/4 200 2010/4/3 100
B 2010/5/4 300 2010/5/3 300
B 2010/6/4 400 2010/8/2 400
B 2010/7/4 500 2010/8/9 500
B 2010/8/4 600 2010/9/1 600
; run;

proc sort data=a; by id A_date; run;

data b;
    set a;
        if A <= B then do;    /* the condition */
              result_A=A_date;
              result_B=b_date;
                    end;
           else result_A = A_date;
       if missing(b_date) then Result_B=a_date;
       by id;
run;

proc sort data=b;
     by id descending a_date; /* reverse the order */
run;

data c;           /* fill the missing value */
   set b;
   lag_Result_b = lag(result_b);
   if missing(result_b) then result_b=lag_result_b;
   drop lag_result_b;
run;

proc sort data=c out=result;   /* back to normal */
    by id a_date;
run;

proc print data=result; title 'result'; run;
二维码

扫码加我 拉你入群

请注明:姓名-公司-职位

以便审核进群资格,未注明则拒绝

相关推荐
栏目导航
热门文章
推荐文章

说点什么

分享

扫码加好友,拉您进群
各岗位、行业、专业交流群