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

Hi Everyone,

 

I have a large number of columns and I want to move var1 var2 var3 to the end.

Since there are many column, I dont want to list too many column in retain.

I can reorder by taking these 3 column out and SQL it back to the main file.

 

However, I wonder if there is any easy way to do it.

 

Thank you,

 

HHCFX

1 ACCEPTED SOLUTION

Accepted Solutions
Astounding
PROC Star
Try it this way:

data want;
if 5=4 then do;
set have (drop=var1 var2 var3);
retain var1 var2 var3;
end;
set have;
run;

Technically, you may not need the retain statement. It gives you control over the order of the last 3 variables.

View solution in original post

7 REPLIES 7
Astounding
PROC Star
Try it this way:

data want;
if 5=4 then do;
set have (drop=var1 var2 var3);
retain var1 var2 var3;
end;
set have;
run;

Technically, you may not need the retain statement. It gives you control over the order of the last 3 variables.
RW9
Diamond | Level 26 RW9
Diamond | Level 26

Interesting idea, so you have a condition which can never be met around the first step which creates all but the var variables in the PDV.  Then add the var variables with retain, then add the data.  You would have thought you could do this in proc datasets by now.

 

OP: Why do you need to move these?  Am just thinking, if its for a report, then your better off just setting the position in the reporting procedure when you assign labels and styles etc.  Combine the two saves time and maintenance.  If its to follow a specific template for the data, you may be better off creating a template of what the file should look like (all formats, informats, labels etc.) in a file with no observations, then setting that with your data when needed, this way you only need to set structure in the template (one place).  There is never, in programming terms a need to move columns as logical position is irrelevant.  

hhchenfx
Barite | Level 11

I am working with an existing report each month and need to reorder thing around.

Thanks.

HHC

PaigeMiller
Diamond | Level 26

@hhchenfx wrote:

I am working with an existing report each month and need to reorder thing around.

Thanks.

HHC


Any report method in SAS has re-ordering of the columns built in.

 

In PROC PRINT, it's the VAR command to re-order columns. In PROC REPORT, it's the COLUMNS command to re-order columns. In PROC TABULATE, use the {I can't remember} command to re-order columns.

 

So don't do any programming to re-order columns, just use one of the above tools.

--
Paige Miller
hhchenfx
Barite | Level 11

Thank you for your suggestion but I am not familiar with those proc.

HHC

RW9
Diamond | Level 26 RW9
Diamond | Level 26

Proc print, report, and tabulate are the standard output procedures of SAS.  If you don't know those then you have more problems than just re-arranging.  Each procedure has a method to arrange output in a certain way and print or not to print certain variables.  Rather than wasting processor cycles and read/write cycles creating a new dataset, use the procedure options.  E.g.:

proc report ...;
  columns ... var1 var2 var3;
...
run;
PaigeMiller
Diamond | Level 26

@hhchenfx wrote:

Thank you for your suggestion but I am not familiar with those proc.

HHC


Then how are you doing the reporting? What PROC are you using? Or are you using a DATA step, with PUT statements?

 

I add that there are many powerful reporting capabilities in these PROCs, and in the long run, you will get a lot more done, and do it a lot more easily, by learning and using these PROCs. In particular, for relative simple reports, PROC PRINT is about as easy to use as can be.

--
Paige Miller

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

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
  • 7 replies
  • 717 views
  • 1 like
  • 4 in conversation