SAS中文论坛

标题: 这样的PROC SQL 到底错在了哪里? [打印本页]

作者: shiyiming    时间: 2011-2-12 09:50
标题: 这样的PROC SQL 到底错在了哪里?
DATASET如下,
id        dat4
1        10/1/1999
1        11/1/1999
1        12/1/1999
1        1/1/2000
1        2/1/2000
1        3/1/2000
1        4/1/2000
1        5/1/2000
1        6/1/2000
2        10/1/1999
2        11/1/1999
2        12/1/1999
2        1/1/2000
2        2/1/2000
2        3/1/2000
2        4/1/2000
2        5/1/2000
2        6/1/2000
3        7/1/2002
3        8/1/2002
3        9/1/2002
3        10/1/2002
3        11/1/2002
3        12/1/2002
3        4/1/2007
3        5/1/2007
3        6/1/2007

我现在想PROC SQL 执行操作把ID=3的记录全部删除,因为ID=3时,月份是不连续的,但是程序怎么也不出结果,
不知道哪里出错了,请大家帮忙看看,

%macro delFun( );
%do i =1 %to 3;
    proc sql noprint;
    select id, count(*), intck('month',min(dat4),max(dat4))+1 into :id, <!-- s:o --><img src="{SMILIES_PATH}/icon_surprised.gif" alt=":o" title="Surprised" /><!-- s:o -->bsCount, :monthDiff
    from mydata
    group by id;
    %put id= &amp;id numObs =&amp;obsCount month diff = &amp;monthDiff;

/* delete the dataset which has the discontinous month */

%let a = %eval(&amp;obsCount);
%let b = %eval(&amp;monthDiff);

  %if &amp;a ^= &amp;b  %then %do;
    proc sql;
       delete from mydata
       where id = &amp;id;
    run;
        %end;
%put id =&amp;id a=%eval(&amp;obsCount) b=%eval(&amp;monthDiff);

%end;
%mend;
options symbolgen mprint mlogic;
%delFun();
<!-- s:) --><img src="{SMILIES_PATH}/icon_smile.gif" alt=":)" title="Smile" /><!-- s:) -->
作者: shiyiming    时间: 2011-2-12 13:01
标题: Re: 这样的PROC SQL 到底错在了哪里?
程序逻辑不怎么对吧。语法倒没有什么错误(?)你那个macro 变量ID应该总是第一个id,也就是&amp;id ==1 所以你当然什么也删不了(我没有sas试不了)。如果你把你那个创建macro变量的sql加以改进,不需要macro就可以删掉id=3了。比如
...group by id having count(*) ^=intck()...;(我还是没有试,你自己斟酌一下。)
作者: shiyiming    时间: 2011-2-12 23:06
标题: Re: 这样的PROC SQL 到底错在了哪里?
jingju11说得不错,每次迭代id总是1,因为每次过程中select的结果是三条记录,每次into变量,只会是第一条.
改得话,可以完全不用macro
如果想在已有程序上修改,也可以加一句话即可
select那段sql中group by前面加上&quot;where id=&amp;i&quot;就ok
作者: shiyiming    时间: 2011-2-13 09:28
标题: Re: 这样的PROC SQL 到底错在了哪里?
多谢各位的帮助。




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