<?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: Reorder the variables after data type conversion in SAS Enterprise Guide</title>
    <link>https://communities.sas.com/t5/SAS-Enterprise-Guide/Reorder-the-variables-after-data-type-conversion/m-p/589085#M34673</link>
    <description>&lt;P&gt;First of all, the order of variables rarely matters in SAS. If this is a one time thing, you can do the following:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;1) Print the variables from the original data set in the log ordered by their initial order.&lt;/P&gt;
&lt;P&gt;2) Copy/Paste there into a Format Statement before the Set Statement.&lt;/P&gt;
&lt;P&gt;3) Change the two variable names to your liking.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;See this small example&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql noprint;
	select name into :vars separated by ' '
	from dictionary.columns
	where libname='SASHELP' and memname='CLASS'
	order by varnum;
quit;

%put &amp;amp;vars.;

data want;
	format Name Sex Age NewHeight Weight; /* Copied from log and changed height to NewHeight */
	set sashelp.class;
	newHeight=put(Height, $8. -l);
	drop height;
	rename newHeight=Height;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
    <pubDate>Mon, 16 Sep 2019 15:24:34 GMT</pubDate>
    <dc:creator>PeterClemmensen</dc:creator>
    <dc:date>2019-09-16T15:24:34Z</dc:date>
    <item>
      <title>Reorder the variables after data type conversion</title>
      <link>https://communities.sas.com/t5/SAS-Enterprise-Guide/Reorder-the-variables-after-data-type-conversion/m-p/589075#M34668</link>
      <description>&lt;P&gt;Hi All,&lt;/P&gt;&lt;P&gt;I used the data step to covert a few variables from charater to numeric.&lt;/P&gt;&lt;P&gt;&lt;FONT color="#000080" face="Courier New" size="3"&gt;&lt;STRONG&gt;data&lt;/STRONG&gt;&lt;/FONT&gt;&lt;FONT face="Courier New" size="3"&gt; NEW;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT color="#0000ff" face="Courier New" size="3"&gt;SET&lt;/FONT&gt;&lt;FONT face="Courier New" size="3"&gt; HAVE;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT face="Courier New" size="3"&gt;Account_New=input(Account, &lt;/FONT&gt;&lt;STRONG&gt;&lt;FONT color="#008080" face="Courier New" size="3"&gt;10.&lt;/FONT&gt;&lt;/STRONG&gt;&lt;FONT face="Courier New" size="3"&gt;);&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT face="Courier New" size="3"&gt;Bor_New=input(Bor, &lt;/FONT&gt;&lt;STRONG&gt;&lt;FONT color="#008080" face="Courier New" size="3"&gt;3.&lt;/FONT&gt;&lt;/STRONG&gt;&lt;FONT face="Courier New" size="3"&gt;);&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT color="#0000ff" face="Courier New" size="3"&gt;drop&lt;/FONT&gt;&lt;FONT face="Courier New" size="3"&gt; Account Bor;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT color="#0000ff" face="Courier New" size="3"&gt;rename&lt;/FONT&gt;&lt;FONT face="Courier New" size="3"&gt; Account_New=Account Bor_New=Bor;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT color="#000080" face="Courier New" size="3"&gt;&lt;STRONG&gt;run&lt;/STRONG&gt;&lt;/FONT&gt;&lt;FONT face="Courier New" size="3"&gt;;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;FONT face="Courier New" size="3"&gt;After I did this, the order of variables changed. Is there any way to restore back the original order of each columns? There are about 50 columns and Account is the first one. So dropping&amp;nbsp;this variable&amp;nbsp;from the data step technically changed the order of all other columns.&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;FONT face="Courier New" size="3"&gt;Any help is much appreciated! &lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT face="Courier New" size="3"&gt;Thanks.&lt;/FONT&gt;&lt;/P&gt;</description>
      <pubDate>Mon, 16 Sep 2019 15:01:38 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Enterprise-Guide/Reorder-the-variables-after-data-type-conversion/m-p/589075#M34668</guid>
      <dc:creator>MelissaN</dc:creator>
      <dc:date>2019-09-16T15:01:38Z</dc:date>
    </item>
    <item>
      <title>Re: Reorder the variables after data type conversion</title>
      <link>https://communities.sas.com/t5/SAS-Enterprise-Guide/Reorder-the-variables-after-data-type-conversion/m-p/589076#M34669</link>
      <description>&lt;P&gt;You can use a Format Statement &lt;EM&gt;before&amp;nbsp;&lt;/EM&gt;the set statement like this to make Account_New the first variable in the PDV.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data NEW;
	format Account_New;
	SET HAVE;
	Account_New=input(Account, 10.);
	Bor_New=input(Bor, 3.);
	drop Account Bor;
	rename Account_New=Account Bor_New=Bor;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Mon, 16 Sep 2019 15:05:28 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Enterprise-Guide/Reorder-the-variables-after-data-type-conversion/m-p/589076#M34669</guid>
      <dc:creator>PeterClemmensen</dc:creator>
      <dc:date>2019-09-16T15:05:28Z</dc:date>
    </item>
    <item>
      <title>Re: Reorder the variables after data type conversion</title>
      <link>https://communities.sas.com/t5/SAS-Enterprise-Guide/Reorder-the-variables-after-data-type-conversion/m-p/589077#M34670</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/274276"&gt;@MelissaN&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;Hi All,&lt;/P&gt;
&lt;P&gt;I used the data step to covert a few variables from charater to numeric.&lt;/P&gt;
&lt;P&gt;&lt;FONT color="#000080" face="Courier New" size="3"&gt;&lt;STRONG&gt;data&lt;/STRONG&gt;&lt;/FONT&gt;&lt;FONT face="Courier New" size="3"&gt; NEW;&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT color="#0000ff" face="Courier New" size="3"&gt;SET&lt;/FONT&gt;&lt;FONT face="Courier New" size="3"&gt; HAVE;&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face="Courier New" size="3"&gt;Account_New=input(Account, &lt;/FONT&gt;&lt;STRONG&gt;&lt;FONT color="#008080" face="Courier New" size="3"&gt;10.&lt;/FONT&gt;&lt;/STRONG&gt;&lt;FONT face="Courier New" size="3"&gt;);&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face="Courier New" size="3"&gt;Bor_New=input(Bor, &lt;/FONT&gt;&lt;STRONG&gt;&lt;FONT color="#008080" face="Courier New" size="3"&gt;3.&lt;/FONT&gt;&lt;/STRONG&gt;&lt;FONT face="Courier New" size="3"&gt;);&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT color="#0000ff" face="Courier New" size="3"&gt;drop&lt;/FONT&gt;&lt;FONT face="Courier New" size="3"&gt; Account Bor;&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT color="#0000ff" face="Courier New" size="3"&gt;rename&lt;/FONT&gt;&lt;FONT face="Courier New" size="3"&gt; Account_New=Account Bor_New=Bor;&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT color="#000080" face="Courier New" size="3"&gt;&lt;STRONG&gt;run&lt;/STRONG&gt;&lt;/FONT&gt;&lt;FONT face="Courier New" size="3"&gt;;&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;FONT face="Courier New" size="3"&gt;After I did this, the order of variables changed. Is there any way to restore back the original order of each columns? There are about 50 columns and Account is the first one. So dropping&amp;nbsp;this variable&amp;nbsp;from the data step technically changed the order of all other columns.&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;FONT face="Courier New" size="3"&gt;Any help is much appreciated! &lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face="Courier New" size="3"&gt;Thanks.&lt;/FONT&gt;&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;I think you need to provide a few more details. Nothing that you show would change the order of the data set. So are you complaining that the account_new variable ends up on the "right" of the variables in a table view?&lt;/P&gt;
&lt;P&gt;If that is the case then modify your code to:&lt;/P&gt;
&lt;PRE&gt;data NEW;
   length Account_new 8;
   SET HAVE;
   Account_New=input(Account, 10.);
   Bor_New=input(Bor, 3.);
   drop Account Bor;
   rename Account_New=Account Bor_New=Bor;
run;
&lt;/PRE&gt;
&lt;P&gt;I generally don't worry about the order of variables inside a data set as computer programs don't care and any report that I write I can specify what goes where.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;BTW I would say you are not doing yourself any help by making an account number actually numeric. Are you going to do arithmetic with it? You never know when someone is going to create "account" numbers with leading zeroes, which numeric values will not honor, or start adding characters to them.&lt;/P&gt;</description>
      <pubDate>Mon, 16 Sep 2019 15:07:47 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Enterprise-Guide/Reorder-the-variables-after-data-type-conversion/m-p/589077#M34670</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2019-09-16T15:07:47Z</dc:date>
    </item>
    <item>
      <title>Re: Reorder the variables after data type conversion</title>
      <link>https://communities.sas.com/t5/SAS-Enterprise-Guide/Reorder-the-variables-after-data-type-conversion/m-p/589081#M34671</link>
      <description>Hi draycut, thanks for your response. It actually works to make Account_New the first variable. How about the Bor columns? That colum was originally in the 30th column.</description>
      <pubDate>Mon, 16 Sep 2019 15:09:49 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Enterprise-Guide/Reorder-the-variables-after-data-type-conversion/m-p/589081#M34671</guid>
      <dc:creator>MelissaN</dc:creator>
      <dc:date>2019-09-16T15:09:49Z</dc:date>
    </item>
    <item>
      <title>Re: Reorder the variables after data type conversion</title>
      <link>https://communities.sas.com/t5/SAS-Enterprise-Guide/Reorder-the-variables-after-data-type-conversion/m-p/589084#M34672</link>
      <description>Hi ballardw,&lt;BR /&gt;Thank you for your response.&lt;BR /&gt;So usually when we add new variables, they will be added to the right (like you mentioned) of the data set. I want them to be in the original order so I can union with another data set.&lt;BR /&gt;I'm not sure if SAS can perfrom the union if the fields of 2 data set are not matching up.&lt;BR /&gt;I may consider to convert the account number to charater in the other data set.&lt;BR /&gt;Thanks.&lt;BR /&gt;</description>
      <pubDate>Mon, 16 Sep 2019 15:15:37 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Enterprise-Guide/Reorder-the-variables-after-data-type-conversion/m-p/589084#M34672</guid>
      <dc:creator>MelissaN</dc:creator>
      <dc:date>2019-09-16T15:15:37Z</dc:date>
    </item>
    <item>
      <title>Re: Reorder the variables after data type conversion</title>
      <link>https://communities.sas.com/t5/SAS-Enterprise-Guide/Reorder-the-variables-after-data-type-conversion/m-p/589085#M34673</link>
      <description>&lt;P&gt;First of all, the order of variables rarely matters in SAS. If this is a one time thing, you can do the following:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;1) Print the variables from the original data set in the log ordered by their initial order.&lt;/P&gt;
&lt;P&gt;2) Copy/Paste there into a Format Statement before the Set Statement.&lt;/P&gt;
&lt;P&gt;3) Change the two variable names to your liking.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;See this small example&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql noprint;
	select name into :vars separated by ' '
	from dictionary.columns
	where libname='SASHELP' and memname='CLASS'
	order by varnum;
quit;

%put &amp;amp;vars.;

data want;
	format Name Sex Age NewHeight Weight; /* Copied from log and changed height to NewHeight */
	set sashelp.class;
	newHeight=put(Height, $8. -l);
	drop height;
	rename newHeight=Height;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Mon, 16 Sep 2019 15:24:34 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Enterprise-Guide/Reorder-the-variables-after-data-type-conversion/m-p/589085#M34673</guid>
      <dc:creator>PeterClemmensen</dc:creator>
      <dc:date>2019-09-16T15:24:34Z</dc:date>
    </item>
    <item>
      <title>Re: Reorder the variables after data type conversion</title>
      <link>https://communities.sas.com/t5/SAS-Enterprise-Guide/Reorder-the-variables-after-data-type-conversion/m-p/589086#M34674</link>
      <description>&lt;P&gt;SAS knows the NAMES of the variables.&amp;nbsp; So if you use normal SAS code to "union" datasets&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
  set one two;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;then the position of the variables in the table is not important.&lt;/P&gt;
&lt;P&gt;In PROC SQL you can use the CORRESPONDING (abbreviation CORR) keyword to make sure that it match variables by name.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql ;
create table want as 
select * from one
union corr
select * from two
;
quit;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;But I definitely prefer that dataset have the variables defined in a rational order.&amp;nbsp; There is nothing worse then trying to browse a dataset to get a sense of what it looks like and have the whole screen filled with some blank $200 character field that someone put at the beginning of the data vector.&lt;/P&gt;
&lt;P&gt;You can query the metadata of the dataset to get the list of variable names in order and use that to force the new dataset to create the variables in the same order.&amp;nbsp; You could use PROC SQL and a select statement. Or the FORMAT statement (without any actual formats) in a data step.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc contents data=have out=contents noprint; run;
proc sql noprint;
 select name into :varlist separated by ' ' 
 from contents 
 order by varnum
 ;
quit;

data want ;
  format &amp;amp;varlist;
  set have(rename=(acct=X_acct BOR=X_bor));
  acct = put(x_acct,z12.);
  bor = input(x_bor,comma32.);
  drop x_: ;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 16 Sep 2019 15:26:55 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Enterprise-Guide/Reorder-the-variables-after-data-type-conversion/m-p/589086#M34674</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2019-09-16T15:26:55Z</dc:date>
    </item>
    <item>
      <title>Re: Reorder the variables after data type conversion</title>
      <link>https://communities.sas.com/t5/SAS-Enterprise-Guide/Reorder-the-variables-after-data-type-conversion/m-p/589090#M34675</link>
      <description>Thank you Tom. I will try this out.</description>
      <pubDate>Mon, 16 Sep 2019 15:31:35 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Enterprise-Guide/Reorder-the-variables-after-data-type-conversion/m-p/589090#M34675</guid>
      <dc:creator>MelissaN</dc:creator>
      <dc:date>2019-09-16T15:31:35Z</dc:date>
    </item>
    <item>
      <title>Re: Reorder the variables after data type conversion</title>
      <link>https://communities.sas.com/t5/SAS-Enterprise-Guide/Reorder-the-variables-after-data-type-conversion/m-p/589159#M34676</link>
      <description>&lt;P&gt;Here's a trick you can use.&amp;nbsp; It relies on you knowing that ACCOUNT is currently the first variable.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
   if 5=4 then do;
      set have (keep=account);
      length Account_new 8;
      set have (keep=account--bor);
      length Bor_new 8;
   end;&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;   set have;
   Account_New=input(Account, 10.);
   Bor_New=input(Bor, 3.);
   drop Account Bor;
   rename Account_New=Account Bor_New=Bor;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;The top block of statements gets all the variables defined in the proper order.&lt;/P&gt;</description>
      <pubDate>Mon, 16 Sep 2019 18:52:28 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Enterprise-Guide/Reorder-the-variables-after-data-type-conversion/m-p/589159#M34676</guid>
      <dc:creator>Astounding</dc:creator>
      <dc:date>2019-09-16T18:52:28Z</dc:date>
    </item>
    <item>
      <title>Re: Reorder the variables after data type conversion</title>
      <link>https://communities.sas.com/t5/SAS-Enterprise-Guide/Reorder-the-variables-after-data-type-conversion/m-p/589194#M34677</link>
      <description>&lt;P&gt;Just when you think you've seen them all! I love it!!&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Tom&lt;/P&gt;</description>
      <pubDate>Mon, 16 Sep 2019 21:01:29 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Enterprise-Guide/Reorder-the-variables-after-data-type-conversion/m-p/589194#M34677</guid>
      <dc:creator>TomKari</dc:creator>
      <dc:date>2019-09-16T21:01:29Z</dc:date>
    </item>
  </channel>
</rss>

