SAS: rearrange field order in data step
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;开发者_JAVA百科
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
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
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
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.
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
精彩评论