楼主: fanzai1hao
2667 7

如何提取某些已找出观测的所有下一观测数据? [推广有奖]

  • 0关注
  • 38粉丝

已卖:3544份资源

讲师

61%

还不是VIP/贵宾

-

威望
0
论坛币
7228 个
通用积分
19.7225
学术水平
17 点
热心指数
38 点
信用等级
15 点
经验
33245 点
帖子
189
精华
0
在线时间
676 小时
注册时间
2010-5-14
最后登录
2025-12-24

楼主
fanzai1hao 学生认证  发表于 2015-4-6 11:31:02 |AI写论文
10论坛币
data test;input date price;
cards;
2013 12
2014 10
2015 9
2016 10
2017 6
2018 11
2019 10
2020 8
2021 9
;
run;
随便举个例子,问题很简单。比如我现在找出了所有价格等于10的观测纪录,现在目的在于找出这些观测的下一个date观测纪录,该用什么函数?请大神们赐教!





最佳答案

jl60156 查看完整内容

data want; _n_ ++ 1; if _n_
关键词:如何提取 cards Price Input date 如何

沙发
jl60156 发表于 2015-4-6 11:31:03
data want;
_n_ ++ 1;
if _n_ <= n then do;
set test point=_n_;
next_date = date;
end;
set test nobs = n;
if price ne 10 then next_date=.;
run;
已有 1 人评分论坛币 收起 理由
admin_kefu + 20 热心帮助其他会员

总评分: 论坛币 + 20   查看全部评分

藤椅
fanzai1hao 学生认证  发表于 2015-4-6 15:46:41
jl60156 发表于 2015-4-6 12:17
data want;
_n_ ++ 1;
if _n_
好的这步懂了,但如果下一个date的price为缺失值. 时,想继续寻找直到下一个price的值不是缺失值的date,这一步如何做呢?eg。
data test;                                                                                                                              
input date price;                                                                                                                       
cards;                                                                                                                                 
1991 23                                                                                                                                 
1992 10                                                                                                                                 
1993 11                                                                                                                                 
1994 10                                                                                                                                 
1995 15                                                                                                                                 
1996 10                                                                                                                                 
1997 .                                                                                                                                 
1998 .
1999 12                                                                                                                                 
;                                                                                                                                       
run;


板凳
jl60156 发表于 2015-4-6 20:55:07
fanzai1hao 发表于 2015-4-6 15:46
好的这步懂了,但如果下一个date的price为缺失值. 时,想继续寻找直到下一个price的值不是缺失值的date, ...
data test1;
        set test;
        if missing(price) then delete;
run;
data want;
_n_ ++ 1;
if _n_ <= n then do;
set test1 point=_n_;
next_date = date;
end;
set test1 nobs = n;
if price ne 10 then next_date=.;
run;
data want;
        merge test want;
        by date price;
run;

报纸
fanzai1hao 学生认证  发表于 2015-4-7 16:20:30
jl60156 发表于 2015-4-6 20:55
data test1;
        set test;
        if missing(price) then delete;
好的,非常感谢~~~~~~~~~

地板
fanzai1hao 学生认证  发表于 2015-4-7 16:29:05
jl60156 发表于 2015-4-6 20:55
data test1;
        set test;
        if missing(price) then delete;
那如果下一个值为10继续跳过,再跳到直到下一个值不为10的观测,该怎么写呢,现在不可能把观测为10的数据都删了吧。。。

7
jl60156 发表于 2015-4-7 21:24:18
fanzai1hao 发表于 2015-4-7 16:29
那如果下一个值为10继续跳过,再跳到直到下一个值不为10的观测,该怎么写呢,现在不可能把观测为10的数据 ...
data test1;
        set test;
        retain flg;
        if price=10 and lag(price) ne 10 then do;
                cnt1+1;
                flg=0;
        end;
        if price not in (10 .)  then do;
                        if flg=0 then cnt2+1;
                        flg+1;
        end;
run;
proc sql noprint;
        create table want as
        select a.*, c.date as next_date
        from test a left join
        (select * from test1 where price=10) b
        on a.date=b.date and a.price=b.price
        left join
        (select * from test1 where flg=1) c
        on b.cnt1=c.cnt2
        order by date;
quit;

8
fanzai1hao 学生认证  发表于 2015-4-8 08:45:39 来自手机
jl60156 发表于 2015-4-7 21:24
data test1;
        set test;
        retain flg;
好的,谢谢大神~~~

您需要登录后才可以回帖 登录 | 我要注册

本版微信群
加好友,备注cda
拉您进交流群
GMT+8, 2026-1-6 12:35