<?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: Convert many character to numeric without changing order of variables in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Convert-many-character-to-numeric-without-changing-order-of/m-p/562217#M157480</link>
    <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/275242"&gt;@manonlyn&lt;/a&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;New columns get added at the end of a row.&lt;/P&gt;
&lt;P&gt;Your code doesn't really change an existing column. It creates a new column, drops the old column, renames the new column to the name of the old column ...but it's still a new column.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;What you want to do can be done but I can't think of a simple way to get there. Why is the internal order of the columns important to you? It actually shouldn't be of any relevance.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Looking into your code.&amp;nbsp;&lt;/P&gt;
&lt;PRE class=" language-sas"&gt;&lt;CODE class="  language-sas"&gt;DATE_NUM &lt;SPAN class="token operator"&gt;=&lt;/SPAN&gt; &lt;SPAN class="token keyword"&gt;INPUT&lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;(&lt;/SPAN&gt;&lt;SPAN class="token function"&gt;DATE&lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;,&lt;/SPAN&gt; &lt;SPAN class="token number"&gt;10&lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;.&lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;)&lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;;&lt;/SPAN&gt;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;If the source is a date string then ideally use a Date informat to convert the string to an actual SAS Date value (=count of days since 1/1/1960). A SAS Date value then allows you to use all the nice SAS calendar functions like intnx() and intck().&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Wed, 29 May 2019 10:16:39 GMT</pubDate>
    <dc:creator>Patrick</dc:creator>
    <dc:date>2019-05-29T10:16:39Z</dc:date>
    <item>
      <title>Convert many character to numeric without changing order of variables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Convert-many-character-to-numeric-without-changing-order-of/m-p/562215#M157478</link>
      <description>&lt;P&gt;I have a dataset with 40 different columns that I need changing to numeric is there a way to change these without changing the order of the columns?&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;If I use the below the order of the columns change:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;DATA WANT;
SET HAVE;
DATE_NUM = INPUT(DATE, 10.);
DROP DATE;
RENAME DATE_NUM = DATE;
RUN;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 29 May 2019 09:52:23 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Convert-many-character-to-numeric-without-changing-order-of/m-p/562215#M157478</guid>
      <dc:creator>manonlyn</dc:creator>
      <dc:date>2019-05-29T09:52:23Z</dc:date>
    </item>
    <item>
      <title>Re: Convert many character to numeric without changing order of variables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Convert-many-character-to-numeric-without-changing-order-of/m-p/562217#M157480</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/275242"&gt;@manonlyn&lt;/a&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;New columns get added at the end of a row.&lt;/P&gt;
&lt;P&gt;Your code doesn't really change an existing column. It creates a new column, drops the old column, renames the new column to the name of the old column ...but it's still a new column.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;What you want to do can be done but I can't think of a simple way to get there. Why is the internal order of the columns important to you? It actually shouldn't be of any relevance.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Looking into your code.&amp;nbsp;&lt;/P&gt;
&lt;PRE class=" language-sas"&gt;&lt;CODE class="  language-sas"&gt;DATE_NUM &lt;SPAN class="token operator"&gt;=&lt;/SPAN&gt; &lt;SPAN class="token keyword"&gt;INPUT&lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;(&lt;/SPAN&gt;&lt;SPAN class="token function"&gt;DATE&lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;,&lt;/SPAN&gt; &lt;SPAN class="token number"&gt;10&lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;.&lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;)&lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;;&lt;/SPAN&gt;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;If the source is a date string then ideally use a Date informat to convert the string to an actual SAS Date value (=count of days since 1/1/1960). A SAS Date value then allows you to use all the nice SAS calendar functions like intnx() and intck().&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 29 May 2019 10:16:39 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Convert-many-character-to-numeric-without-changing-order-of/m-p/562217#M157480</guid>
      <dc:creator>Patrick</dc:creator>
      <dc:date>2019-05-29T10:16:39Z</dc:date>
    </item>
    <item>
      <title>Re: Convert many character to numeric without changing order of variables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Convert-many-character-to-numeric-without-changing-order-of/m-p/562218#M157481</link>
      <description>&lt;P&gt;It's important as they will be fed into another system and they need to be in a specific order.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I have looked for ways to change the column from character to numeric without creating a new column and I've not yet been able to find a way to do this. I've always used the method in my question.&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 29 May 2019 10:26:22 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Convert-many-character-to-numeric-without-changing-order-of/m-p/562218#M157481</guid>
      <dc:creator>manonlyn</dc:creator>
      <dc:date>2019-05-29T10:26:22Z</dc:date>
    </item>
    <item>
      <title>Re: Convert many character to numeric without changing order of variables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Convert-many-character-to-numeric-without-changing-order-of/m-p/562223#M157482</link>
      <description>&lt;P&gt;If the order of the columns is important, use SQL:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
input a $ b $ c $;
datalines;
10 20 30
;

proc sql;
create table want as
select
  input(a,10.) as a,
  input(b,10.) as b,
  input(c,10.) as c
from have;
quit;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;You can create a list of variables from dictionary.columns, join that with a list of variables that need to be changed, and order it by the variable number (also contained in dictionary.columns).&lt;/P&gt;
&lt;P&gt;Using that dataset, you can use call execute to create the SQL code dynamically:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
input x $ a $ b $ c $;
datalines;
a 10 20 30
;

data vars_to_change;
input name :$32.;
datalines;
a
b
c
;

proc sql;
create table control as
select
  co.name,
  case
    when ch.name &amp;gt; " "
    then 'Y'
    else 'N'
  end as change
from dictionary.columns co
left join vars_to_change ch
on upcase(co.name) = upcase(ch.name)
where co.libname = 'WORK' and co.memname = 'HAVE'
order by co.varnum
;
quit;

data _null_;
set control end=eof;
if _n_ = 1 then call execute("
  proc sql;
  create table want as select
");
if _n_ &amp;gt; 1 then call execute(',');
if change = 'Y'
then call execute('input(' !! trim(name) !! ',10.) as ' !! trim(name));
else call execute(trim(name));
if eof then call execute(' from have;quit;');
run; &lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 29 May 2019 11:07:11 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Convert-many-character-to-numeric-without-changing-order-of/m-p/562223#M157482</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2019-05-29T11:07:11Z</dc:date>
    </item>
    <item>
      <title>Re: Convert many character to numeric without changing order of variables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Convert-many-character-to-numeric-without-changing-order-of/m-p/562224#M157483</link>
      <description>&lt;P&gt;If you have created a dataset or view with the converted variables (&lt;EM&gt;edit:&lt;/EM&gt; and all the other variables from the initial dataset HAVE), let's call it TMP, you can apply a RETAIN statement to adjust the order of variables.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql noprint;
select name into :varlist separated by ' '
from dictionary.columns
where libname='WORK' &amp;amp; memname='HAVE'
order by varnum;
quit;

data want;
retain &amp;amp;varlist;
set tmp;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 29 May 2019 11:24:40 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Convert-many-character-to-numeric-without-changing-order-of/m-p/562224#M157483</guid>
      <dc:creator>FreelanceReinh</dc:creator>
      <dc:date>2019-05-29T11:24:40Z</dc:date>
    </item>
    <item>
      <title>Re: Convert many character to numeric without changing order of variables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Convert-many-character-to-numeric-without-changing-order-of/m-p/562225#M157484</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/275242"&gt;@manonlyn&lt;/a&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I stay corrected by both&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/11562"&gt;@Kurt_Bremser&lt;/a&gt;&amp;nbsp;and&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/32733"&gt;@FreelanceReinh&lt;/a&gt;&amp;nbsp;. It's not that hard to code for.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I'd say the "best" way is using SQL as this only requires a single pass through the data. With a SQL you have to spell out all the columns. For lazy people like me one way to go if you've got a lot of columns is using the following code:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql feedback inobs=0;
  select *
  from sashelp.class;
  ;
quit;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Then copy from the SAS log the expanded SQL into Notepad ++ and use the plugin "Poor Man's T_SQL Formatter" and you'll end up with code like below which you then copy back into a SAS editor.&lt;/P&gt;
&lt;PRE&gt;SELECT CLASS.NAME
  ,CLASS.Sex
  ,CLASS.Age
  ,CLASS.Height
  ,CLASS.Weight
FROM SASHELP.CLASS;
&lt;/PRE&gt;
&lt;P&gt;Now add the changes to the columns and you're done.&lt;/P&gt;</description>
      <pubDate>Wed, 29 May 2019 11:32:13 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Convert-many-character-to-numeric-without-changing-order-of/m-p/562225#M157484</guid>
      <dc:creator>Patrick</dc:creator>
      <dc:date>2019-05-29T11:32:13Z</dc:date>
    </item>
    <item>
      <title>Re: Convert many character to numeric without changing order of variables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Convert-many-character-to-numeric-without-changing-order-of/m-p/562244#M157488</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
   length x1-x40 $10;
   array x {40} x1-x40;
   do u = 1 to 100;
      do i=1 to 40;
      z=ranuni(0);
      if z&amp;lt;0.995 then x{i} = byte(int(rand("uniform",48,58)));
      else x{i} = byte(int(rand("uniform",65,66)));
      end;
      output;
   end;
   drop u z i;
run;

data _null_;
   set sashelp.vcolumn end=last;
   where libname eq 'WORK' and memname eq 'HAVE';
   if _N_ eq 1 then call execute('DATA want;set have;');
   call execute(cats(name,'_num')||'=input('||strip(name)||',?? best32.); drop '||strip(name)||';');
   if last then call execute('RUN;');
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 29 May 2019 12:45:46 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Convert-many-character-to-numeric-without-changing-order-of/m-p/562244#M157488</guid>
      <dc:creator>Oligolas</dc:creator>
      <dc:date>2019-05-29T12:45:46Z</dc:date>
    </item>
  </channel>
</rss>

