楼主: BraveMadMan
2454 9

[问答] 保留相邻的最大(小)值 [推广有奖]

  • 1关注
  • 6粉丝

已卖:97份资源

讲师

16%

还不是VIP/贵宾

-

威望
0
论坛币
966 个
通用积分
6.3763
学术水平
26 点
热心指数
42 点
信用等级
10 点
经验
7812 点
帖子
300
精华
0
在线时间
552 小时
注册时间
2005-8-11
最后登录
2021-5-20

楼主
BraveMadMan 发表于 2011-1-24 14:42:34 |AI写论文
20论坛币
有如图的数据(SAS数据格式见附件):
have.png

现在high列中1和-1不是交叉出现的数据点,并根据p_sm的值重新定义high。

具体来讲,我要在high列两个或更多相邻为1的,取取p_sm最的一个,将high重新定义为1,其他的定义为. (missing); 类似,我要在high列两个或更多相邻为-1的,取出p_sm最的一个,将high重新定义为-1,其他的定义为 . (missing)

比如,在附件的数据里(以从第一个到第19个观测值为例),要将第2,13,15,19个观测值改为 “.”, 其他不变。

请问在SAS如何实现。

have.rar
下载链接: https://bbs.pinggu.org/a-840252.html

1.36 KB

本附件包括:

  • have.sas7bdat

关键词:missing High SSIN Miss 数据格式 相邻

本帖被以下文库推荐

Don't get lost in technical details. What is the big picture?

沙发
elek.me 发表于 2011-1-24 14:42:35
  1. * method 1;
  2. data want;
  3.         retain min max;
  4.         do until (last.high);
  5.                 set tmp1.have;
  6.                 by high notsorted;
  7.                 if first.high then do;
  8.                         min=p_sm;
  9.                         max=p_sm;
  10.                 end;
  11.                 else do;
  12.                         min=min(min,p_sm);
  13.                         max=max(max,p_sm);
  14.                 end;
  15.         end;
  16.         do until (last.high);
  17.                 set tmp1.have;
  18.                 by high notsorted;
  19.                 if high=1 and p_sm^=max then high=.;
  20.                 if high=-1 and p_sm^=min then high=.;
  21.                 output;
  22.         end;
  23. run;
复制代码
已有 2 人评分学术水平 热心指数 信用等级 收起 理由
peijiamei + 1 + 2 观点有启发
BraveMadMan + 1 + 1 + 1 多谢精彩程序

总评分: 学术水平 + 2  热心指数 + 3  信用等级 + 1   查看全部评分

我的博客: http://elek.me/sas
联系我: http://about.me/elek

藤椅
baoaibaobao 发表于 2011-1-24 15:58:56
2# elek.me 一直没彻底明白notsorted的使用,看了这个代码,彻底明白了。

板凳
elek.me 发表于 2011-1-24 16:11:44
  1. * method 2;
  2. data tmp1;
  3.         set tmp1.have;
  4.         obs=_n_;
  5. run;
  6. proc means data=tmp1 noprint idmin;
  7.         by high notsorted;
  8.         var p_sm;
  9.         id  p_sm obs;
  10.         output out=tmp2(drop=_freq_ _type_) min(p_sm)=min max(p_sm)=max;
  11. run;
  12. proc means data=tmp1 noprint;
  13.         by high notsorted;
  14.         var p_sm;
  15.         id  p_sm obs;
  16.         output out=tmp3(drop=_freq_ _type_) min(p_sm)=min max(p_sm)=max;
  17. run;
  18. data want;
  19.         merge tmp1 tmp2 tmp3;
  20.         by obs;
  21.         if high=1 and max^=p_sm then high=.;
  22.         if high=-1 and min^=p_sm then high=.;
  23.         drop min max obs;
  24. run;
复制代码
已有 3 人评分学术水平 热心指数 信用等级 收起 理由
pinseng + 1 + 1 + 1 很不错的程序
peijiamei + 2 + 2 观点有启发
BraveMadMan + 1 + 1 + 1 多谢精彩程序

总评分: 学术水平 + 4  热心指数 + 4  信用等级 + 2   查看全部评分

我的博客: http://elek.me/sas
联系我: http://about.me/elek

报纸
elek.me 发表于 2011-1-24 16:12:35
baoaibaobao 发表于 2011-1-24 15:58
2# elek.me 一直没彻底明白notsorted的使用,看了这个代码,彻底明白了。
呵呵。 其实挺简单。
by 和 class 在sas里的关系,有些微妙。
我的博客: http://elek.me/sas
联系我: http://about.me/elek

地板
elek.me 发表于 2011-1-24 16:15:18
elek.me 发表于 2011-1-24 16:11
  1. * method 2;
  2. data tmp1;
  3.         set tmp1.have;
  4.         obs=_n_;
  5. run;
  6. proc means data=tmp1 noprint idmin;
  7.         by high notsorted;
  8.         var p_sm;
  9.         id  p_sm obs;
  10.         output out=tmp2(drop=_freq_ _type_) min(p_sm)=min max(p_sm)=max;
  11. run;
  12. proc means data=tmp1 noprint;
  13.         by high notsorted;
  14.         var p_sm;
  15.         id  p_sm obs;
  16.         output out=tmp3(drop=_freq_ _type_) min(p_sm)=min max(p_sm)=max;
  17. run;
  18. data want;
  19.         merge tmp1 tmp2 tmp3;
  20.         by obs;
  21.         if high=1 and max^=p_sm then high=.;
  22.         if high=-1 and min^=p_sm then high=.;
  23.         drop min max obs;
  24. run;
复制代码
楼主的数据里 date 这个变量,看上去是唯一的而且是排序的,如果确实是的话,就没必要用obs这个变量了,程序可以更简练。
我的博客: http://elek.me/sas
联系我: http://about.me/elek

7
BraveMadMan 发表于 2011-1-24 21:29:12
baoaibaobao 发表于 2011-1-24 15:58
2# elek.me 一直没彻底明白notsorted的使用,看了这个代码,彻底明白了。
确实,又学了一招
Don't get lost in technical details. What is the big picture?

8
pinseng 发表于 2011-1-26 12:55:55
支持elek.me
多多学习

9
sber 发表于 2011-1-29 23:04:11
我尝试着写了一个很笨拙的程序来解决这个问题,由于太笨拙了不怒好意思拿出手,过完年再给ELEK师兄看看,呵呵

10
shenliang_111 发表于 2011-12-30 21:35:55
  1. data have2;
  2. set have;
  3. by high notsorted;
  4. if first.high then group+1;
  5. run;
  6. data have3(drop=max min cnt type);
  7.   max=-999;min=999;
  8.   do _n_=1 by 1 until(last.group);
  9.      set have2;
  10.       by group notsorted;
  11.       max=max(p_sm,max);
  12.           min=min(p_sm,min);
  13.   end;
  14.   cnt=_n_;
  15.   type=high;
  16.   do _n_=1 to _n_;
  17.    set have2;
  18.       if cnt=1 then output;
  19.          else if type=1 then do;
  20.                  high=ifn(p_sm=max,1,.);output;
  21.                   end;       
  22.              else do;high=ifn(p_sm=min,1,.);output;end;
  23.   end;
  24. run;

  25.   

  26.    
复制代码

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

本版微信群
加好友,备注cda
拉您进交流群
GMT+8, 2025-12-5 18:19