按SAS中值的升序按每组的最小值排序

Sort by minimum value of each group in ascending order of value in SAS

我正在尝试按每组的最小值对值进行升序排序。我已经使用分组和排序尝试了几种不同的组合,但无法正确地按组排序。什么是最有效的方法?

我试过这个:

PROC SQL;
CREATE TABLE NEED AS 
SELECT DISTINCT *
FROM HAVE
GROUP BY ID,VALUE
ORDER BY VALUE;
QUIT; 

使用此输出并认为它会根据 ID 分组进行排序

您可以尝试简单的排序:

proc sort data = have out = want;
  by descending ID value;
run;

如果您想按最小值排序,则需要对每个观察值进行排序。

PROC SQL;
CREATE TABLE NEED AS 
  SELECT DISTINCT *,min(value) as min_value
  FROM HAVE
  GROUP BY ID
  order by min_value,id,value
;
QUIT; 

默认情况下,SQL Procedure 实现汇总统计信息的自动重新合并。

Per Documentation

Remerging Data

When you use a summary function in a SELECT clause or a HAVING clause, you might see the following message in the SAS log:
NOTE: The query requires remerging summary statistics back with the original data.

The process of remerging involves two passes through the data.

On the first pass, PROC SQL
• calculates and returns the value of summary functions. It then uses the result to calculate the arithmetic expressions in which the summary function participates.
• groups data according to the GROUP BY clause.

On the second pass, PROC SQL retrieves any additional columns and rows that it needs to show in the output.

Note: To specify that PROC SQL not process queries that use remerging of data, use either the PROC SQL NOREMERGE option or the NOSQLREMERGE system option. If remerging is attempted when the NOMERGE option or the NOSQLREMERGE system option is set, an error is written to the SAS log.

依赖自动重新合并的查询如下例所示:

proc sql;
  create table want as 
  select all.* from
  (select make, min(msrp) as lowest from sashelp.cars group by make) as sequencer
  join 
  sashelp.cars as all on sequencer.make = all.make
  order by sequencer.lowest, all.make, all.msrp
  ;

LOG window 将根据 lowest 的使用方式显示 NOTE

NOTE: The query as specified involves ordering by an item that doesn't appear in its SELECT clause.