I have one data set
Like this,
ORDER | ADDRE:HOME_1 | TELEPONE:HOME_1 | TELEPONE:FAX_1 | ADDRE:HOME_2 | TELEPONE:HOME_2 | ADDRE:HOME_3 |
1 | A | 11 | 22 | B | 22 | C |
2 | B | 22 | 33 | C | 33 | D |
3 | C | 33 | 44 | D | 44 | E |
4 | D | 44 | 55 | E | 55 | F |
5 | E | 55 | 66 | F | 66 | G |
6 | F | 66 | 77 | G | 77 | H |
7 | G | 77 | 88 | H | 88 | I |
8 | H | 88 | 99 | I | 99 | J |
9 | I | 99 | 110 | J | 110 | K |
10 | J | 10 | 121 | K | 121 | L |
Is there a way to automatically have the columns in the data set order as follows?
ORDER | ADDRE:HOME_1 | ADDRE:HOME_2 | ADDRE:HOME_3 | TELEPONE:HOME_1 | TELEPONE:HOME_2 | TELEPONE:FAX_1 |
1 | A | B | C | 11 | 22 | 22 |
2 | B | C | D | 22 | 33 | 33 |
3 | C | D | E | 33 | 44 | 44 |
4 | D | E | F | 44 | 55 | 55 |
5 | E | F | G | 55 | 66 | 66 |
6 | F | G | H | 66 | 77 | 77 |
7 | G | H | I | 77 | 88 | 88 |
8 | H | I | J | 88 | 99 | 99 |
9 | I | J | K | 99 | 110 | 110 |
10 | J | K | L | 10 | 121 | 121 |
Thank you for your help!!!!
Allan
with your data;
data want;
retain ADDRE:HOME_1 ADDRE:HOME_2 ADDRE:HOME_3 ORDER TELEPONE:FAX_1 TELEPONE:HOME_1 TELEPONE:HOME_2;
set have;
run;
OR
%let var= ADDRE:HOME_1 ADDRE:HOME_2 ADDRE:HOME_3 ORDER TELEPONE:FAX_1 TELEPONE:HOME_1 TELEPONE:HOME_2;
data want;
retain &var.;
set have;
run;
ADDRE:HOME_1 | ADDRE:HOME_2 | ADDRE:HOME_3 | ORDER | TELEPONE:FAX_1 | TELEPONE:HOME_1 | TELEPONE:HOME_2 |
The easiest way is:
export the variables to Excel and then sort it . Then run the following code
%let var= <copy and paste the sorted variable names from Excel here>;
data want;
retain &var.;
set have;
run;
with your data;
data want;
retain ADDRE:HOME_1 ADDRE:HOME_2 ADDRE:HOME_3 ORDER TELEPONE:FAX_1 TELEPONE:HOME_1 TELEPONE:HOME_2;
set have;
run;
OR
%let var= ADDRE:HOME_1 ADDRE:HOME_2 ADDRE:HOME_3 ORDER TELEPONE:FAX_1 TELEPONE:HOME_1 TELEPONE:HOME_2;
data want;
retain &var.;
set have;
run;
ADDRE:HOME_1 | ADDRE:HOME_2 | ADDRE:HOME_3 | ORDER | TELEPONE:FAX_1 | TELEPONE:HOME_1 | TELEPONE:HOME_2 |
Thank you,
But I have a problem with that solution, in some cases the number of columns it would be diferent, not always are the same number of columns. Thats my problem.
Untested code
proc contents data=have out=_cont_ noprint;
run;
proc sql;
select distinct name into :var separated by ' ' from _cont_ where lowcase(name)^='order' order by name;
quit;
data want;
retain order &var;
set have;
run;
Which brings up the interesting question ... why do this? None of the procedures used by SAS care what order the columns are in; and if you are doing this for presentation, then procedures such as PROC PRINT and PROC REPORT let you explicity state what order the variables are to appear in.
Thanks.
Works perfectly!!!!
Registration is now open for SAS Innovate 2025 , our biggest and most exciting global event of the year! Join us in Orlando, FL, May 6-9.
Sign up by Dec. 31 to get the 2024 rate of just $495.
Register now!
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.