在SAS数据步骤中设置重复的数据集

时间:2011-07-11 02:14:52

标签: sas

假设我有一个如下所示的SAS数据集:

id  x
1   1234
2   2345
3   3456

我需要一个新数据集,该数据集读入(比方说)2次,新变量指示这是“复制”:

id  x     rep
1   1234  1
2   2345  1
3   3456  1
1   1234  2
2   2345  2
3   3456  2

以正确的顺序读取数据非常重要 - 整个初始数据集只需读取一次,然后再读取等等。

有关在数据步骤中执行此操作的有效方法的任何想法? (实际上我的数据集很大,我需要多次阅读,我想避免排序。)

我试过这个,但是新数据集中观察的顺序并不是我想要的:

data foo;
 set tmp; rep=1; output;
 set tmp; rep=2; output;
run;

3 个答案:

答案 0 :(得分:8)

如果你想保持数据步骤,那么这将按你所描述的那样工作。

data foo;
  set tmp (in=INA) tmp (in=INB);
  if INA then REP=1;
  if INB then REP=2;
run;

答案 1 :(得分:1)

data rep;
  set tmp;

  do rep = 1 to 2; /* or 3, or whatever */
    output;
  end;
proc sort;
  by rep id;
run;

就是这样。

答案 2 :(得分:0)

你可以尝试使用视图和proc附加如下:

/* create view for rep=2 */

data rep2 / view=rep2;
 set tmp;
 rep = 2;
run;

/* create dataset for rep=1 */

data foo;
 set tmp;
 rep = 1;
run;

/* append rep=2 to rep=1 dataset */

proc append base=foo data=rep2;
run;
相关问题