【问题标题】:How to use limit observations in SAS when union two tables合并两个表时如何在SAS中使用极限观察
【发布时间】:2019-07-08 00:17:41
【问题描述】:

我正在使用 sas,我想在订购数据源后限制每个表的输出行数,谁能告诉我如何在 SAS 中实现这一点?我知道在 mysql 中我可以使用 limit 来完成这项工作,但在 SAS 中,如果我使用 (obs=10)(outobs =10),它只会限制数据输入的数量。这是我的proc sql

select distinct sales as a from lucas
group by province 
outer union
select distinct sales as b from lucas
group by province
order by a desc, b asc; 

【问题讨论】:

  • 您能否也显示 MySQL 版本,您是否限制了每个选择的联合?样本似乎有点奇怪,SAS 日志显示了什么?

标签: sql sas


【解决方案1】:

通常您在读取数据时只使用OBS= 选项。

data top10;
  set have (obs=10);
  by size descending;
run;

如果您还没有按该顺序排序的数据集,并且您想避免写出完整的数据集,您可以使用 VIEW 为您进行生成和/或排序。

proc sql ;
create view derived_sales as
  select id,sum(sales) as total_sales
  from have 
  group by id 
  order by calculated total_sales desc
;
quit;
data top10_sales;
  set derived_sales(obs=10);
run;

【讨论】:

    【解决方案2】:

    Proc SQL 没有实现当前的现代子句,例如LIMITOFFSETFETCH,也没有您可能熟悉的分区功能。

    也就是说,您不能对排序的子选择或视图的输出进行行限制,但是,您可以使用OUTOBS 选项将输出限制为表。

    此示例创建了两个表,每个表对应于一个限制 10 行排序结果集的子选择。该选项在合并之前重置。

    proc sql;
    
      reset outobs=10;
    
      create table have_ss1 as
      select distinct msrp as msrp_1 
       from sashelp.cars 
       group by model
      ;
    
      create table have_ss2 as 
      select distinct msrp as msrp_2
       from sashelp.cars 
       group by model
      ;
    
      reset outobs=&sysmaxlong;
    
      create table want as 
      select * from have_ss1
      outer union
      select * from have_ss2
      ;
    

    SAS 日志窗口将显示信息性警告,例如:

    WARNING: A GROUP BY clause has been transformed into an ORDER BY clause because neither the
             SELECT clause nor the optional HAVING clause of the associated table-expression
             referenced a summary function.
    WARNING: The query as specified involves ordering by an item that doesn't appear in its SELECT
             clause. Since you are ordering the output of a SELECT DISTINCT it may appear that some
             duplicates have not been eliminated.
    WARNING: Statement terminated early due to OUTOBS=10 option.
    

    【讨论】:

      【解决方案3】:

      我会这样做,因为这会限制在 proc sql 中创建的数据集/表,而不是来自 lucas 数据集/表的输入:

      proc sql outobs=10;
      select distinct sales as a from lucas
      group by province 
      outer union
      select distinct sales as b from lucas
      group by province
      order by a desc, b asc; 
      quit;
      

      这只会限制输出而不是输入!

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2018-07-16
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-07-21
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多