Post by k***@gmail.comI have an issue that has been stumping me.
Does anyone know of an automated way to sort column orders for output?
For example...
AAA BBB DDD CCC
That's the variable order in my SAS dataset. I want the columns to be
sorted alphabetically though. (I have about 800 columns that can
change names, so manual hardcording isn't possible.)
Does this question make sense?
800 columns is oftne hard to make sense of.
This code sample will rebuild a table such that the column names are sorted
by prefix and numeric suffix.
--------------------------------------
%macro ugly_factory (n=300, out=ugly);
option NOQUOTELENMAX ;
data prefixes;
words = "
The Unanimous Declaration of the Thirteen United States of
America
When, in the course of human events, it becomes necessary for one
people to dissolve the political bonds which have connected them
with another, and to assume among the powers of the earth, the
separate and equal station to which the laws of nature and of
nature's God entitle them, a decent respect to the opinions of
mankind requires that they should declare the causes which impel
them to the separation.
";
i = 1;
do while (scan(words,i) ne '');
prefix = compress(scan(words,i,collate(0,64)));
output;
i+1;
end;
keep prefix;
run;
data _null_;
retain seed 654321;
length vars $32000;
do _n_ = 1 to &n;
p = 1 + floor (nobs*ranuni(seed));
set prefixes point=p nobs=nobs;
vars = catx(',',vars,cats(prefix,ceil(250*ranuni(seed))));
end;
call symput('vars', trim(vars));
stop;
run;
data ugly;
call missing (&vars, fee, fi, fo, phum, x);
run;
%mend;
%ugly_factory()
proc sql noprint;
create view name_split as
select name
,
indexc(left(reverse(name)),compress(collate(0,255),collate(30x,39x))) as p
, case calculated p
when 1
then upcase(name)
else upcase(substr(name,1,length(name)-(calculated p)+1))
end as left
, case calculated p
when 1
then 0
else input(substr(name,length(name)-(calculated p)+2),best12.)
end as right
from dictionary.columns
where libname='WORK' and memname='UGLY'
;
select name
into :names separated by ' '
from name_split
order by
left, right
;
quit;
data ugly;
retain &names;
set ugly;
run;
--------------------------------------
Richard A. DeVenezia
http://www.devenezia.com/