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.
Don't miss out on SAS Innovate - Register now for the FREE Livestream!
Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.
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.