Hello,
I tried to build multiple tables which always have 'Label' col and COLX, which x is non repeated number select from 1 to 99.
Is it a way I can always recorder or output my outputs use 'Label' as 1st col and COLs in the order from small number to 99?
Currently my outputs are always in a random col order. Any suggest or macro that can solve this?
For example: my output contain following cols:
DS1:
'Label', 'COL1', 'COL7', 'COL8', 'COL11', 'COL99'
DS2:
'COL7', 'COL1', 'COL88', 'COL99', COL1', 'Label'.
The simplest way to reorder the variables is to take advantage of the fact that the RETAIN statement places the variables into the dataset, but does not define a type for them (unless you include an initial value in the RETAIN statement).
data want;
retain label col1 col47 col99 ;
set have;
run;
If you want to automate that then find a rule that sets the order of the columns in the way you want. For your example I might be tempted to do something like:
proc contents data=have noprint out=contents; run;
proc sql noprint;
select nliteral(name) into :varlist separated by ' '
from contents
;
quit;
data want;
retain label col1-col99;
set have;
keep &varlist;
run;
More explanation is needed. How are you going to do this "output"? What SAS PROC or other SAS command is going to produce this "output"? Is this "output" a SAS data set or a printed report or something else?
@stataq wrote:
Maybe just output another dataset that contain correct col order?
data ds1_update;
set ds1;
...
Run;
Seems pointless to me to re-arrange columns within a data set. What is the reason you want this? How would this help?
I hope to achieve a status that my outputs are organized in a good col order as a dataset. when I need to output them in file, I can directly use them without reorder the cols. Maybe also easier for me to review the results when I just look at the dataset without proc print step. might be silly but make sense to me.😅
@PaigeMiller wrote:
Seems pointless to me to re-arrange columns within a data set. What is the reason you want this? How would this help?
I often do pay attention to establishing column order, for two reasons:
The simplest way to reorder the variables is to take advantage of the fact that the RETAIN statement places the variables into the dataset, but does not define a type for them (unless you include an initial value in the RETAIN statement).
data want;
retain label col1 col47 col99 ;
set have;
run;
If you want to automate that then find a rule that sets the order of the columns in the way you want. For your example I might be tempted to do something like:
proc contents data=have noprint out=contents; run;
proc sql noprint;
select nliteral(name) into :varlist separated by ' '
from contents
;
quit;
data want;
retain label col1-col99;
set have;
keep &varlist;
run;
Catch the best of SAS Innovate 2025 — anytime, anywhere. Stream powerful keynotes, real-world demos, and game-changing insights from the world’s leading data and AI minds.
Learn how use the CAT functions in SAS to join values from multiple variables into a single value.
Find more tutorials on the SAS Users YouTube channel.
Ready to level-up your skills? Choose your own adventure.