SAS中文论坛

 找回密码
 立即注册

扫一扫,访问微社区

查看: 3425|回复: 15
打印 上一主题 下一主题

[求助]还是数据查找输出问题

[复制链接]

49

主题

76

帖子

1462

积分

管理员

Rank: 9Rank: 9Rank: 9

积分
1462
楼主
 楼主| 发表于 2009-10-27 13:01:30 | 只看该作者

[求助]还是数据查找输出问题

[code:2lbp5w1x]
data aa;
   input name$ quote_id$;
   datalines;
AA a000
AA a000
AA a001
AA a002
BB b003
BB b003
BB b003
BB b004
BB b005
CC c006
CC c007
CC c008
CC c008
CC c008
;

[/code:2lbp5w1x]

这样的数据,我想把含有同样quote_id的数据输出,即:
AA a000
AA a000
BB b003
BB b003
BB b003
CC c008
CC c008
CC c008

我写了下面的code,希望能把同样quote_id的数据标记出来,但是怎么也编译不过去,请大牛给看看。万分感谢!

[code:2lbp5w1x]
proc sort data=aa;
        by quote_id;

data aa;
       
        do _n_ = 1 by 1 until (last.quote_id);
                set aa;
                by quote_id;
                if first.quote_id then lagq=.;
                else lagq=lag(quote_id);
        end;
        if quote_id=lagq then qflag=1;
        else qflag=0;
run;

[/code:2lbp5w1x]
回复 支持 反对

使用道具 举报

49

主题

76

帖子

1462

积分

管理员

Rank: 9Rank: 9Rank: 9

积分
1462
沙发
 楼主| 发表于 2009-10-27 13:54:39 | 只看该作者

Re: [求助]还是数据查找输出问题

[code:1aumtnjr]proc sort data=aa;
   by name quote_id;
run;

data aa;
   set aa;
   by name quote_id;
   if first.quote_id and last.quote_id then delete;
run;[/code:1aumtnjr]
回复 支持 反对

使用道具 举报

49

主题

76

帖子

1462

积分

管理员

Rank: 9Rank: 9Rank: 9

积分
1462
板凳
 楼主| 发表于 2009-10-27 14:40:11 | 只看该作者

Re: [求助]还是数据查找输出问题

wow,原来如此简单,谢谢!!!

不过请问这个first.xxx 在系统中会是个什么值?last.xxx呢?

if first.xxx and last.xxx 可以表示成first.xxx=last.xxx吗?

再次感谢!!!
回复 支持 反对

使用道具 举报

49

主题

76

帖子

1462

积分

管理员

Rank: 9Rank: 9Rank: 9

积分
1462
地板
 楼主| 发表于 2009-10-27 17:17:58 | 只看该作者

继续求助Re: [求助]还是数据查找输出问题

如果输出的data set中还有别的variable
比如:
name quote_id var1 var2
AA a000 4 5
AA a000 4 5
BB b003 2 3
BB b003 2 3
BB b003 . .
CC c008 1 1
CC c008 1 0
CC c008 . 0

我想找出后面var的不同之处,应该如何操作?如果用compare statement的话要把这个set 变成两个,但是有些比较是2组,有些比较的是3组,或者更多,这该怎么办?

谢谢!
回复 支持 反对

使用道具 举报

49

主题

76

帖子

1462

积分

管理员

Rank: 9Rank: 9Rank: 9

积分
1462
5#
 楼主| 发表于 2009-10-27 19:39:46 | 只看该作者

Re: [求助]还是数据查找输出问题

抛砖引玉
[code:1e44u4y7]data aa;
input name $ quote_id $ var1 var2;
cards;
AA a000 4 5
AA a000 4 5
BB b003 2 3
BB b003 2 3
BB b003 . .
CC c008 1 1
CC c008 1 0
CC c008 . 0
;
run;

proc sort data=aa;
   by name quote_id;
run;

data aa;
   set aa;
   by name quote_id;
   if first.quote_id and last.quote_id then delete;
run;



proc sort data=aa;by  name quote_id descending var1  descending var2;run;  /*以最大为参考值*/

data dif;
   set aa;
   by name quote_id;
   array var{*}  var1-var2;
   array f_var{*} f_var1-f_var2;
   array dif{*} dif1-dif2;
   retain f_var1-f_var2;
   if first.quote_id then do;
        do i=1 to 2;
        f_var(i)=var(i);
        dif(i)=0;/*只有0代表无差别*/
        end;
   end;
   else do;
        do i=1 to 2;
        if f_var(i)=. then dif(i)=0;
        else dif(i)=var(i)-f_var(i);/*与最大值的差别*/;
        end;
   end;
   drop i f_:;
run;
proc print;run;[/code:1e44u4y7]
回复 支持 反对

使用道具 举报

49

主题

76

帖子

1462

积分

管理员

Rank: 9Rank: 9Rank: 9

积分
1462
6#
 楼主| 发表于 2009-10-27 20:53:49 | 只看该作者

Re: [求助]还是数据查找输出问题

谢谢楼上牛人,现在又出现个问题。

实际上我有15个variable,其中有character的也有numeric,按照楼上的code,运行之后出现:

74   data dif;
75      set samequoteid;
76      by ticker quoteid;
77      array var{*}  var1-var15;
ERROR: All variables in array list must be the same type, i.e., all numeric or character.
ERROR: All variables in array list must be the same type, i.e., all numeric or character.
ERROR: All variables in array list must be the same type, i.e., all numeric or character.
ERROR: All variables in array list must be the same type, i.e., all numeric or character.
ERROR: All variables in array list must be the same type, i.e., all numeric or character.
ERROR: All variables in array list must be the same type, i.e., all numeric or character.
ERROR: All variables in array list must be the same type, i.e., all numeric or character.
ERROR: All variables in array list must be the same type, i.e., all numeric or character.
ERROR: All variables in array list must be the same type, i.e., all numeric or character.
ERROR: All variables in array list must be the same type, i.e., all numeric or character.
78      array f_var{*} f_var1-f_var15;
79      array dif{*} dif1-dif15;
80      retain f_var1-f_va15;
ERROR: Alphabetic prefixes for enumerated variables (f_var1-f_va15) are different.
81      if first.quoteid then do;
82           do i=1 to 15;
83           f_var(i)=var(i);
WARNING: The array var has the same name as a SAS-supplied or user-defined function.  Parenthesized
         references involving this name have been treated as array references and not function
         references.
84           dif(i)=0;/*??0?????*/
WARNING: The array dif has the same name as a SAS-supplied or user-defined function.  Parenthesized
         references involving this name have been treated as array references and not function
         references.
85           end;
86      end;
87      else do;
88           do i=1 to 15;
89           if f_var(i)=. then dif(i)=0;
90           else dif(i)=var(i)-f_var(i);/*???????*/;
91           end;
92      end;
93      drop i f_:;
94   run;

NOTE: Character values have been converted to numeric values at the places given by:
      (Line):(Column).
      83:9    90:21
NOTE: The SAS System stopped processing this step because of errors.


请指教......
回复 支持 反对

使用道具 举报

49

主题

76

帖子

1462

积分

管理员

Rank: 9Rank: 9Rank: 9

积分
1462
7#
 楼主| 发表于 2009-10-28 09:08:45 | 只看该作者

Re: [求助]还是数据查找输出问题

[quote="dengzi":2hf9xzd1]wow,原来如此简单,谢谢!!!

不过请问这个first.xxx 在系统中会是个什么值?last.xxx呢?

if first.xxx and last.xxx 可以表示成first.xxx=last.xxx吗?

再次感谢!!![/quote:2hf9xzd1]

首先你需要将数据集按照 XXX排序后,在data步中需要有BY XXX语句。sas读取数据时遇到xxx第一行,则first.xxx条件为真,data步赋first.xxx值为1,否则为0,last.xxx同理。

针对第二个问题,你的问题我没明白,我按我的理解使用LAG() function 作了一个例子,希望对你有用。
[code:2hf9xzd1]
data aa;
input name $ quote_id $ var1 var2;
cards;
AA a000 4 5
AA a000 4 5
BB b003 2 3
BB b003 2 3
BB b003 . .
CC c008 1 1
CC c008 1 0
CC c008 . 0
;
run;

proc sort data=aa;
   by name quote_id;
run;

data aa;
   set aa;
   by name quote_id;
   if first.quote_id and last.quote_id then delete;
run;

proc sort data=aa;by  name quote_id descending var1  descending var2;run;  /*以最大为参考值*/

data dif;
   set aa;
   by name quote_id;
   var1_lag=lag(var1);
   var2_lag=lag(var2);
   if first.quote_id then do;
           var1_lag=.;
           var2_lag=.;
           x=0;
   end;
   else do;
           if var1 ne var1_lag then x=1;
        else x=0;
   end;
run;
proc print;run;[/code:2hf9xzd1]
回复 支持 反对

使用道具 举报

49

主题

76

帖子

1462

积分

管理员

Rank: 9Rank: 9Rank: 9

积分
1462
8#
 楼主| 发表于 2009-10-28 10:26:31 | 只看该作者

Re: [求助]还是数据查找输出问题

[quote:subnonyz]我想找出后面var的不同之处,应该如何操作?如果用compare statement的话要把这个set 变成两个,但是有些比较是2组,有些比较的是3组,或者更多,这该怎么办?[/quote:subnonyz]
我也觉得第二个问题不太明白,特别是对于字符变量,不同之处是指?
1. 只需知道同组‘有’ 还是 ‘没有’ 差别?
2. 除了知道是否有差别,还需知道差别大还是小(可以用complev函数)。
如aXc         abc                        差别1 个字符
     aXbZc       abc                       差别2 个字符
3. 找出详细的差别位置(如果是2组可用compare,2组以上估计比较麻烦)。
如aXc         abc                         -x-  
     aXbZc       abc                        -xxxx  

最好给一个例子详细说明一下。
回复 支持 反对

使用道具 举报

49

主题

76

帖子

1462

积分

管理员

Rank: 9Rank: 9Rank: 9

积分
1462
9#
 楼主| 发表于 2009-10-28 10:47:40 | 只看该作者

Re: [求助]还是数据查找输出问题

第二个问题实际上我就是想知道有没有duplicate的数据,因为suppose一个quote_id就应该对应有一组数,可是现在出现了同一个quote_id对应了2-3组数的情况,我就想分析一下这些重复的部分是否完全相同,或者说哪里有差别,是否记录错误。

lag也是可以的,不过我有15个variable,按个写下来太麻烦了。而且数据形式不同,有的是character,有的是numeric,所以上面那个数组的形式也行不通。

有什么好办法?
回复 支持 反对

使用道具 举报

49

主题

76

帖子

1462

积分

管理员

Rank: 9Rank: 9Rank: 9

积分
1462
10#
 楼主| 发表于 2009-10-28 11:27:32 | 只看该作者

Re: [求助]还是数据查找输出问题

如果不太讲究,可以用proc compare将就一下
[code:3psjwi00]data raw;
        input name $ quote_id $ var1 var2 var3 $;
datalines;
AA a000 4 5        A
AA a000 4 5        V
BB b003 2 3        A
BB b003 2 3        A
BB b003 . .        V
CC c008 1 1        A
CC c008 1 0        A
CC c008 . 0        V
;

proc sort data=raw;
        by name quote_id;
run;

data std;
        set raw;
        by name quote_id;
        if first.quote_id then output;
run;

data std;
        merge std raw(keep=name quote_id);
        by name quote_id;
run;

proc compare base=std compare=raw nosummary;
        by name quote_id;
run;[/code:3psjwi00]
回复 支持 反对

使用道具 举报

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

本版积分规则

QQ|小黑屋|手机版|Archiver|SAS中文论坛  

GMT+8, 2026-2-6 07:58 , Processed in 0.070079 second(s), 20 queries .

Powered by Discuz! X3.2

© 2001-2013 Comsenz Inc.

快速回复 返回顶部 返回列表