DATA Step, Macro, Functions and more

How to Move Column to the end

Accepted Solution Solved
Reply
Frequent Contributor
Posts: 94
Accepted Solution

How to Move Column to the end

I have a big report that have 90 columns (I have used proc sql- Combined many datasets finally). 
Is there any way, to move the 60 th and 61st column to the end.
I don't want to list all the columns in the Select Column. Wanted to move only few columns to the end and rest should stay same.


Accepted Solutions
Solution
‎02-08-2018 02:34 PM
Super User
Posts: 8,220

Re: How to Move Column to the end

Posted in reply to Kalai2008

My error! I hadn't tested the solution.

 

Does it have to be using proc sql? If not, the following data step will work:

data have;
  input col5 col61 col4 col12 col62 col63 col112;
  cards;
1 2 3 4 5 6 7
2 3 4 5 6 7 8
;

data combined;
   set have (drop=col61 col62);
   set have (keep=col61 col62);
run;

Art, CEO, AnalystFinder.com

 

View solution in original post


All Replies
Super User
Super User
Posts: 9,855

Re: How to Move Column to the end

Posted in reply to Kalai2008

A report containing 90 variables, would be interested in seeing the code which gets that onto an A4 bit of paper.  Anyways, not a simple method - order of columns in SAS does not matter you see.  What you can do is create a list of all variables except the two you want to move then add those, something like:

proc sql;
  select name 
  into :vlist
  from sashelp.vcolumn
  where name not in ("A","B");
quit;

data want;
  keep=&vlist. A B;
  set yourdata;
run;

However, if your creating one large data from many using select statements, ordering variables at the point would be better rather than moving around at the end.

Frequent Contributor
Posts: 94

Re: How to Move Column to the end

Appreciate your response.. Thank you.. I will try yours and check if its working.

Super User
Posts: 8,220

Re: How to Move Column to the end

Posted in reply to Kalai2008

You could always concatenate the file twice using drop and keep statements. e.g.:

proc sql;
   create table combined as
      select * from have (drop=col61 col62)
      outer union corr
      select * from have (keep=col61 col62)
  ;
 quit;

Art, CEO, AnalystFinder.com

 

Frequent Contributor
Posts: 94

Re: How to Move Column to the end

Thank you for the solution. The 60th column moved to end but with empty columns. Do you know why is the missing data in the column? I tried removing the CORR too.

Solution
‎02-08-2018 02:34 PM
Super User
Posts: 8,220

Re: How to Move Column to the end

Posted in reply to Kalai2008

My error! I hadn't tested the solution.

 

Does it have to be using proc sql? If not, the following data step will work:

data have;
  input col5 col61 col4 col12 col62 col63 col112;
  cards;
1 2 3 4 5 6 7
2 3 4 5 6 7 8
;

data combined;
   set have (drop=col61 col62);
   set have (keep=col61 col62);
run;

Art, CEO, AnalystFinder.com

 

Frequent Contributor
Posts: 94

Re: How to Move Column to the end

Thank you.. I already tried that way and it worked.. Thank you so much.

☑ This topic is solved.

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

Discussion stats
  • 6 replies
  • 375 views
  • 5 likes
  • 3 in conversation