SAS中文论坛

标题: 请教:相同的code, 不同的结果(关于monotonic()函数) [打印本页]

作者: shiyiming    时间: 2009-7-2 17:33
标题: 请教:相同的code, 不同的结果(关于monotonic()函数)
有如下代码:

data a;
     input x;
         cards;
3
4
5
6
;
run;

proc sql;
     create table b as
          select mean(x) as meanx
          from a
          where monotonic() gt 2;

     create table c as
        select mean(x) as meanx
          from a
          where monotonic() gt 2;
run;


请问为什么数据集b为:
     meanx
         5.5

而数据集c却为:
    meanx
        4.5

请解释一下monotonic()的用法,谢谢!
作者: shiyiming    时间: 2009-7-2 20:31
标题: Re: 请教:相同的code, 不同的结果(关于monotonic()函数)
估计是由于关系型数据库里,记录的次序本来就不应当是一个合法的信息,所以很难确定哪一条是实际排在前面或者后面。这就是使用不该使用的信息要花的代价。如果次序是有用的就应该有一个显式的key variable.
作者: shiyiming    时间: 2009-7-3 08:57
标题: Re: 请教:相同的code, 不同的结果(关于monotonic()函数)
谢谢ahuige!

不过我感觉还不是次序的问题,应该是monotonic()的原因。

data a;
input x;
cards;
3
4
5
6
;
run;

proc sql;
create table b as
select x,monotonic() as n
from a;

create table c as
select x,monotonic() as n
from a;
quit;

table b:
x    n
-------
3    1
4    2
5    3
6    4

table c:

x    n
-------
3    5
4    6
5    7
6    8

所以表b里后面两条记录的monotonic()大于2,所以均值为5.5;
而表c里四条记录的monotonic()都大于2, 所以4.5是所有四个x:3,4,5,6的均值。
作者: shiyiming    时间: 2009-7-3 13:45
标题: Re: 请教:相同的code, 不同的结果(关于monotonic()函数)
好测试,看来是只用了一个PROC SQL的原因,你用两次PROC SQL分别操作应该就好了。
作者: shiyiming    时间: 2009-7-3 14:02
标题: Re: 请教:相同的code, 不同的结果(关于monotonic()函数)
对,用两个PROC SQL就没有问题了。
只是对monotonic()的处理过程还是不懂。请看下面的code:

data a;
     input x y;
         cards;
3 1
4 2
5 5
6 8
;
run;


proc sql;
     create table b as
            select x, monotonic() as n
                  from a;

     create table c as
            select x,y, monotonic() as n
                  from a;
quit;

=====================================
table b:
x    n
--------
3    1
4    2
5    3
6    4

table c:
x    y    n
-------------
3    1    1
4    2    2
5    5    3
6    8    4
======================================


proc sql;
     create table d as
            select mean(x) as meanx
                  from a
                  where monotonic() gt 2;

     create table e as
            select mean(x)as meanx,mean(y) as meany
                  from a
                  where monotonic() gt 2;
quit;

====================================
table d:

meanx
--------
5.5


table e:

meanx    meany
---------------------
   5.5      6.5
=============================================




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