未排序数据上带有 BY 变量的 SAS 数据步骤

SAS data step with BY variable on unsorted data

我正在执行 SAS data step with by variable。我理解 数据按键 排序时的输出(在我的例子中是 X)。但是,当数据未排序时,我得到以下输出:

我正在使用 SAS ODA's AFRICA dataset from MAPS library which has 52824 rows. Here 的 link 到 CSV 文件。

data  AFRICA_NEW12;
set Maps.AFRICA;
by X;
firstX = FIRST.X;
lastX = LAST.X;
run;

我不明白数据未排序时如何选择行。为什么输出有14行?

您的日志有错误,因为您没有对它进行排序。请务必阅读您的日志。

您可能会遇到同样的问题:

data cars;
set sashelp.cars;
by model;
run;

proc print data=cars;
var make model origin;
run;

输出为:

Obs Make    Model   Origin
1   Acura   MDX Asia
2   Acura   RSX Type S 2dr  Asia

并且日志显示:

 ERROR: BY variables are not properly sorted on data set SASHELP.CARS.
 Make=Acura Model=TSX 4dr Type=Sedan Origin=Asia DriveTrain=Front MSRP=,990 Invoice=,647 EngineSize=2.4 Cylinders=4
 Horsepower=200 MPG_City=22 MPG_Highway=29 Weight=3230 Wheelbase=105 Length=183 FIRST.Model=1 LAST.Model=1 _ERROR_=1 _N_=3
 NOTE: The SAS System stopped processing this step because of errors.
 NOTE: There were 4 observations read from the data set SASHELP.CARS.
 WARNING: The data set WORK.CARS may be incomplete.  When this step was stopped there were 2 observations and 15 variables.
 WARNING: Data set WORK.CARS was not replaced because this step was stopped.

特别注意这部分:

警告:数据集 WORK.CARS 可能不完整。当这一步停止时,有 2 个观察值和 15 个变量。

如果您知道数据按您想要的顺序排序,这可能与 SAS 期望的顺序不同,您可以添加 notsorted option on the BY statement 但这是一种不同类型的功能所以彻底检查你的代码。

data cars;
set sashelp.cars;
by model notsorted;
run;