<?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: Converting character data to numeric in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Converting-character-data-to-numeric/m-p/433174#M281835</link>
    <description>&lt;P&gt;You can't change a variable type on the fly so you need to create a new variable. Otherwise your code and approach is correct.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;Age_Num = input(age, 2.);&lt;/CODE&gt;&lt;/PRE&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/190577"&gt;@newbie_grad&lt;/a&gt; wrote:&lt;BR /&gt;
&lt;P&gt;Hello everyone!&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I am having trouble converting a character column to numeric. So the Smoker and Age columns were created from the MOTHER column, and now I want the age column to be numeric. I tried to do the Age = Input(Age,2.); but the result was the same.&lt;/P&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="age.jpg" style="width: 360px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/18255i2142E4960F64DCAB/image-size/large?v=v2&amp;amp;px=999" role="button" title="age.jpg" alt="age.jpg" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data Minute1;
title 'Corrected minute1 dataset';
set Homework.minute1;
if Appearance in ('0','1','2') and Pulse in ('0','1','2') and Grimace in ('0','1','2') and Activity in ('0','1','2') and Respiration in ('0','1','2');
Score=sum(of Appearance Pulse Grimace Activity Respiration);
length Smoker $ 1;
n_char = length(MOTHER);
Smoker = substr(MOTHER,n_char,1);
Age = substr(MOTHER,1,n_char-1);
keep Baby_ID MOTHER Appearance Pulse Grimace Activity Respiration Smoker Age;
run;
proc print data=Minute1;
run;&lt;BR /&gt;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Thanks!&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;&lt;BR /&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Thu, 01 Feb 2018 16:52:33 GMT</pubDate>
    <dc:creator>Reeza</dc:creator>
    <dc:date>2018-02-01T16:52:33Z</dc:date>
    <item>
      <title>Converting character data to numeric</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Converting-character-data-to-numeric/m-p/433173#M281834</link>
      <description>&lt;P&gt;Hello everyone!&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I am having trouble converting a character column to numeric. So the Smoker and Age columns were created from the MOTHER column, and now I want the age column to be numeric. I tried to do the Age = Input(Age,2.); but the result was the same.&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="age.jpg" style="width: 360px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/18255i2142E4960F64DCAB/image-size/large?v=v2&amp;amp;px=999" role="button" title="age.jpg" alt="age.jpg" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data Minute1;
title 'Corrected minute1 dataset';
set Homework.minute1;
if Appearance in ('0','1','2') and Pulse in ('0','1','2') and Grimace in ('0','1','2') and Activity in ('0','1','2') and Respiration in ('0','1','2');
Score=sum(of Appearance Pulse Grimace Activity Respiration);
length Smoker $ 1;
n_char = length(MOTHER);
Smoker = substr(MOTHER,n_char,1);
Age = substr(MOTHER,1,n_char-1);
keep Baby_ID MOTHER Appearance Pulse Grimace Activity Respiration Smoker Age;
run;
proc print data=Minute1;
run;&lt;BR /&gt;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;Thanks!&lt;/P&gt;</description>
      <pubDate>Thu, 01 Feb 2018 16:49:17 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Converting-character-data-to-numeric/m-p/433173#M281834</guid>
      <dc:creator>newbie_grad</dc:creator>
      <dc:date>2018-02-01T16:49:17Z</dc:date>
    </item>
    <item>
      <title>Re: Converting character data to numeric</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Converting-character-data-to-numeric/m-p/433174#M281835</link>
      <description>&lt;P&gt;You can't change a variable type on the fly so you need to create a new variable. Otherwise your code and approach is correct.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;Age_Num = input(age, 2.);&lt;/CODE&gt;&lt;/PRE&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/190577"&gt;@newbie_grad&lt;/a&gt; wrote:&lt;BR /&gt;
&lt;P&gt;Hello everyone!&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I am having trouble converting a character column to numeric. So the Smoker and Age columns were created from the MOTHER column, and now I want the age column to be numeric. I tried to do the Age = Input(Age,2.); but the result was the same.&lt;/P&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="age.jpg" style="width: 360px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/18255i2142E4960F64DCAB/image-size/large?v=v2&amp;amp;px=999" role="button" title="age.jpg" alt="age.jpg" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data Minute1;
title 'Corrected minute1 dataset';
set Homework.minute1;
if Appearance in ('0','1','2') and Pulse in ('0','1','2') and Grimace in ('0','1','2') and Activity in ('0','1','2') and Respiration in ('0','1','2');
Score=sum(of Appearance Pulse Grimace Activity Respiration);
length Smoker $ 1;
n_char = length(MOTHER);
Smoker = substr(MOTHER,n_char,1);
Age = substr(MOTHER,1,n_char-1);
keep Baby_ID MOTHER Appearance Pulse Grimace Activity Respiration Smoker Age;
run;
proc print data=Minute1;
run;&lt;BR /&gt;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Thanks!&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;&lt;BR /&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 01 Feb 2018 16:52:33 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Converting-character-data-to-numeric/m-p/433174#M281835</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2018-02-01T16:52:33Z</dc:date>
    </item>
    <item>
      <title>Re: Converting character data to numeric</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Converting-character-data-to-numeric/m-p/433175#M281836</link>
      <description>&lt;P&gt;Age &lt;SPAN class="token operator"&gt;=&lt;/SPAN&gt; input ( &lt;SPAN class="token function"&gt;substr&lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;(&lt;/SPAN&gt;MOTHER&lt;SPAN class="token punctuation"&gt;,&lt;/SPAN&gt;&lt;SPAN class="token number"&gt;1&lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;,&lt;/SPAN&gt;n_char&lt;SPAN class="token operator"&gt;-&lt;/SPAN&gt;&lt;SPAN class="token number"&gt;1&lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;) , best.)&lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;SPAN class="token punctuation"&gt;Your approach did not work because once a variable is created as character (use of substr in this case) you cannot change the variable type. So do it in one string. &lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&lt;SPAN class="token punctuation"&gt;OR create a temporary variable and then input that though that approach leads to many temporary variables that you may want to drop.&lt;/SPAN&gt;&lt;/P&gt;</description>
      <pubDate>Thu, 01 Feb 2018 16:53:56 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Converting-character-data-to-numeric/m-p/433175#M281836</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2018-02-01T16:53:56Z</dc:date>
    </item>
    <item>
      <title>Re: Converting character data to numeric</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Converting-character-data-to-numeric/m-p/433177#M281837</link>
      <description>&lt;P&gt;Thank you! That worked out great!&lt;/P&gt;</description>
      <pubDate>Thu, 01 Feb 2018 16:57:51 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Converting-character-data-to-numeric/m-p/433177#M281837</guid>
      <dc:creator>newbie_grad</dc:creator>
      <dc:date>2018-02-01T16:57:51Z</dc:date>
    </item>
    <item>
      <title>Re: Converting character data to numeric</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Converting-character-data-to-numeric/m-p/433182#M281838</link>
      <description>&lt;P&gt;Perhaps a little simpler:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;age = inputn(mother, n_char - 1);&lt;/P&gt;</description>
      <pubDate>Thu, 01 Feb 2018 17:03:52 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Converting-character-data-to-numeric/m-p/433182#M281838</guid>
      <dc:creator>Astounding</dc:creator>
      <dc:date>2018-02-01T17:03:52Z</dc:date>
    </item>
  </channel>
</rss>

