BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
PTD_SAS
Obsidian | Level 7

Is there a way in SAS 9.3 to sort datasets alphabetically by variable names? I.e. each observation of the dataset to have the variables sorted alphabetically.

Thanks.

1 ACCEPTED SOLUTION

Accepted Solutions
art297
Opal | Level 21

Did you try my SQL solution?  The only thing you have to insure is that, in the call to dictionary.columns, that the libname and memname are spelled in CAPs.

View solution in original post

10 REPLIES 10
Haikuo
Onyx | Level 15

I am sure there will be better approaches, but given it is alreay 10pm est, you may not get them as timely as you wanted. So here is one option for you:

data have;

input b a c e d;

cards;

1 2 3 4 5

;

proc contents data=have out=have1;

run;

proc sql;

select name into :name separated by ' ' from have1;

quit;

data want;

retain &name;

set have;

run;

of course, one variation of this solution is to Proc SQL for the second half part of it:

proc sql;

select name into :name separated by ',' from have1;

quit;

proc sql;

create table want as select &name from have; quit;

Regards,

Haikuo

PTD_SAS
Obsidian | Level 7

Thanks for that! I'll try it although it'll be a bit awkward with many variable names.

Haikuo
Onyx | Level 15

"Many variable names", I hope not too many and too long. As long as it 's less than 32k characters after concatenation (including blanks), you will be fine.

Haikuo
Onyx | Level 15

Just found out that there is something worth mentioning of my solution: the case. You may want to add an option if you want to ignore the case:

proc contents data=have out=have1 order=ignorecase;

run;

otherwise it will put upper case variable names ahead of those lower case.

Haikuo

PTD_SAS
Obsidian | Level 7

Thanks again. I used art297's SQL solution, it works very well.

art297
Opal | Level 21

There is always the pure SQL solution:

proc sql noprint;

  select name

    into :names separated by ","

    from dictionary.columns

      where libname="SASHELP" and

            memname="CLASS"

       order by name

  ;

  create table want

    as select &names.

      from sashelp.class

  ;

quit;

Tom
Super User Tom
Super User

What does that mean?  Normally when you talk of sorting a dataset you mean sorting the observations.

Are you saying you want to create a copy of the data with the variables ordered alphabetically by their name rather than their original position?

PTD_SAS
Obsidian | Level 7

Tom,

Yes, exactly. I want to have the dataset with the variables ordered alphabetically by their name.

art297
Opal | Level 21

Did you try my SQL solution?  The only thing you have to insure is that, in the call to dictionary.columns, that the libname and memname are spelled in CAPs.

PTD_SAS
Obsidian | Level 7

That's excellent!, it worked. It made my job much easier because I analyse large datasets in JMP and now I can locate a specific variable name very easily.

Thanks.

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

Register now!

What is Bayesian Analysis?

Learn the difference between classical and Bayesian statistical approaches and see a few PROC examples to perform Bayesian analysis in this video.

Find more tutorials on the SAS Users YouTube channel.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 10 replies
  • 2072 views
  • 3 likes
  • 4 in conversation