tags:

views:

213

answers:

2

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

+3  A: 

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

mcpeterson
+2  A: 

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

Rob Penridge