Help using Base SAS procedures

How can I re-order variables in a dataset

Accepted Solution Solved
Reply
Frequent Contributor
Posts: 131
Accepted Solution

How can I re-order variables in a dataset

Any idea how I can reorder variables in a dataset? 


Accepted Solutions
Solution
‎01-18-2012 10:54 AM
PROC Star
Posts: 7,362

How can I re-order variables in a dataset

Use a retain statement but place it before the set statement.  e.g.:

data want;

   retain name height weight age;

  set sashelp.class;

run;

View solution in original post


All Replies
Solution
‎01-18-2012 10:54 AM
PROC Star
Posts: 7,362

How can I re-order variables in a dataset

Use a retain statement but place it before the set statement.  e.g.:

data want;

   retain name height weight age;

  set sashelp.class;

run;

Valued Guide
Posts: 2,174

Re: How can I re-order variables in a dataset

Art

pardon me for butting in ;-)

I would like to suggest a significant change to this "standard" re-ordering.

Rather than re-create the data in the order required (this time?) create a VIEW.

Then only when columns are being read will that data be rearranged.

(and it probably makes no difference to the performance of reading the data)

OK a view might require a little storage, but probably very very little.

Once the concept is accepted, a few advantages become apparent!

For a substantial (or even BIG) data set - expect to make more than one re-arrangement - it's only another VIEW!

Create VIEWS for every new order requested.

Obviously the VIEW process will vary only in the order required and the name of the VIEW to be created. So that suggests a macro. Perhaps it might be as simple as your code wrapped in a macro like:

%macro new_view( data= input_table, VIEW= new_VIEW, columns= a b c ) ;

data &VIEW / view= &VIEW ;

retain &columns ;

set &data ;

run ;

%mend ;

but you know me ...

for a basic capability, I expect it to be reused many times until, it is used by those who might not understand it so well. Preparing for that, I add checks that the input exists, and can be opened, and the output is not already in existence (unless the caller adds a parameter to confirm "replace") so I tested the macro %new_view, attached as the file new_view.sas

Usage

%new_view( data= sashelp.vformat, view= sasuser.vformat, columns fmtName fmtType minw defw maxw mind defd maxd );

regards

Peter

Attachment
Super Contributor
Posts: 1,636

Re: How can I re-order variables in a dataset

using retain statement:

example:

data class;

  set sashelp.class (obs=5);

proc print;run;

data class2;

retain height weight age name sex;

set class;

proc print;run;

Linlin

☑ This topic is SOLVED.

Need further help from the community? Please ask a new question.

Discussion stats
  • 3 replies
  • 216 views
  • 3 likes
  • 4 in conversation