0% found this document useful (0 votes)
692 views

SAS Macro To Reorder Dataset Variables in Alphabetic Order

This document describes a SAS macro that reorders the variables in a dataset alphabetically. It uses PROC CONTENTS to get the variable names into a separate dataset, sorts that dataset alphabetically, then uses macro variables to retain the variables in the correct order when writing the reordered dataset. The macro demonstrates reordering the variables in the SASHELP.FLAGS dataset for example. Instructions are provided on how to use the macro on a dataset to reorder its variables.

Uploaded by

SASTechies
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
692 views

SAS Macro To Reorder Dataset Variables in Alphabetic Order

This document describes a SAS macro that reorders the variables in a dataset alphabetically. It uses PROC CONTENTS to get the variable names into a separate dataset, sorts that dataset alphabetically, then uses macro variables to retain the variables in the correct order when writing the reordered dataset. The macro demonstrates reordering the variables in the SASHELP.FLAGS dataset for example. Instructions are provided on how to use the macro on a dataset to reorder its variables.

Uploaded by

SASTechies
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 4

SAS macro to reorder dataset variables in alphabetic order...

How do you reorder variables in a dataset...I get this many a times....


Here's a macro for you to achieve it...For example I've used a dataset sashelp.flags and created some more variables
with variety of variables with names upper / lower cases and _'s to demonstrate the reorder macro....

Please try this macro for yourself and let me know your suggestions....

/* Example dataset with variety of variable names */

data flags;
set sashelp.flags;
a=2;
b=4;
_common=10;
Cool=30;
SubJecT=40;
run
;

%macro reorder(dsn);
/* Get the variables names to a dataset using proc contents and keeping the variable name only */
proc contents data=&dsn
out=varnames(keep=name) noprint;
run;
/* It is very much important that you UPCASE or LOWCASE the variable names...
otherwise you get a different order...Remove this datastep and see for yourself... */
data varnames;
set varnames;
name=lowcase(name);
run;

/* Sort the variable names in alphabetical order */


proc sort data=varnames;
by name;
run;

/* Get the observation count */


data _null_;
set varnames nobs=num;
call symput('obscnt',num);/* Get the observation count */
call symput(compress('macvar'||_n_),trim(left(name))); /* Get the variable names into macro variables */
run;

%let obscnt=&obscnt; /*remove the leading and trailing blankspaces generated when numeric is converted to Best12.
format */

/*Please NOTE that the step of getting all variable names into a macro variable could be simply done by using SQL
instead of a macro
proc sql noprint;
select trim(left(name)) into:macvar separated by ' '
from varnames;
quit;
and the next datastep simply
data &dsn;
retain &macvar;
set &dsn;
run;
But the cons here is that if there are too many variables and the total length of all the names put together crosses 32767
bytes the SQL approach would'nt work...
*/

data &dsn;
retain %do i=1 %to &obscnt;
&&macvar&i /* NOTE: there should be a blank space after &&macvar&i to separate the variable names by
space eg. retain subject a b c;
NOTE: NO semicolon should be typed here*/
%end;
set &dsn;
run;

%mend reorder;

%reorder(flags);
You might also like:
• Ways to Count the Number of Obs in a dataset and pass it into a macro variable...
• SAS Interview Questions and Answers(2)
• SAS Interview Questions and Answers(1)
• SAS macro to Remove a PC Directory...
Read full article @ http://sastechies.blogspot.com/2009/11/sas-macro-to-reorder-dataset-variables.html

You might also like