SAS中文论坛

标题: [求助]还是数据查找输出问题 [打印本页]

作者: shiyiming    时间: 2009-10-27 13:01
标题: [求助]还是数据查找输出问题
[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]
作者: shiyiming    时间: 2009-10-27 13:54
标题: 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]
作者: shiyiming    时间: 2009-10-27 14:40
标题: Re: [求助]还是数据查找输出问题
wow,原来如此简单,谢谢!!!

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

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

再次感谢!!!
作者: shiyiming    时间: 2009-10-27 17:17
标题: 继续求助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组,或者更多,这该怎么办?

谢谢!
作者: shiyiming    时间: 2009-10-27 19:39
标题: 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]
作者: shiyiming    时间: 2009-10-27 20:53
标题: 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.


请指教......
作者: shiyiming    时间: 2009-10-28 09:08
标题: 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]
作者: shiyiming    时间: 2009-10-28 10:26
标题: 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  

最好给一个例子详细说明一下。
作者: shiyiming    时间: 2009-10-28 10:47
标题: Re: [求助]还是数据查找输出问题
第二个问题实际上我就是想知道有没有duplicate的数据,因为suppose一个quote_id就应该对应有一组数,可是现在出现了同一个quote_id对应了2-3组数的情况,我就想分析一下这些重复的部分是否完全相同,或者说哪里有差别,是否记录错误。

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

有什么好办法?
作者: shiyiming    时间: 2009-10-28 11:27
标题: 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]
作者: shiyiming    时间: 2009-10-28 11:53
标题: Re: [求助]还是数据查找输出问题
要在SAS中同时运算字符和数值型变量,我现在所知道方法可能只有用宏。但是针对15个变量进行重复操作,可能要降低运算速度。HOPEWELL的方法可能是目前最适合的方法了。
作者: shiyiming    时间: 2009-10-28 14:01
标题: Re: [求助]还是数据查找输出问题
hopewell 大侠巧妙的用proc compare 实现了多条observations之间的比较,应该是最好的解决方法了。如果要找出字符型变量详细的差别位置,可以把compare的结果output出来打印。
作者: shiyiming    时间: 2009-10-28 21:15
标题: Re: [求助]还是数据查找输出问题
Do not look for non-standard solutions for standard questions unless you get a neater one. Keep it simple.
[code:183tos1c]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 sql;
    select distinct *,count(*) as dupNum
    from raw
    group by name,quote_id,var1,var2,var3
    ;[/code:183tos1c]
作者: shiyiming    时间: 2009-10-28 22:03
标题: Re: [求助]还是数据查找输出问题
<!-- s:D --><img src="{SMILIES_PATH}/icon_biggrin.gif" alt=":D" title="Very Happy" /><!-- s:D -->
作者: shiyiming    时间: 2009-10-30 14:17
标题: Re: [求助]还是数据查找输出问题
大虾们太厉害了,让我一一运行一下看看有没有什么差别。

向大家学习!!!
作者: shiyiming    时间: 2009-12-3 00:07
标题: Re: [求助]还是数据查找输出问题
这个论坛的讨论氛围太好了,新手学习啦!




欢迎光临 SAS中文论坛 (https://mysas.net/forum/) Powered by Discuz! X3.2