<?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 Reorde variables in one dataset based on another dataset in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Reorde-variables-in-one-dataset-based-on-another-dataset/m-p/894212#M353229</link>
    <description>&lt;P&gt;Hi,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I have a question about variable reordering.&amp;nbsp; I have one dataset that is called a and another dataset called b. The variables in dataset a are exactly the same as dataset B but the ordering is different.&amp;nbsp; I would like to reorder the variables in dataset A based on the order in dataset b.&amp;nbsp;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I know that retain can be used to reorder the variables. But I have many variables, using retain to order it one by one is not possible.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Thanks in advance for the help!&lt;/P&gt;</description>
    <pubDate>Thu, 14 Sep 2023 03:49:13 GMT</pubDate>
    <dc:creator>daradanye</dc:creator>
    <dc:date>2023-09-14T03:49:13Z</dc:date>
    <item>
      <title>Reorde variables in one dataset based on another dataset</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Reorde-variables-in-one-dataset-based-on-another-dataset/m-p/894212#M353229</link>
      <description>&lt;P&gt;Hi,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I have a question about variable reordering.&amp;nbsp; I have one dataset that is called a and another dataset called b. The variables in dataset a are exactly the same as dataset B but the ordering is different.&amp;nbsp; I would like to reorder the variables in dataset A based on the order in dataset b.&amp;nbsp;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I know that retain can be used to reorder the variables. But I have many variables, using retain to order it one by one is not possible.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Thanks in advance for the help!&lt;/P&gt;</description>
      <pubDate>Thu, 14 Sep 2023 03:49:13 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Reorde-variables-in-one-dataset-based-on-another-dataset/m-p/894212#M353229</guid>
      <dc:creator>daradanye</dc:creator>
      <dc:date>2023-09-14T03:49:13Z</dc:date>
    </item>
    <item>
      <title>Re: Reorde variables in one dataset based on another dataset</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Reorde-variables-in-one-dataset-based-on-another-dataset/m-p/894214#M353231</link>
      <description>&lt;P&gt;You can't reorder variables within a dataset.&amp;nbsp; It will have to be copied:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data new_a;
  set b (obs=0)  a;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The order of&amp;nbsp; vars in B will be honored, but the values will be from A.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Any variables in B, but not A, will be in NEW_A, but will be missing from first through last observation.&amp;nbsp; Any vars in A but not B will be to the right of the vars from B.&lt;/P&gt;</description>
      <pubDate>Thu, 14 Sep 2023 05:06:29 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Reorde-variables-in-one-dataset-based-on-another-dataset/m-p/894214#M353231</guid>
      <dc:creator>mkeintz</dc:creator>
      <dc:date>2023-09-14T05:06:29Z</dc:date>
    </item>
    <item>
      <title>Re: Reorde variables in one dataset based on another dataset</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Reorde-variables-in-one-dataset-based-on-another-dataset/m-p/894219#M353235</link>
      <description>&lt;P&gt;Another solution, just a minor variation of the code posted by &lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/31461"&gt;@mkeintz&lt;/a&gt; :&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data work.c;
   if 0 then set work.a;
   set work.b;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Thu, 14 Sep 2023 06:09:45 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Reorde-variables-in-one-dataset-based-on-another-dataset/m-p/894219#M353235</guid>
      <dc:creator>andreas_lds</dc:creator>
      <dc:date>2023-09-14T06:09:45Z</dc:date>
    </item>
    <item>
      <title>Re: Reorde variables in one dataset based on another dataset</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Reorde-variables-in-one-dataset-based-on-another-dataset/m-p/894274#M353262</link>
      <description>&lt;P&gt;Thanks!&amp;nbsp; One thing I forgot to mention is that: the length of some string variables are different in a and b (the string length is longer in a). For this code, I assume that the length of variables in B will be kept. Is there any way to keep the length of variables in dataset a but follow the order in dataset b?&amp;nbsp; Thanks.&lt;/P&gt;</description>
      <pubDate>Thu, 14 Sep 2023 14:39:44 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Reorde-variables-in-one-dataset-based-on-another-dataset/m-p/894274#M353262</guid>
      <dc:creator>daradanye</dc:creator>
      <dc:date>2023-09-14T14:39:44Z</dc:date>
    </item>
    <item>
      <title>Re: Reorde variables in one dataset based on another dataset</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Reorde-variables-in-one-dataset-based-on-another-dataset/m-p/894278#M353265</link>
      <description>&lt;P&gt;&lt;SPAN&gt;Thanks!&amp;nbsp; One thing I forgot to mention is that: the length of some string variables are different in a and b (the string length is longer in a). For this code, I assume that the length of variables in B will be kept. Is there any way to keep the length of variables in dataset a but follow the order in dataset b?&amp;nbsp; Thanks.&lt;/SPAN&gt;&lt;/P&gt;</description>
      <pubDate>Thu, 14 Sep 2023 14:57:41 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Reorde-variables-in-one-dataset-based-on-another-dataset/m-p/894278#M353265</guid>
      <dc:creator>daradanye</dc:creator>
      <dc:date>2023-09-14T14:57:41Z</dc:date>
    </item>
    <item>
      <title>Re: Reorde variables in one dataset based on another dataset</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Reorde-variables-in-one-dataset-based-on-another-dataset/m-p/894279#M353266</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/32269"&gt;@daradanye&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;Thanks!&amp;nbsp; One thing I forgot to mention is that: the length of some string variables are different in a and b (the string length is longer in a). For this code, I assume that the length of variables in B will be kept. Is there any way to keep the length of variables in dataset a but follow the order in dataset b?&amp;nbsp; Thanks.&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;Not really. Your initial statement is not true: " The variables in dataset a are exactly the same as dataset B but the ordering is different."&amp;nbsp;&lt;/P&gt;
&lt;P&gt;My first thought reading that was "and what about the LENGTHS of variables as that is often not considered".&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Question: WHY is the order of the variables in the data set important? SAS doesn't care. None of the analysis or reporting procedures care about the order of the variables in a set (order of variables on statements in syntax perhaps but not the data set).&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The only time it should be any concern is to &lt;FONT color="#800080"&gt;&lt;STRONG&gt;send the data to a different data system and you should take control&lt;/STRONG&gt; &lt;/FONT&gt;of that process to make sure it works properly.&lt;/P&gt;</description>
      <pubDate>Thu, 14 Sep 2023 17:15:28 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Reorde-variables-in-one-dataset-based-on-another-dataset/m-p/894279#M353266</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2023-09-14T17:15:28Z</dc:date>
    </item>
    <item>
      <title>Re: Reorde variables in one dataset based on another dataset</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Reorde-variables-in-one-dataset-based-on-another-dataset/m-p/894280#M353267</link>
      <description>&lt;P&gt;I need to send the dataset to my colleague. With proper order, it is easier for him to handle. He is handling the dataset in different software.&lt;/P&gt;</description>
      <pubDate>Thu, 14 Sep 2023 15:03:09 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Reorde-variables-in-one-dataset-based-on-another-dataset/m-p/894280#M353267</guid>
      <dc:creator>daradanye</dc:creator>
      <dc:date>2023-09-14T15:03:09Z</dc:date>
    </item>
    <item>
      <title>Re: Reorde variables in one dataset based on another dataset</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Reorde-variables-in-one-dataset-based-on-another-dataset/m-p/894288#M353273</link>
      <description>&lt;P&gt;Then you determine the order in the PUT statement of your export DATA step.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/32269"&gt;@daradanye&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;I need to send the dataset to my colleague. With proper order, it is easier for him to handle. He is handling the dataset in different software.&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 14 Sep 2023 15:24:04 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Reorde-variables-in-one-dataset-based-on-another-dataset/m-p/894288#M353273</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2023-09-14T15:24:04Z</dc:date>
    </item>
    <item>
      <title>Re: Reorde variables in one dataset based on another dataset</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Reorde-variables-in-one-dataset-based-on-another-dataset/m-p/894398#M353299</link>
      <description>&lt;P&gt;Are you exporting the data from sas dataset A to another format? Then maybe it makes the most sense to use&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/11562"&gt;@Kurt_Bremser&lt;/a&gt;'s suggestion of&amp;nbsp; a hard-coded PUT statement.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If you really must make a copy of dataset A, with vars ordered as in B, but with var lengths from A, then you can use the metadata resources found via the "dictionary" facility in PROC SQL&amp;nbsp; (see&amp;nbsp;&lt;A href="https://go.documentation.sas.com/doc/en/pgmsascdc/9.4_3.2/sqlproc/n02s19q65mw08gn140bwfdh7spx7.htm" target="_self"&gt;Accessing SAS Information By Using DICTIONARY Tables&lt;/A&gt;)&amp;nbsp; :&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data a ;
  length age 4 sex $10 name $40 ;
  set sashelp.class;
run;
data b;
  set sashelp.class;
run;

proc sql ;
  select catx(' ',dictb.name,_alength) into :LENGTHS_FROM_A_ORDERED_BY_B separated by '   ' 
  from
    (select name,varnum
      from dictionary.columns    where libname='WORK' and memname='B'  )
      as dictb

  inner join

    (select name,
      case when type='char' then cats('$',length)
           else cats(length)
      end 
      as _alength
      from dictionary.columns    where libname='WORK' and memname='A'  )
      as dicta

  on upcase(dicta.name)=upcase(dictb.name)
  order by dictb.varnum 
  ;
quit ;
%put &amp;amp;=lengths_from_a_ordered_by_b ;

data new_a;
  length &amp;amp;lengths_from_a_ordered_by_b ;
  set a;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;The PROC SQL above builds the arguments for a LENGTH statement with variables ordered by dataset B, but assigning lengths from A.&amp;nbsp; Those list of arguments get pasted into a macro variable, which is subsequently used in a LENGTH statement.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 14 Sep 2023 23:27:43 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Reorde-variables-in-one-dataset-based-on-another-dataset/m-p/894398#M353299</guid>
      <dc:creator>mkeintz</dc:creator>
      <dc:date>2023-09-14T23:27:43Z</dc:date>
    </item>
    <item>
      <title>Re: Reorde variables in one dataset based on another dataset</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Reorde-variables-in-one-dataset-based-on-another-dataset/m-p/894407#M353303</link>
      <description>&lt;P&gt;A SAS SQL UNION CORR ALL will create a table where the variable length is the maximum of any contributing source table and the variable order is taken from the first source table.&lt;/P&gt;
&lt;P&gt;You could use code similar to below example.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data work.have1;
   length var2 $20 var1 $15;
   var2='aaa';
   var1='bbb';
   output;
run;

data work.have2;
   length var1 $5 var2 $50;
   var1='yyy';
   var2='xxx';
   output;
run;

proc sql;
  create table want as
  select * from work.have1(obs=0)
  union corr all
  select * from work.have2
  ;
quit;

proc print data=want;
run;
proc contents data=want order=varnum;
quit;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Patrick_0-1694740222997.png" style="width: 400px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/88021i4644D222A2AE8DE8/image-size/medium?v=v2&amp;amp;px=400" role="button" title="Patrick_0-1694740222997.png" alt="Patrick_0-1694740222997.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Patrick_1-1694740237645.png" style="width: 400px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/88022iF37FF44232D3C912/image-size/medium?v=v2&amp;amp;px=400" role="button" title="Patrick_1-1694740237645.png" alt="Patrick_1-1694740237645.png" /&gt;&lt;/span&gt;&lt;/P&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, 15 Sep 2023 01:10:44 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Reorde-variables-in-one-dataset-based-on-another-dataset/m-p/894407#M353303</guid>
      <dc:creator>Patrick</dc:creator>
      <dc:date>2023-09-15T01:10:44Z</dc:date>
    </item>
  </channel>
</rss>

