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

Hey Follow SAS Users!

 

I have a quick question which I hope you can help with. 

 

If I have a dataset with many columns and I just want to re-order a couple of them towards the end of the dataset, is there something simple I can use?  Using retain and re-ordering the 25th and 26th variables (y and z in this case), I would have to do the following code for example:

 

data work.order;

  retain a b c d e f g h i j k l m n o p q r s t u v w x z y;

  set x.order;

run;

 

So, I would have to enter the preceding 24 variables before I can amend the order of the last two at the end.  Anything I'm missing/can be done to avoid entering the preceding 24 variables?

 

Hope that's clear Smiley Happy

 

I did search and found lots on retain but couldn't find anything that specifically helps with the above.

 

Thanks in advance all.

 

 

1 ACCEPTED SOLUTION

Accepted Solutions
Astounding
PROC Star

Here's a way to get around that:

 

data want;

if 5=4 then set have (drop=y z);

retain z y;

set have;

run;

 

The first SET statement doesn't actually read any data (unless you invent some new type of math where 5=4).  But it orders all variables except Y and Z.

View solution in original post

5 REPLIES 5
Astounding
PROC Star

Here's a way to get around that:

 

data want;

if 5=4 then set have (drop=y z);

retain z y;

set have;

run;

 

The first SET statement doesn't actually read any data (unless you invent some new type of math where 5=4).  But it orders all variables except Y and Z.

Brickinnit
Fluorite | Level 6

Thanks for the reply!!  I've never seen that before but it worked fine for my dataset.  It's amazing how much there is to learn in SAS even if you 'think' you are fairly proficient Smiley Happy

 

Thanks again for your time and what a very helpful community Smiley Happy

Brickinnit
Fluorite | Level 6

@Astounding wrote:

Here's a way to get around that:

 

data want;

if 5=4 then set have (drop=y z);

retain z y;

set have;

run;

 

The first SET statement doesn't actually read any data (unless you invent some new type of math where 5=4).  But it orders all variables except Y and Z.


 

Sorry 'Astounding' one quick question - what does the 'if 5=4 then' actually do?  If you take that away, it still works - i.e., code:

 

data want;

set have (drop=y z);

retain z y;

set have;

run;

 

Thanks!

Astounding
PROC Star

Since 5=4 is false, the first SET statement never executes.  While you can remove it, the DATA step takes longer to run that way because the first SET statement now actually reads in data.  I'm not sure if this is clever or not, but you could split up the work between the two SET statements:

 

data want;

set have (drop=y z);

retain z y;

set have (keep=y z);

run;

Brickinnit
Fluorite | Level 6

Ah ha - OK!  Thanks for the info once again

SAS Innovate 2025: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

How to Concatenate Values

Learn how use the CAT functions in SAS to join values from multiple variables into a single value.

Find more tutorials on the SAS Users YouTube channel.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 5 replies
  • 1451 views
  • 3 likes
  • 2 in conversation