SAS:重新排列数据步骤中的字段顺序

发布于 2024-08-30 04:20:59 字数 379 浏览 7 评论 0原文

在 SAS 9 中,如何在简单的数据步骤中重新排列字段的顺序。

Data set2;
  /*Something probably goes here*/
  set set1;
run;

因此,如果 set1 具有以下字段:

Name   Title   Salary
A      Chief   40000
B      Chief   45000

那么我可以将 set2 的字段顺序更改为:

Title  Salary  Name
Chief  40000   A
Chief  45000   B

谢谢,

Dan

In SAS 9, how can I in a simple data step, rearrange the order the field.

Data set2;
  /*Something probably goes here*/
  set set1;
run;

So if set1 has the following fields:

Name   Title   Salary
A      Chief   40000
B      Chief   45000

Then I can change the field order of set2 to:

Title  Salary  Name
Chief  40000   A
Chief  45000   B

Thanks,

Dan

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

知足的幸福 2024-09-06 04:20:59

一些快速谷歌搜索给了我这个方法:

data set2;
  retain title salary name;
  set set1;
  run;

从这里:
http://analytics.ncsu.edu/sesug/2002/PS12.pdf

Some quick googling gave me this method:

data set2;
  retain title salary name;
  set set1;
  run;

from here:
http://analytics.ncsu.edu/sesug/2002/PS12.pdf

策马西风 2024-09-06 04:20:59

如果数据集中有大量变量,有时使用 sql 语句而不是 datastep 会更容易。这允许您仅列出您关心顺序的变量,并使用通配符保留其他所有内容。

proc sql noprint;
  create table set2 as
  select title, salary, *
  from set1;
quit;

如果您使用大型表执行此操作,则可以通过创建视图来节省 IO 开销。这既可以应用于数据集方法,也可以应用于 proc sql 方法。

proc sql noprint;
  create view set2 as
  select title, *
  from set1;
quit;

** OR;

data set2 / view=set2;
  retain title salary name;
  set set1;
run;

干杯

If you have a very large number of variables in your dataset sometimes it is easier to use an sql statement instead of a datastep. This allows you to list just the variables whose order you care about and use a wildcard to retain everything else.

proc sql noprint;
  create table set2 as
  select title, salary, *
  from set1;
quit;

If you are doing this with a large table you can save yourself the IO overhead by creating a view instead. This can be applied to both the data set approach or the proc sql approach.

proc sql noprint;
  create view set2 as
  select title, *
  from set1;
quit;

** OR;

data set2 / view=set2;
  retain title salary name;
  set set1;
run;

Cheers
Rob

苏佲洛 2024-09-06 04:20:59

您还可以使用信息语句来执行此操作 - 无需指定任何信息。我怀疑这比等效的保留语句稍微更有效,因为它允许 SAS 将值初始化为缺失,而不是从上一行检索它们。实际上,差异很小,并且您还可以选择使用视图。

data set2;
  informat title salary name;
  set set1;
run;

informat 语句中指定的变量将移至数据集的左侧并按该顺序排列,其余变量保留在输入数据集中的原样。

You can also use an informat statement to do this - there is no need to specify any informats. I suspect this is slightly more efficient than an equivalent retain statement, as it allows SAS to initialise values to missing rather than retrieving them from the previous row. In practice the difference is minimal, and you also have the option of using a view.

data set2;
  informat title salary name;
  set set1;
run;

The variables specified in the informat statement are moved to the left of the dataset and into that order, and the rest are left as they were in the input dataset.

蓬勃野心 2024-09-06 04:20:59

您可以使用任何按您想要的顺序初始化 PDV 的变量(ATTRIBARRAYFORMATINFORMAT、长度保留)。

来源:此 SAS 注释:http://support.sas.com/kb/8/ 395.html

You can use anything that initializes the PDV with variables in the order you want (ATTRIB, ARRAY, FORMAT, INFORMAT, LENGTH, RETAIN).

Source: This SAS note: http://support.sas.com/kb/8/395.html

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文