BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
bncoxuk
Obsidian | Level 7

Any idea how I can reorder variables in a dataset? 

1 ACCEPTED SOLUTION

Accepted Solutions
art297
Opal | Level 21

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

3 REPLIES 3
art297
Opal | Level 21

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

data want;

   retain name height weight age;

  set sashelp.class;

run;

Peter_C
Rhodochrosite | Level 12

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

Linlin
Lapis Lazuli | Level 10

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

Ready to join fellow brilliant minds for the SAS Hackathon?

Build your skills. Make connections. Enjoy creative freedom. Maybe change the world. Registration is now open through August 30th. Visit the SAS Hackathon homepage.

Register today!
What is Bayesian Analysis?

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

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