<?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 all character variables to numeric in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Convert-all-character-variables-to-numeric/m-p/740481#M231320</link>
    <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/381519"&gt;@Emma2021&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;How can I change all character variables into numeric without changing variables names? Thank you&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;The real question is why are variables that should be numeric character in the first place. Most of the time when we see this question it relates to not taking control of your data early enough, as in when it is read into SAS and typically involves one or more of 1) spreadsheet data, 2) poorly laid out - multiple header rows for example or values like NA or NULL in 'numeric' columns, and 3) relying on Proc Import or a wizard.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Usually this means that the data should be read with a data step where you control things.&lt;/P&gt;</description>
    <pubDate>Tue, 11 May 2021 14:39:03 GMT</pubDate>
    <dc:creator>ballardw</dc:creator>
    <dc:date>2021-05-11T14:39:03Z</dc:date>
    <item>
      <title>Convert all character variables to numeric</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Convert-all-character-variables-to-numeric/m-p/740462#M231305</link>
      <description>How can I change all character variables into numeric without changing variables names? Thank you</description>
      <pubDate>Tue, 11 May 2021 13:35:58 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Convert-all-character-variables-to-numeric/m-p/740462#M231305</guid>
      <dc:creator>Emma2021</dc:creator>
      <dc:date>2021-05-11T13:35:58Z</dc:date>
    </item>
    <item>
      <title>Re: Convert all character variables to numeric</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Convert-all-character-variables-to-numeric/m-p/740466#M231308</link>
      <description>&lt;P&gt;You can't.&amp;nbsp; But you can change the name back.&lt;/P&gt;
&lt;P&gt;Here is how you could change one.&amp;nbsp;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
  set have;
  num = input(char,32.);
  drop char ;
  rename num=char;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;For multiple you might adapt this to this pattern.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
  set have;
  num1 = input(char1,32.);
  num2 = input(char2,32.);
  drop char1 char2 ;
  rename num1=char1 num2=char2;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;If you have the list of the names of the character variable then it should be simple enough to generate the code from the names.&lt;/P&gt;</description>
      <pubDate>Tue, 11 May 2021 14:01:41 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Convert-all-character-variables-to-numeric/m-p/740466#M231308</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2021-05-11T14:01:41Z</dc:date>
    </item>
    <item>
      <title>Re: Convert all character variables to numeric</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Convert-all-character-variables-to-numeric/m-p/740467#M231309</link>
      <description>&lt;P&gt;Hi,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;try this:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%MACRO convertChar2Num(varlist=);
   %local varlist i currItem;
   %LET i=1;
   %LET currItem=%SCAN(&amp;amp;varlist.,&amp;amp;i.,%STR( ));
   %DO %WHILE(%LENGTH(&amp;amp;currItem.)&amp;gt;0);

      _hlp&amp;amp;currItem.=input(&amp;amp;currItem.,best32.);
      if strip(&amp;amp;currItem.) ne strip(put(_hlp&amp;amp;currItem.,best32.)) then put 'E' 'RROR: unexpected different values encountered, please check!' _N_= &amp;amp;currItem.= @100 _hlp&amp;amp;currItem.=;
      drop &amp;amp;currItem.;
      rename _hlp&amp;amp;currItem.=&amp;amp;currItem.;
      %put &amp;amp;=varlist &amp;amp;=i &amp;amp;=currItem;
      %LET i=%EVAL(&amp;amp;i.+1);
      %LET currItem=%SCAN(&amp;amp;varlist.,&amp;amp;i.,%STR( ));
   %END;
%MEND convertChar2Num;

data class;
   set sashelp.class;
   x=put(age,best.);
   y=put(weight,best.);
   z=put(height,best.);
   drop age weight height;
   %convertChar2Num(varlist=x y z);
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Tue, 11 May 2021 14:03:28 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Convert-all-character-variables-to-numeric/m-p/740467#M231309</guid>
      <dc:creator>Oligolas</dc:creator>
      <dc:date>2021-05-11T14:03:28Z</dc:date>
    </item>
    <item>
      <title>Re: Convert all character variables to numeric</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Convert-all-character-variables-to-numeric/m-p/740469#M231311</link>
      <description>&lt;P&gt;If you really wanted a simple way to convert all character variables to numeric then here is a simply way.&lt;/P&gt;
&lt;P&gt;Get a list of the names of the variables.&amp;nbsp; If the list is short enough you could just put it into a macro variable.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql noprint;
select nliteral(name) into :varlist separated by ' ' 
  from dictionary.columns
  where libname='WORK'
    and memname='HAVE'
    and type='char'
;
quit;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Now use that list of names to write the values to a file. Then read the values back as numbers.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;filename text temp;
data _null_;
  set have;
  file text dsd ;
  put &amp;amp;varlist;
run;
data want;
  set have(drop=&amp;amp;varlist);
  infile text dsd truncover ;
  input &amp;amp;varlist;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 11 May 2021 14:08:40 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Convert-all-character-variables-to-numeric/m-p/740469#M231311</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2021-05-11T14:08:40Z</dc:date>
    </item>
    <item>
      <title>Re: Convert all character variables to numeric</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Convert-all-character-variables-to-numeric/m-p/740481#M231320</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/381519"&gt;@Emma2021&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;How can I change all character variables into numeric without changing variables names? Thank you&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;The real question is why are variables that should be numeric character in the first place. Most of the time when we see this question it relates to not taking control of your data early enough, as in when it is read into SAS and typically involves one or more of 1) spreadsheet data, 2) poorly laid out - multiple header rows for example or values like NA or NULL in 'numeric' columns, and 3) relying on Proc Import or a wizard.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Usually this means that the data should be read with a data step where you control things.&lt;/P&gt;</description>
      <pubDate>Tue, 11 May 2021 14:39:03 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Convert-all-character-variables-to-numeric/m-p/740481#M231320</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2021-05-11T14:39:03Z</dc:date>
    </item>
  </channel>
</rss>

