楼主: Tigflanker
1197 5

[问答] 求助观测汇总连接问题 [推广有奖]

  • 8关注
  • 18粉丝

副教授

49%

还不是VIP/贵宾

-

威望
0
论坛币
2321 个
通用积分
9.8528
学术水平
179 点
热心指数
194 点
信用等级
167 点
经验
27443 点
帖子
622
精华
0
在线时间
851 小时
注册时间
2011-3-27
最后登录
2023-5-14

100论坛币
有如下数据集:

data a;
if _n_<8 then g=1;else g=2;
input x $ @@;
y=dif(x);
cards;
07 12 13 14 15 19 22 23 24 29 30 31 32 36
;run;

想得到如下效果:g=1 result='07,12-15,19,22' ; g=2 result='23-24,29-32,36'
意思就是按照g来分组,将x进行汇总,得到的字符串如有连号,用‘-’连接。

问题貌似不难,但是想了一个晚上,很受打击。。。

貌似按照y=dif(x)可以算,但是感觉写出来会很啰嗦,不知有没有更简洁的办法,我记得前段时间貌似坛子里看到过。。

One way as follow:

data b;set a;
retain z 0;
if y^=1 then z+1;
run;


..... Still have a loooooong way to go .....

最佳答案

pobel 查看完整内容

data a; if _n_1 or last.g); set a; by g x; if first.g then result=x; if y=1 then do;flag=1; tmp=x;end; end; if flag=1 then do; result=catx("-",result,tmp); if not last.g or y>1 then result=catx(", ",result,x); end; else result=catx(", ",result,x); if last.g then output; keep g result; run;
关键词:Data 数据集

回帖推荐

hopewell 发表于6楼  查看完整内容

Bye SAS.
若有缘,能重聚。
沙发
pobel 在职认证  发表于 2013-10-8 23:58:40 |只看作者 |坛友微信交流群
data a;
if _n_<11 then g=1;else g=2;
input x $ @@;
y=dif(x);
cards;
07 08 09 12 13 14 15 19 21 22 23 24 29 30 31 32 36
;run;

data c;
   length result $100;
   retain result;
   flag=0;
   do until(y>1 or last.g);
      set a;
          by g x;
          if first.g then result=x;
          if y=1 then do;flag=1; tmp=x;end;
   end;
   if flag=1 then do;
        result=catx("-",result,tmp);
        if not last.g or y>1 then result=catx(", ",result,x);
          end;
   else result=catx(", ",result,x);

   if last.g then output;
   keep g result;
run;
和谐拯救危机

使用道具

藤椅
pobel 在职认证  发表于 2013-10-9 08:16:19 |只看作者 |坛友微信交流群
比较常规的方法:
data a;
if _n_<8 then g=1;else g=2;
input x $ @@;
y=dif(x);
cards;
07 12 13 14 15 19 22 23 24 29 30 31 32 36
;run;

data b;
   set a;
   by g;
   length result $100 tmp $8;
   retain flag 0 tmp result;
   if first.g then do; result=x; flag=0; tmp=" "; end;
   else do;
       if y ne 1 then do;
               if flag=0 then result=catx(", ",result,x);
                           else if flag=1 then result=catx(", ",catx("-",result,tmp),x);
                           flag=0;
                        end;
       else if y=1 then do;
               flag=1;
               tmp=x;
                           if last.g then result=catx("-",result,tmp);
            end;
   end;
   if last.g;
   keep g result;
run;

和谐拯救危机

使用道具

板凳
Tigflanker 发表于 2013-10-9 08:25:52 来自手机 |只看作者 |坛友微信交流群
pobel 发表于 2013-10-9 08:16
比较常规的方法:
data a;
if _n_
谢谢pobel,需要很强的逻辑。
Bye SAS.
若有缘,能重聚。

使用道具

报纸
tangliang0905 发表于 2013-10-9 11:38:37 |只看作者 |坛友微信交流群
pobel 发表于 2013-10-8 23:58
data a;
if _n_1 or last.g);
      set a;
有个问题有点疑惑,可能还是涉及到PDV的问题,

在第一个loop里面,set a在do until里面和在do until之外有什么区别呢?

能不能麻烦讲讲其中的逻辑呢?

是不是第一个do loop就是把flag的值按照y的值全部赋值好以后,然后再进行下面的if else的语句呢

其实,在看你的那个常规方法的时候,那种疑惑更大,
尤其是在那个else do的循环里面,能不能麻烦仔细讲讲那个循环里面的逻辑呢

使用道具

地板
hopewell 发表于 2013-10-9 11:52:15 |只看作者 |坛友微信交流群
  1. data a;
  2.     if _n_<8 then g=1;else g=2;
  3.     input x $ @@;
  4. cards;
  5. 07 12 13 14 15 19 22 23 24 29 30 31 32 36
  6. ;

  7. data b(keep=g result);
  8.     length result $100;
  9.     do until(last.g);
  10.         set a;
  11.         by g;
  12.         _x=input(x,best.);
  13.         if first.g or _x ne sum(lag(_x),1) then result=cats(catx(',',result,x),'-');
  14.         else result=prxchange(cats('s/-\d*$/-',x,'/'),1,strip(result));
  15.     end;
  16.     result=prxchange(cats('s/-(,)|-$/$1/'),-1,strip(result));
  17. run;
复制代码
已有 1 人评分学术水平 热心指数 信用等级 收起 理由
Tigflanker + 1 + 1 + 1 Excellent !! 3Q !!

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

观钓颇逾垂钓趣 种花何问看花谁

使用道具

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

本版微信群
加好友,备注cda
拉您进交流群

京ICP备16021002-2号 京B2-20170662号 京公网安备 11010802022788号 论坛法律顾问:王进律师 知识产权保护声明   免责及隐私声明

GMT+8, 2024-9-21 08:54