Reorder Variables in SAS: RETAIN, ATTRIB, SQL and What Each Actually Changes
How to reorder variables in SAS with RETAIN, ATTRIB, LENGTH or PROC SQL, what each method changes in the stored data set, and when display-only reordering is better.
Reordering variables in SAS means changing the position of columns in a data set without changing their names or values. SAS has no dedicated reorder option. Instead, you rely on how the DATA step builds its program data vector (PDV), or you rebuild the table with PROC SQL. This review covers the common methods, what each changes, and when you should change only the displayed order and leave the stored data set alone.
Is reordering the right step for your use?
Change the stored order when downstream consumers depend on position. Examples include CSV exports that another system reads by column position, name range lists such as a--d, and colleagues who browse the table and expect a fixed layout. The benefit is a predictable layout everywhere the data set goes. The main tradeoff is that every method rewrites the data set, so you must confirm that values, attributes and table-level properties come through intact.
If you only need a report or printout in a particular order, leave the data set as it is and set the order in the procedure that produces the output. That approach rewrites nothing and carries no risk to the stored data.
What reordering actually changes
Variable order in a SAS data set is set during DATA step compilation. Each variable takes its position when the compiler first encounters it. Any statement that names a variable before the SET statement therefore fixes that variable's position. Variables you do not list follow in their original order. The values in each row stay the same. Only the column positions recorded in the data set's descriptor change.
Keep stored order and displayed order separate. A VAR statement in PROC PRINT or a COLUMNS statement in PROC REPORT changes only what appears in the output. The methods below change the data set itself.
RETAIN before SET
The RETAIN statement is the most common idiom for reordering. It names variables without assigning lengths, formats or labels, so for variables read from the input data set it changes position and nothing else.
data work.reordered;
retain id visit_date subject_name;
set work.original;
run;
Here id, visit_date and subject_name come first, and the remaining variables follow in their existing order. The RETAIN statement must appear before SET. If it comes after SET, the order does not change.
Caveat: RETAIN still does its normal job. Variables already read by SET are reloaded on every iteration, so retaining them has no practical effect. A new variable created in the same step behaves differently. If you list it in RETAIN, it is not reset to missing at the start of each iteration. When its assignment is conditional, rows where the condition is false will silently carry the previous row's value. To avoid this, reorder in a separate step from new calculations, or make sure every new retained variable is assigned on every iteration.
ATTRIB, LENGTH and FORMAT before SET
Attribute statements placed before SET also fix position, because they are the first place the compiler sees each variable. Unlike RETAIN, they require or imply attribute values, which brings a different risk.
data work.reordered;
attrib id length=8
visit_date length=8 format=date9.
subject_name length=$40;
set work.original;
run;
- ATTRIB needs at least one attribute for each variable, so you end up restating lengths or formats. If what you state differs from the source, the output carries your version, not the original.
- LENGTH placed before SET can shorten a character variable. A shorter length truncates values, and SAS may warn about differing lengths.
- A FORMAT statement that lists variables without naming a format removes their formats, so check it carefully before using it for ordering.
- This approach works best when you are deliberately standardizing attributes and want the order set at the same time.
PROC SQL SELECT
proc sql;
create table work.reordered as
select id, visit_date, subject_name, *
from work.original;
quit;
The SELECT list order becomes the column order. Adding an asterisk after the named columns brings in the rest. SAS writes a warning that the named variables already exist and keeps the first occurrence. The table is still created, but the warning shows up in logs that some teams require to be clean. Listing every column explicitly avoids the warning at the cost of a long SELECT list.
Methods that do not reorder
- KEEP= and DROP=, and the KEEP and DROP statements, control which variables are included but not their order.
- PROC DATASETS with MODIFY can rename variables and change some attributes, but it cannot change position.
- RENAME changes names and leaves positions as they are.
Comparing the methods
| Method | Changes stored order | Risk to attributes | Main caveat |
| RETAIN before SET | Yes | None for variables read from input | New variables in the same step keep values across rows |
| ATTRIB or LENGTH before SET | Yes | Restated attributes override the source | Truncation or format changes if mismatched |
| PROC SQL SELECT | Yes | Low for listed columns | Duplicate-variable warning when * is used |
| PROC PRINT VAR or PROC REPORT COLUMNS | No | None | Order applies only to that output |
Does the result stay usable after export or sharing?
Once the data set is rewritten, the new order is the order. PROC EXPORT and other procedures that write every variable without an explicit list use the stored order, so a CSV exported from the reordered table reflects it. Consumers that read columns by name are unaffected. Consumers that read by position are affected, which is often the reason for reordering in the first place, so test them with the new file.
Reordering can also change what existing code means. A name range list such as first_var--last_var selects variables by position, so after reordering it can include different variables than before. ARRAY statements and VAR, KEEP or DROP lists written with double-dash ranges carry the same risk. Single-dash numbered lists such as q1-q10 depend on names, not positions, and are not affected. Before replacing a production table, search dependent programs for double-dash ranges.
Because each method creates a new data set, check table-level properties after you rewrite it. Indexes, integrity constraints, sort indicators and the data set label may not carry over the way they did on the original. Compare PROC CONTENTS output for the old and new tables, and spot-check row counts and a sample of values.
Leave the stored data set alone when the order matters only to human readers of a report, when other programs depend on the current positions, or when the table carries indexes or constraints you would have to rebuild. In those cases, use VAR or COLUMNS in the reporting step.
Gaps in this review
This review draws on general, documented SAS behavior. No version-specific documentation, test results or environment details were supplied. Exactly which table-level properties survive each method can depend on your SAS release and the engine behind the library, such as Base SAS libraries, database libraries or other storage. Confirm that behavior in your own environment before relying on it.