BookmarkSubscribeRSS Feed
Konkordanz
Pyrite | Level 9

Hello,

my dataset has ~100 columns. I want to order the columns alphabetically with sql or sas-code (the Column with the number 1 shall appears first, 2 second and so on)... Is it implementable with little code?

 

Thank you!

 

Data have:

5 3 2 50 6 8 7

 

data want:

2 3 5 6 7 8 50
2 REPLIES 2
Astounding
PROC Star
Do your column names follow typical naming conventions (begin with a letter or underscore, use up to 32 letters, numbers and underscores)? If so, this is a straightforward problem. If not, we have some hoops to jump through.
Also note what it means to use alphabetical order. If you have names 2, 3, 5, 15, 20, alphabetical order would mean 15, 2, 20, 3, 5. Is that what you want?
Ksharp
Super User
data have;
input var12 var2 var3 var1 var4;
cards;
1 2 3 4 5
;



proc sql noprint;
select name into :names separated by ','
 from dictionary.columns
  where upcase(libname)='WORK' and upcase(memname)='HAVE'
   order by input(compress(name,,'kd'),best.);

create table want as
select &names. from have;
quit;

SAS Innovate 2025: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

How to Concatenate Values

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 2 replies
  • 1056 views
  • 0 likes
  • 3 in conversation