Solved
New Contributor
Posts: 3

# How orden columns alphabetically?

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

Allan

Accepted Solutions
Solution
‎10-22-2013 07:27 PM
Frequent Contributor
Posts: 83

## Re: How orden columns alphabetically?

data want;

set have;

run;

OR

data want;

retain &var.;

set have;

run;

All Replies
Frequent Contributor
Posts: 83

## Re: How orden columns alphabetically?

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;

Solution
‎10-22-2013 07:27 PM
Frequent Contributor
Posts: 83

data want;

set have;

run;

OR

data want;

retain &var.;

set have;

run;

New Contributor
Posts: 3

## Re: How orden columns alphabetically?

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.

Posts: 3,003

## Re: How orden columns alphabetically?

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.

--
Paige Miller
New Contributor
Posts: 3

## Re: How orden columns alphabetically?

Thanks.

Works perfectly!!!!

🔒 This topic is solved and locked.