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-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

Register now!

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.

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
  • 5 replies
  • 1013 views
  • 3 likes
  • 2 in conversation