SAS Programming

DATA Step, Macro, Functions and more
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-white.png

Our biggest data and AI event of the year.

Don’t miss the livestream kicking off May 7. It’s free. It’s easy. And it’s the best seat in the house.

Join us virtually with our complimentary SAS Innovate Digital Pass. Watch live or on-demand in multiple languages, with translations available to help you get the most out of every session.

 

Register now!

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
  • 1472 views
  • 0 likes
  • 3 in conversation