<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic What is the best way to change column order when adding new columns? in New SAS User</title>
    <link>https://communities.sas.com/t5/New-SAS-User/What-is-the-best-way-to-change-column-order-when-adding-new/m-p/984881#M43731</link>
    <description>&lt;DIV&gt;In SAS Studio 9.4v2:&lt;/DIV&gt;&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;&lt;DIV&gt;data np_summary_update;&lt;/DIV&gt;&lt;DIV&gt;&lt;SPAN&gt;set pg1.np_summary;&lt;/SPAN&gt;&lt;/DIV&gt;&lt;DIV&gt;&lt;SPAN&gt;keep Reg ParkName SqMiles Camping DayVisits OtherLodging Acres ; &lt;/SPAN&gt;&lt;/DIV&gt;&lt;DIV&gt;&lt;SPAN&gt;SqMiles = acres * .0015625;&amp;nbsp;&lt;/SPAN&gt;&lt;/DIV&gt;&lt;DIV&gt;&lt;SPAN&gt;Format SqMiles comma6.;&lt;/SPAN&gt;&lt;/DIV&gt;&lt;DIV&gt;&lt;SPAN&gt;Camping = sum (OtherCamping, TentCampers, RVCampers, BackCountryCampers);&lt;/SPAN&gt;&lt;/DIV&gt;&lt;DIV&gt;&lt;SPAN&gt;Format Camping comma11.;&lt;/SPAN&gt;&lt;/DIV&gt;&lt;DIV&gt;&lt;SPAN&gt;Retain&amp;nbsp; Reg ParkName Acres SqMiles Camping DayVisits OtherLodging ;&lt;/SPAN&gt;&lt;/DIV&gt;&lt;DIV&gt;&lt;SPAN&gt;set pg1.np_summary;&lt;/SPAN&gt;&lt;/DIV&gt;&lt;DIV&gt;&lt;SPAN&gt;data want;&lt;/SPAN&gt;&lt;/DIV&gt;&lt;DIV&gt;run;&lt;/DIV&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Output produces columns in this order:&amp;nbsp; Reg ParkName&amp;nbsp; DayVisits OtherLodging Acres SqMiles Camping.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;This is my code from a SAS Programming 1 exercise. I understood the initial assignment and solved it. I then started futzing around looking for a way to move around the newly created columns in the KEEP statement. I have tried a few things which didn't work. The code above is just the last stab at it. Suggestions?&amp;nbsp;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Also, code with an explanation why would be deeply appreciated.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thank you.&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Tue, 17 Mar 2026 16:16:35 GMT</pubDate>
    <dc:creator>musicandyceram</dc:creator>
    <dc:date>2026-03-17T16:16:35Z</dc:date>
    <item>
      <title>What is the best way to change column order when adding new columns?</title>
      <link>https://communities.sas.com/t5/New-SAS-User/What-is-the-best-way-to-change-column-order-when-adding-new/m-p/984881#M43731</link>
      <description>&lt;DIV&gt;In SAS Studio 9.4v2:&lt;/DIV&gt;&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;&lt;DIV&gt;data np_summary_update;&lt;/DIV&gt;&lt;DIV&gt;&lt;SPAN&gt;set pg1.np_summary;&lt;/SPAN&gt;&lt;/DIV&gt;&lt;DIV&gt;&lt;SPAN&gt;keep Reg ParkName SqMiles Camping DayVisits OtherLodging Acres ; &lt;/SPAN&gt;&lt;/DIV&gt;&lt;DIV&gt;&lt;SPAN&gt;SqMiles = acres * .0015625;&amp;nbsp;&lt;/SPAN&gt;&lt;/DIV&gt;&lt;DIV&gt;&lt;SPAN&gt;Format SqMiles comma6.;&lt;/SPAN&gt;&lt;/DIV&gt;&lt;DIV&gt;&lt;SPAN&gt;Camping = sum (OtherCamping, TentCampers, RVCampers, BackCountryCampers);&lt;/SPAN&gt;&lt;/DIV&gt;&lt;DIV&gt;&lt;SPAN&gt;Format Camping comma11.;&lt;/SPAN&gt;&lt;/DIV&gt;&lt;DIV&gt;&lt;SPAN&gt;Retain&amp;nbsp; Reg ParkName Acres SqMiles Camping DayVisits OtherLodging ;&lt;/SPAN&gt;&lt;/DIV&gt;&lt;DIV&gt;&lt;SPAN&gt;set pg1.np_summary;&lt;/SPAN&gt;&lt;/DIV&gt;&lt;DIV&gt;&lt;SPAN&gt;data want;&lt;/SPAN&gt;&lt;/DIV&gt;&lt;DIV&gt;run;&lt;/DIV&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Output produces columns in this order:&amp;nbsp; Reg ParkName&amp;nbsp; DayVisits OtherLodging Acres SqMiles Camping.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;This is my code from a SAS Programming 1 exercise. I understood the initial assignment and solved it. I then started futzing around looking for a way to move around the newly created columns in the KEEP statement. I have tried a few things which didn't work. The code above is just the last stab at it. Suggestions?&amp;nbsp;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Also, code with an explanation why would be deeply appreciated.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thank you.&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 17 Mar 2026 16:16:35 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/What-is-the-best-way-to-change-column-order-when-adding-new/m-p/984881#M43731</guid>
      <dc:creator>musicandyceram</dc:creator>
      <dc:date>2026-03-17T16:16:35Z</dc:date>
    </item>
    <item>
      <title>Re: What is the best way to change column order when adding new columns?</title>
      <link>https://communities.sas.com/t5/New-SAS-User/What-is-the-best-way-to-change-column-order-when-adding-new/m-p/984885#M43732</link>
      <description>&lt;P&gt;Basically, column position makes no difference to SAS. Anything you want to do in SAS works regardless of order of the columns in the data set (although there might be some rare exceptions).&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The only time column position makes a difference is when you are creating an output that needs to be viewed by humans and then a logical order of columns is needed &lt;EM&gt;in the output&lt;/EM&gt;, not in the SAS data set. So with that in mind, the columns positions can be &lt;FONT color="#FF0000"&gt;easily&lt;/FONT&gt; specified in output PROCs, such as PRINT, TABULATE and REPORT. Since you asked for the "best way", I believe this to be the "best way".&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Thus my advice is to not bother re-arranging the columns in a data set. Re-arranging the columns when you output the results is the time to do it.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 17 Mar 2026 18:14:30 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/What-is-the-best-way-to-change-column-order-when-adding-new/m-p/984885#M43732</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2026-03-17T18:14:30Z</dc:date>
    </item>
    <item>
      <title>Re: What is the best way to change column order when adding new columns?</title>
      <link>https://communities.sas.com/t5/New-SAS-User/What-is-the-best-way-to-change-column-order-when-adding-new/m-p/984894#M43733</link>
      <description>&lt;P&gt;Create a template dataset.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;title "Template data to reorder variables";
data template;
   attrib Trt    length=8  label='Treatment';
   attrib Sex    length=$1 label='Gender';
   attrib Age    length=8  label='Age (years)';
   attrib Weight length=8;
   attrib Height length=8;
   attrib Name   length=$10 format=$quote12.;
   stop;
   call missing(of _all_);
   run;

data class;
   set template sashelp.class;
   trt = rantbl(12345,.6)-1;
   run;
proc contents varnum;
   run;
proc print;
   run;

proc contents data=sashelp.class varnum;
   run;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Screenshot 2026-03-17 123149.png" style="width: 289px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/113665i947738AE9A2FDAA4/image-size/large?v=v2&amp;amp;px=999" role="button" title="Screenshot 2026-03-17 123149.png" alt="Screenshot 2026-03-17 123149.png" /&gt;&lt;/span&gt;&lt;/P&gt;</description>
      <pubDate>Tue, 17 Mar 2026 17:34:01 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/What-is-the-best-way-to-change-column-order-when-adding-new/m-p/984894#M43733</guid>
      <dc:creator>data_null__</dc:creator>
      <dc:date>2026-03-17T17:34:01Z</dc:date>
    </item>
    <item>
      <title>Re: What is the best way to change column order when adding new columns?</title>
      <link>https://communities.sas.com/t5/New-SAS-User/What-is-the-best-way-to-change-column-order-when-adding-new/m-p/984914#M43734</link>
      <description>&lt;P&gt;PROC SQL ?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;See&amp;nbsp;&lt;/P&gt;
&lt;P&gt;SESUG Paper 242-2018&lt;/P&gt;
&lt;P&gt;&lt;STRONG&gt;Order, Order! Four Ways To Reorder Your Variables, Ranked by Elegance and Efficiency &lt;/STRONG&gt;&lt;/P&gt;
&lt;P&gt;Louise S. Hadden, Abt Associates Inc.&lt;/P&gt;
&lt;P&gt;&lt;A href="https://www.lexjansen.com/sesug/2018/SESUG2018_Paper-242_Final_PDF.pdf" target="_blank"&gt;https://www.lexjansen.com/sesug/2018/SESUG2018_Paper-242_Final_PDF.pdf&lt;/A&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;BR, Koen&lt;/P&gt;</description>
      <pubDate>Tue, 17 Mar 2026 23:32:12 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/What-is-the-best-way-to-change-column-order-when-adding-new/m-p/984914#M43734</guid>
      <dc:creator>sbxkoenk</dc:creator>
      <dc:date>2026-03-17T23:32:12Z</dc:date>
    </item>
    <item>
      <title>Re: What is the best way to change column order when adding new columns?</title>
      <link>https://communities.sas.com/t5/New-SAS-User/What-is-the-best-way-to-change-column-order-when-adding-new/m-p/984976#M43741</link>
      <description>&lt;P&gt;The data step compiler adds the variables to the dataset in the order.&amp;nbsp; So to change the order make sure the code references the variables in the order you want.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Not all references actually cause SAS to need to place the variable in the dataset at that time. For example the DROP or&amp;nbsp;KEEP statement does not impact order (even if it is the first place the variable is referenced).&amp;nbsp; Instead they just set flags to indicate if the variable will be written to the output dataset.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The data step compiler will also set the type (and storage length) of the variable at the first place where it HAS to.&amp;nbsp; But not all references will set the type.&amp;nbsp; For example the RETAIN statement will set the variables place, but it will not determine its TYPE.&amp;nbsp; That will be (or has been) determined by where it is used.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;So you can use RETAIN to set the order.&amp;nbsp; Just be sure that your data step will not be impacted by the actual purpose of a RETAIN statement, that the variables value is not clear when a new iteration starts.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Example:&lt;/P&gt;
&lt;PRE&gt; 73         data _null_;
 74           keep height age name ;
 75           set sashelp.class(obs=1);
 76           put (_all_) (=/);
 77         run;
 
 Name=Alfred
 Sex=M
 Age=14
 Height=69
 Weight=112.5
 NOTE: There were 1 observations read from the data set SASHELP.CLASS.
 NOTE: DATA statement used (Total process time):
       real time           0.00 seconds
       cpu time            0.00 seconds
       
 
 78         
 79         data _null_;
 80           retain height age name ;
 81           set sashelp.class(obs=1);
 82           put (_all_) (=/);
 83         run;
 
 height=69
 age=14
 name=Alfred
 Sex=M
 Weight=112.5
 NOTE: There were 1 observations read from the data set SASHELP.CLASS.
 NOTE: DATA statement used (Total process time):
       real time           0.00 seconds
       cpu time            0.00 seconds&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Notice that the RETAIN statement also set the case of the variable names and the KEEP statement did not.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;To really set the order you should probably just DEFINE all of the variables first using a LENGTH statement.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
  length age height 8 Sex $1 Name $10 weight 8;
  set sashelp.class(obs=1);
run;

proc contents varnum;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;But if you don't know how to define the existing variable then perhaps you could just use a series of references to the dataset to let the data step compiler find it for you.&amp;nbsp; Say you want to move AGE to the front and leave the others in place.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
  set sashelp.class(keep=age);
  set sashelp.class;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 18 Mar 2026 18:52:07 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/What-is-the-best-way-to-change-column-order-when-adding-new/m-p/984976#M43741</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2026-03-18T18:52:07Z</dc:date>
    </item>
    <item>
      <title>Re: What is the best way to change column order when adding new columns?</title>
      <link>https://communities.sas.com/t5/New-SAS-User/What-is-the-best-way-to-change-column-order-when-adding-new/m-p/984977#M43742</link>
      <description>&lt;P&gt;I don't know about you but I actually like to LOOK at my data.&amp;nbsp; That is very difficult when the variables are placed randomly.&amp;nbsp; There is nothing worse than trying to debug something and looking at a dataset here the whole first screen is full of long empty character variables of low importance.&lt;/P&gt;</description>
      <pubDate>Wed, 18 Mar 2026 18:44:15 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/What-is-the-best-way-to-change-column-order-when-adding-new/m-p/984977#M43742</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2026-03-18T18:44:15Z</dc:date>
    </item>
  </channel>
</rss>

