合并两个表时如何在SAS中使用极限观察值

时间:2019-02-14 02:45:19

标签: sql sas

我使用的是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; 

3 个答案:

答案 0 :(得分:0)

通常读取数据时,您只需要使用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;

答案 1 :(得分:0)

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.

答案 2 :(得分:0)

我会这样做,因为这会限制在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;

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