SAS中文论坛

标题: 怎样计算sas数据集的行数? [打印本页]

作者: shiyiming    时间: 2008-10-15 10:57
标题: 怎样计算sas数据集的行数?
怎样对变化的sas数据集计算它的行数?使用循环语句么,请大侠帮忙!
作者: shiyiming    时间: 2008-10-15 11:02
标题: Re: 怎样计算sas数据集的行数?
也有类似问题,

Create Table new as
Select  count(product) as cnt from myTable
            group by product
               Order by cnt desc;

如果希望选出前 5 条记录,应该怎么写
   
     where ......  <=5 ;
作者: shiyiming    时间: 2008-10-15 11:20
标题: Re: 怎样计算sas数据集的行数?
取前五条记录 where _n_<=5;
计算行数用:[code:367hckdh]proc sql;
     create table temp as
         select count(*) as totnum
         from sashelp.class;
quit;


data temp;
     set sashelp.class end=eof;
         num+1;
         if eof then output;
         keep num;
run;
[/code:367hckdh]
作者: shiyiming    时间: 2008-10-15 11:22
标题: Re: 怎样计算sas数据集的行数?
应该用 if _n_<=5;
作者: shiyiming    时间: 2008-10-16 06:30
标题: Re: 怎样计算sas数据集的行数?
horse1,

能够举个例子吗?比如怎么用  SQL 选出前 5 个同学的记录。
作者: shiyiming    时间: 2008-10-16 16:38
标题: Re: 怎样计算sas数据集的行数?
试一下这个(生成一个宏变量):
[color=#FF0000:1dn2ihga]data a;
  set a end=final;
    if _n_ then
                do;
                  i+1;
                  output;
                end;
          
        if final then  call symput("numbe",i);
run;[/color:1dn2ihga]
作者: shiyiming    时间: 2008-10-16 17:06
标题: Re: 怎样计算sas数据集的行数?
请问需要的是这个吗?
data _null_;
    set table1 nobs=mm;
    call symput('count',mm);
    put count;
run;
作者: shiyiming    时间: 2008-10-16 17:55
标题: Re: 怎样计算sas数据集的行数?
楼上几位完全都没有考虑到执行效率,呵呵呵,如果数据集很大,你们这么做法实在不敢恭维。不相信可以挑个大数据集试试看,需要花多久。
不过还是很感谢积极参与!来,我来演示两招效率高的做法。
[code:25vy3x72]
/*方法一:MACRO实现*/
%let dsid=%sysfunc(open(sashelp.class,i));
%let count=%sysfunc(attrn(&dsid,nlobs));
%let rc=%sysfunc(close(&dsid));
%put COUNT: &count;
[/code:25vy3x72]
[code:25vy3x72]
/*方法二:DATA STEP实现*/
data _null_;
if 0 then set sashelp.class nobs=nobs;
call symput('count',nobs);
run;
%put COUNT: &count;
[/code:25vy3x72]
想要提高执行效率,请参考宝典:SAS Programming III这本书。
作者: shiyiming    时间: 2008-10-16 23:02
标题: Re: 怎样计算sas数据集的行数?
这样在  log 窗口里是可以看到了,但如何选出前 5 条记录呢?
再次谢谢!
作者: shiyiming    时间: 2008-10-16 23:17
标题: Re: 怎样计算sas数据集的行数?
<!-- m --><a class="postlink" href="http://sasor.feoh.net/viewtopic.php?f=1&amp;t=3977">http://sasor.feoh.net/viewtopic.php?f=1&amp;t=3977</a><!-- m -->
作者: shiyiming    时间: 2008-10-17 09:57
标题: Re: 怎样计算sas数据集的行数?
回复lightening[code:8848uof3]proc sql noprint outobs=5;
     create table temp as
         select *
         from class;
quit;
[/code:8848uof3]
作者: shiyiming    时间: 2008-10-17 10:51
标题: Re: 怎样计算sas数据集的行数?
Thanks again, urchin,
Your link opens another window for me to learn SAS.

It works after I reviewed horse1's entry. I tried on a big dataset.
Thanks a lot, all of you.

[color=#4000FF:1xy0jay6]proc sql  outobs=10;       /* 如果要选 第 200 至 299 的记录呢?*/
   Create table work.temp as
select *
from sashelp.class
order by height desc;
quit; [/color:1xy0jay6]

只是感到奇怪,有些很简单的东西,SAS 处理起来变得复杂了。
.Net 好像可以  Select top 10 .... from table_Name;

FoxPro 可以 Select * from yourtable where  recno()&lt;=10;

Select yourtable
Go Bottom
? recno()     * 显示所有记录, (包括已删除的);

或:
Select * from yourtable
? _tally
作者: shiyiming    时间: 2008-10-17 13:19
标题: Re: 怎样计算sas数据集的行数?
那你可以在用SQL读入数据前做个flag, 然后在sql中用where语句
作者: shiyiming    时间: 2008-10-17 14:20
标题: Re: 怎样计算sas数据集的行数?
干吗放着强大的DATA STEP不用,老想着用SQL啊?在SAS里能不用SQL就不要用,执行效率巨低下!
[code:2kr92e4n]data class;
set sashelp&#46;class(firstobs=2 obs=10);
run;[/code:2kr92e4n]
[code:2kr92e4n]proc sort data=sashelp&#46;class(firstobs=2 obs=10) out=class;
by height descending;
run;[/code:2kr92e4n]
[code:2kr92e4n]proc sort data=sashelp&#46;class out=class;
by height descending;
run;
data class;
set class(firstobs=2 obs=10);
run;[/code:2kr92e4n]
作者: shiyiming    时间: 2008-10-17 20:33
标题: Re: 怎样计算sas数据集的行数?
十分感谢。一些模糊概念变得清晰起来。
作者: shiyiming    时间: 2008-10-21 12:58
标题: Re: 怎样计算sas数据集的行数?
用select直接生成红变量就OK了
proc sql;
     create table temp as
    select count(*) into :totnum
    from sashelp.class;
quit;
put &amp;totnum;
作者: shiyiming    时间: 2008-10-24 05:09
标题: Re: 怎样计算sas数据集的行数?
那个 Put 不好使呀。
作者: shiyiming    时间: 2008-10-24 07:40
标题: Re: 怎样计算sas数据集的行数?
%put &amp;totnum;
作者: shiyiming    时间: 2008-10-24 10:14
标题: Re: 怎样计算sas数据集的行数?
是这样吗?
proc sql;
create table temp as
select count(*) into :totnum
from sashelp.class;
quit;

Data _null_;
%put &amp;totnum=;
run;

Log screen 显示

WARNING: Apparent symbolic reference TOTNUM not resolved.
作者: shiyiming    时间: 2008-10-24 10:27
标题: Re: 怎样计算sas数据集的行数?
晕,还要手把手写的这么详细才行啊。你需要恶补SAS Programming I和II,还有SAS Macro这些教材啊。
不过前面不是特意写了,proc sql的效率暴差。如果原始数据集很大,这个过程将会是若干小时。用我之前写的方法,无论原始数据集多大,运行时间始终不超过5秒。
<!-- s:lol: --><img src="{SMILIES_PATH}/icon_lol.gif" alt=":lol:" title="Laughing" /><!-- s:lol: --> [code:1zggklsw]proc sql;
create table temp as
select count(*) into &#58;totnum
from sashelp&#46;class;
quit;
%put &amp;totnum; [/code:1zggklsw]
作者: shiyiming    时间: 2008-10-24 22:27
标题: Re: 怎样计算sas数据集的行数?
对不起,依然显示不了。




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