You can't use the -- (double-dash) notation because the only reason retain
works is that it operates before the dataset's variables enter the PDV; once SAS sees those variables, it assigns them in the order it sees them, and you can't change their position. However, double-dash notation requires already having those variables in the PDV, so the two concepts (reordering variables, and double-dash notation) conflict.
There isn't a great solution to what you're trying to do entirely within SAS. The simplest solution is to use a proc contents
output, or similarly dictionary.columns
in SQL, to get the list; but you still will have to somehow add to that.
The best solution I recommend is to create an excel spreadsheet (or a CSV or similar) that contains your variables, in the order you want them in. You can produce this initially from PROC CONTENTS
with the VARNUM
option, which orders the variable in the current variable order (rather than alphabetical).
Then import that spreadsheet, and use it in the RETAIN statement.
proc import file="mydatadictionary.xlsx" out=datadict dbms=excel replace;
run;
proc sql;
select name into :orderlist separated by ' '
from datadict
where active=1
order by var_order;
quit;
data want;
retain &orderlist.;
set have;
run;
The above assumes that your data dictionary spreadsheet (what I call this - also contains information about the variables, formats, etc.) has columns name
(variable name), var_order
(order in the dataset), and active
which is 1 or blank (active variables 1, no longer active variables 0 or blank).