SAS : Reordering Variables

Suppose you have two datasets with same named variables. You want the order of the variables to be same in both the datasets.

Let's create two datasets
data abc;
input a b c;
1 3 4
2 4 5

data bac;
input b a c;
1 3 4
2 4 5
SAS Code : Reordering Variables
proc sql noprint;
select name
into :reorder
separated by ' '
from dictionary.columns
where libname="WORK" and
order by name;

data bac;
retain &reorder.;
set bac;
Note : Put library and dataset name in LIBNAME and MEMNAME (in caps) in the above code. RETAIN is used to reorder variables in a SAS dataset.

SAS Tutorials : 100 Free SAS Tutorials
About Author:

Deepanshu founded ListenData with a simple objective - Make analytics easy to understand and follow. He has over 8 years of experience in data science and predictive modeling. During his tenure, he has worked with global clients in various domains like Banking, Insurance, Telecom and Human Resource.

While I love having friends who agree, I only learn from those who don't.

Let's Get Connected: Email | LinkedIn

Get Free Email Updates :
*Please confirm your email address by clicking on the link sent to your Email*
Related Posts:
2 Responses to "SAS : Reordering Variables"
  1. Hi Deepanshu ,
    Will you please expalain above code a bit.

    1. it is quite simple. Basically accessing metadata within dictionary.columns table where variable information is stored. He is storing the variable names in a macro variable called 'reorder' ordered by name variable separated by spaces. This macro variable will then have value something like 'a b c'. This is used in the retain statement in the final data step where dataset bac is created again from same bac dataset.


Next → ← Prev