<?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 Re: Moving new column to the end of the dataset in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Moving-new-column-to-the-end-of-the-dataset/m-p/879022#M347291</link>
    <description>&lt;P&gt;Thank you and I have run this sample code and it is working. Since, I have two new &lt;STRONG&gt;char&lt;/STRONG&gt; variables &lt;STRONG&gt;ch1&lt;/STRONG&gt; and &lt;STRONG&gt;ch2&lt;/STRONG&gt; and the data set &lt;STRONG&gt;test&lt;/STRONG&gt; has 25 columns. Now, I have tried the following:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;data test;&lt;/P&gt;&lt;P&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;if (0) then set test;&lt;/P&gt;&lt;P&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; length ch1 ch2 $12.;&lt;/P&gt;&lt;P&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; retain ch1 ch2 $12.;&lt;/P&gt;&lt;P&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; set test;&lt;/P&gt;&lt;P&gt;run;&lt;/P&gt;&lt;P&gt;It's not working.&amp;nbsp; Getting error:&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;ERROR 200-322: The symbol is not recognized and will be ignored.&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Can you please advise how to work with new char variables?&lt;/P&gt;</description>
    <pubDate>Fri, 02 Jun 2023 14:55:33 GMT</pubDate>
    <dc:creator>Moksha</dc:creator>
    <dc:date>2023-06-02T14:55:33Z</dc:date>
    <item>
      <title>Moving new column to the end of the dataset</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Moving-new-column-to-the-end-of-the-dataset/m-p/878952#M347267</link>
      <description>&lt;P&gt;Hi All,&lt;/P&gt;&lt;P&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;I have to process different datasets and add a column to it. But, this new column gets added to the beginning of the datasets. I want to move this to the end of all columns in each dataset. I know that&amp;nbsp; length, attrib, retain, format, informat can be used to reorder the columns as required, but they require to mention the columns names, but the problem here is that I don't know what other columns are there in each dataset since each dataset has different columns and each of them have 25+ columns.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Please,&amp;nbsp; advise how to reorder in this case and provide some sample code, if possible.&lt;/P&gt;</description>
      <pubDate>Fri, 02 Jun 2023 11:43:48 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Moving-new-column-to-the-end-of-the-dataset/m-p/878952#M347267</guid>
      <dc:creator>Moksha</dc:creator>
      <dc:date>2023-06-02T11:43:48Z</dc:date>
    </item>
    <item>
      <title>Re: Moving new column to the end of the dataset</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Moving-new-column-to-the-end-of-the-dataset/m-p/878955#M347268</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/439180"&gt;@Moksha&lt;/a&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Try this&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data &amp;lt;DsName);
   if (0) the Set &amp;lt;originalDsName&amp;gt;; /* would declare all the vars in the PDV */
   Length ...;
   set &amp;lt;originalDsName&amp;gt;;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Hope this helps&lt;/P&gt;</description>
      <pubDate>Fri, 02 Jun 2023 11:51:03 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Moving-new-column-to-the-end-of-the-dataset/m-p/878955#M347268</guid>
      <dc:creator>AhmedAl_Attar</dc:creator>
      <dc:date>2023-06-02T11:51:03Z</dc:date>
    </item>
    <item>
      <title>Re: Moving new column to the end of the dataset</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Moving-new-column-to-the-end-of-the-dataset/m-p/878969#M347270</link>
      <description>&lt;P&gt;I didn't understand&amp;nbsp;&lt;/P&gt;&lt;P&gt;what exactly we need give in length as I should not alter the lengths of the variables and I don't know the variable names also.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Can you please explain what is length ...;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thanks&lt;/P&gt;</description>
      <pubDate>Fri, 02 Jun 2023 12:48:12 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Moving-new-column-to-the-end-of-the-dataset/m-p/878969#M347270</guid>
      <dc:creator>Moksha</dc:creator>
      <dc:date>2023-06-02T12:48:12Z</dc:date>
    </item>
    <item>
      <title>Re: Moving new column to the end of the dataset</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Moving-new-column-to-the-end-of-the-dataset/m-p/878970#M347271</link>
      <description>&lt;P&gt;My opinion is that there is rarely, if ever, a compelling reason to move columns to the right or move them to the left. Most PROCs and DATA steps really don't care where in the data set the column exists.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;However, when you output the data set somehow for humans to look at, then the column order matters, but using PROC PRINT, PROC TABULATE, PROC REPORT you have complete control over the ordering of columns in a much more intuitive way, and this is much easier to program as well. For example, here's a way to print the SASHELP.CLASS data set with columns in different order:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc print data=sashelp.class;
    var age sex height weight name;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Note that I did not have to move NAME to the end of the data set using DATA step commands, and yet it will print as the last column.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;So&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/439180"&gt;@Moksha&lt;/a&gt;&amp;nbsp;, please re-consider the need to move columns around within a data set, and please consider using other tools to make columns appear in different places as shown above, this will make your programming much easier. It is a very rare situation where you really need to have columns in a specific order within a data set.&lt;/P&gt;</description>
      <pubDate>Fri, 02 Jun 2023 12:56:25 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Moving-new-column-to-the-end-of-the-dataset/m-p/878970#M347271</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2023-06-02T12:56:25Z</dc:date>
    </item>
    <item>
      <title>Re: Moving new column to the end of the dataset</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Moving-new-column-to-the-end-of-the-dataset/m-p/879006#M347278</link>
      <description>&lt;P&gt;I know that with proc print&amp;nbsp; using var will reorder as mentioned. But, I have requirement to have the newly added columns are at end.&amp;nbsp; Is it a limitation of SAS that there is no other alternative ways to reorder the columns in case we don't the column names and we have many number of columns which is not possible to list all columns. But, we have to list the columns names in all the ways that are available right now with SAS, but it's not possible to list all column names as we don't know the column names and the number of columns is huge.&amp;nbsp;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 02 Jun 2023 14:08:47 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Moving-new-column-to-the-end-of-the-dataset/m-p/879006#M347278</guid>
      <dc:creator>Moksha</dc:creator>
      <dc:date>2023-06-02T14:08:47Z</dc:date>
    </item>
    <item>
      <title>Re: Moving new column to the end of the dataset</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Moving-new-column-to-the-end-of-the-dataset/m-p/879009#M347280</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/439180"&gt;@Moksha&lt;/a&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The Length statement is for the new variables!&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;if (0) then SET &amp;lt;originalDataSet&amp;gt;; /* Will place all pre-existing variables in the PDV */&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Any other variables created after that statement, will appear at the end of the output data set. For illustration, run the code below.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data class;
	set sashelp.class(keep=name age sex obs=5);
run;
proc contents data=class varnum; run;

data class;
 if (0) then set class;
 Length newV1 newV2 newV3 3;
 retain newV1 newV2 newV3 0;
 set class;
run;
proc contents data=class varnum; run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Fri, 02 Jun 2023 14:23:39 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Moving-new-column-to-the-end-of-the-dataset/m-p/879009#M347280</guid>
      <dc:creator>AhmedAl_Attar</dc:creator>
      <dc:date>2023-06-02T14:23:39Z</dc:date>
    </item>
    <item>
      <title>Re: Moving new column to the end of the dataset</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Moving-new-column-to-the-end-of-the-dataset/m-p/879013#M347283</link>
      <description>&lt;P&gt;I don't understand.&amp;nbsp; That should be the NORMAL behavior.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
  set sashelp.class ;
  newvar = age ** 2;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Fri, 02 Jun 2023 14:30:38 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Moving-new-column-to-the-end-of-the-dataset/m-p/879013#M347283</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2023-06-02T14:30:38Z</dc:date>
    </item>
    <item>
      <title>Re: Moving new column to the end of the dataset</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Moving-new-column-to-the-end-of-the-dataset/m-p/879014#M347284</link>
      <description>&lt;BLOCKQUOTE&gt;
&lt;P&gt;&lt;SPAN&gt;I have requirement to have the newly added columns are at end&lt;/SPAN&gt;&lt;/P&gt;
&lt;/BLOCKQUOTE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Well, if someone has told you that specific columns HAVE TO BE on the right, I think this person has a misunderstanding which causes you to do unnecessary work. You might consider (politely) pushing back on this requirement.&lt;/P&gt;</description>
      <pubDate>Fri, 02 Jun 2023 14:54:58 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Moving-new-column-to-the-end-of-the-dataset/m-p/879014#M347284</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2023-06-02T14:54:58Z</dc:date>
    </item>
    <item>
      <title>Re: Moving new column to the end of the dataset</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Moving-new-column-to-the-end-of-the-dataset/m-p/879022#M347291</link>
      <description>&lt;P&gt;Thank you and I have run this sample code and it is working. Since, I have two new &lt;STRONG&gt;char&lt;/STRONG&gt; variables &lt;STRONG&gt;ch1&lt;/STRONG&gt; and &lt;STRONG&gt;ch2&lt;/STRONG&gt; and the data set &lt;STRONG&gt;test&lt;/STRONG&gt; has 25 columns. Now, I have tried the following:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;data test;&lt;/P&gt;&lt;P&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;if (0) then set test;&lt;/P&gt;&lt;P&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; length ch1 ch2 $12.;&lt;/P&gt;&lt;P&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; retain ch1 ch2 $12.;&lt;/P&gt;&lt;P&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; set test;&lt;/P&gt;&lt;P&gt;run;&lt;/P&gt;&lt;P&gt;It's not working.&amp;nbsp; Getting error:&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;ERROR 200-322: The symbol is not recognized and will be ignored.&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Can you please advise how to work with new char variables?&lt;/P&gt;</description>
      <pubDate>Fri, 02 Jun 2023 14:55:33 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Moving-new-column-to-the-end-of-the-dataset/m-p/879022#M347291</guid>
      <dc:creator>Moksha</dc:creator>
      <dc:date>2023-06-02T14:55:33Z</dc:date>
    </item>
    <item>
      <title>Re: Moving new column to the end of the dataset</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Moving-new-column-to-the-end-of-the-dataset/m-p/879023#M347292</link>
      <description>&lt;P&gt;The error message you posted would not be generated by that code (unless there are some invisible characters there, perhaps TAB or non-breaking spaces).&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;But if you just want to add two new variables then just add the two new variables. There is no need to do anything strange.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data test;
  set test;
  length ch1 ch2 $12;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;If the request is to MOVE existing variables to the end then use DROP= and KEEP= dataset options.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data test;
  set test(drop=ch1 ch2) test(keep=ch1 ch2);
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;If you want to MOVE them and also change the lengths use two SET statements so you can add the LENGTH statement in between.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data test;
  set test(drop=ch1 ch2);
  length ch1 ch2 $12;
  set test(keep=ch1 ch2);
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;PS Lengths can only be integers so there is no need to include a decimal point.&lt;/P&gt;</description>
      <pubDate>Fri, 02 Jun 2023 15:02:57 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Moving-new-column-to-the-end-of-the-dataset/m-p/879023#M347292</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2023-06-02T15:02:57Z</dc:date>
    </item>
    <item>
      <title>Re: Moving new column to the end of the dataset</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Moving-new-column-to-the-end-of-the-dataset/m-p/879024#M347293</link>
      <description>You have syntax error.&lt;BR /&gt;You cannot retain a length!</description>
      <pubDate>Fri, 02 Jun 2023 15:00:28 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Moving-new-column-to-the-end-of-the-dataset/m-p/879024#M347293</guid>
      <dc:creator>AhmedAl_Attar</dc:creator>
      <dc:date>2023-06-02T15:00:28Z</dc:date>
    </item>
    <item>
      <title>Re: Moving new column to the end of the dataset</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Moving-new-column-to-the-end-of-the-dataset/m-p/879030#M347296</link>
      <description>&lt;P&gt;I have tried multiple ways like retain without mentioning $12. or $12 and also length statement without . but since getting errors. I have posted only one code.&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 02 Jun 2023 15:12:28 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Moving-new-column-to-the-end-of-the-dataset/m-p/879030#M347296</guid>
      <dc:creator>Moksha</dc:creator>
      <dc:date>2023-06-02T15:12:28Z</dc:date>
    </item>
    <item>
      <title>Re: Moving new column to the end of the dataset</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Moving-new-column-to-the-end-of-the-dataset/m-p/879031#M347297</link>
      <description>&lt;P&gt;Thank you very much. The code provided by you:&amp;nbsp; has worked.&lt;/P&gt;&lt;PRE class=""&gt;&lt;CODE&gt;data test;
  set test(drop=ch1 ch2);
  length ch1 ch2 $12;
  set test(keep=ch1 ch2);
run;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 02 Jun 2023 15:14:19 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Moving-new-column-to-the-end-of-the-dataset/m-p/879031#M347297</guid>
      <dc:creator>Moksha</dc:creator>
      <dc:date>2023-06-02T15:14:19Z</dc:date>
    </item>
    <item>
      <title>Re: Moving new column to the end of the dataset</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Moving-new-column-to-the-end-of-the-dataset/m-p/879041#M347299</link>
      <description>&lt;P&gt;If you are one of those people that needlessly attach $ formats to character variables (or you had them attached for you by SAS generated code) then you might also want to remove any format attach to those moved variables by adding&amp;nbsp; this statement to the end of your data step.&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;format ch1 ch2 ;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;The problem with attaching the $ format to a character variable is when the width of the format is different from the length of the variable it can cause problems.&amp;nbsp; &amp;nbsp;If the width is less than the length then when you print the values they look truncated.&amp;nbsp; It the width is larger then you might accidentally try to assign too long a value to the variable and then it will actually be truncated.&lt;/P&gt;</description>
      <pubDate>Fri, 02 Jun 2023 15:23:05 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Moving-new-column-to-the-end-of-the-dataset/m-p/879041#M347299</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2023-06-02T15:23:05Z</dc:date>
    </item>
    <item>
      <title>Re: Moving new column to the end of the dataset</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Moving-new-column-to-the-end-of-the-dataset/m-p/879042#M347300</link>
      <description>&lt;P&gt;Do you know the name of the new column ahead of time or need to determine it dynamically?&lt;/P&gt;
&lt;P&gt;As Tom mentioned, this should be the default behaviour so sharing how you are creating this variable might be helpful. Could be that a small change in code could accomplish what you need without further modifications.&lt;/P&gt;</description>
      <pubDate>Fri, 02 Jun 2023 15:24:01 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Moving-new-column-to-the-end-of-the-dataset/m-p/879042#M347300</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2023-06-02T15:24:01Z</dc:date>
    </item>
  </channel>
</rss>

