简体   繁体   English

如何在SAS中的数据集之上添加新观察值?

[英]How can I add a new observation on top of a data set in SAS?

I know that I can use PROC SQL INSERT INTO (_Table_Name_) values('Value') or by using INSERT INTO SET Variable Name =numeric_value or 'char_value' 我知道我可以使用PROC SQL INSERT INTO(_Table_Name_)values('Value')或通过使用INSERT INTO SET 变量名 = numeric_value或'char_value'

but my question is: how do I make sure that this value appears on top of the dataset instead of the default bottom position? 但是我的问题是:如何确保该值出现在数据集的顶部而不是默认的底部位置?

data temp;
input x y;
datalines;
1 2
3 4
5 6
;
run;

proc sql;
insert into work.temp
(x,y)
values (8,9);
quit;

You cannot insert values "on top" of the dataset without rewriting the dataset. 如果不重写数据集,则不能在数据集的“顶部”插入值。 INSERT (and PROC APPEND) work by avoiding rewriting the entire dataset, instead just adding the rows to the bottom. INSERT(和PROC APPEND)的工作方式是避免重写整个数据集,而只需将行添加到底部即可。 SAS has a defined data structure where observations are physically stored in the order they will be processed in when normal, sequential processing is used (as opposed to index-based or random-access methods). SAS具有已定义的数据结构,在该结构中,按照使用常规顺序处理时(与基于索引的方法或随机访问方法相对)的处理顺序,以物理方式存储观测值。

To put rows at the "top" of the dataset, simply create a new dataset (which can use the same name, if you choose, though it will be a different dataset technically) and add them however you choose; 要将行放在数据集的“顶部”,只需创建一个新的数据集(如果您选择的话,可以使用相同的名称,尽管从技术上讲它将是一个不同的数据集)并按您的选择添加它们。 even something as simple as below would work, though I'd put the data-to-be-inserted into a separate dataset in a real application (as it probably would come from a different data source). 尽管我将要插入的数据放入真实应用程序中的单独数据集中(即使它可能来自不同的数据源),但即使是如下所示的简单操作也可以使用。

data temp;
  if _n_=1 then do;  *if on first iteration, add things here;
    x=8; 
    y=9; 
    output;  *outputs the new record;
  end;
  set temp;
  output;  *outputs the original record;
run;

you can do this in a data step as follows: 您可以在数据步骤中执行以下操作:

data a;
x=1;
y=2;
output;
x=3;
y=4;
output;
run;
data b;
x=7;
y=8;
output;
run;

data c;
set b a;
run;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM