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.
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.
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
Thanks for that! I'll try it although it'll be a bit awkward with many variable names.
"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.
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
Thanks again. I used art297's SQL solution, it works very well.
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;
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?
Tom,
Yes, exactly. I want to have the dataset with the variables ordered alphabetically by their name.
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.
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.
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!
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.