<?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: Do Loop to change variables from character to numeric in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Do-Loop-to-change-variables-from-character-to-numeric/m-p/503474#M134538</link>
    <description>&lt;P&gt;Thank you! How would I ensure that my data is read in correctly, if SAS imports it as a character variable?&lt;/P&gt;</description>
    <pubDate>Thu, 11 Oct 2018 17:02:40 GMT</pubDate>
    <dc:creator>kmardinian</dc:creator>
    <dc:date>2018-10-11T17:02:40Z</dc:date>
    <item>
      <title>Do Loop to change variables from character to numeric</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Do-Loop-to-change-variables-from-character-to-numeric/m-p/503458#M134526</link>
      <description>&lt;P&gt;Hi I would like to convert 32 different variables q1-q32 from character to numeric using a do-loop but I cannot seem to find the right code to do that. Any help is much appreciated. Below is the code I have so far, but is not working correctly.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;data t1_2;&lt;BR /&gt;set t1;&lt;BR /&gt;array i {32} q1-q32;&lt;BR /&gt;do i=1 to 32;&lt;BR /&gt;i_num=input(i,best5.);&lt;BR /&gt;drop i;&lt;BR /&gt;rename i_num=i;&lt;BR /&gt;end;&lt;BR /&gt;run;&lt;/P&gt;</description>
      <pubDate>Thu, 11 Oct 2018 16:36:25 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Do-Loop-to-change-variables-from-character-to-numeric/m-p/503458#M134526</guid>
      <dc:creator>kmardinian</dc:creator>
      <dc:date>2018-10-11T16:36:25Z</dc:date>
    </item>
    <item>
      <title>Re: Do Loop to change variables from character to numeric</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Do-Loop-to-change-variables-from-character-to-numeric/m-p/503472#M134536</link>
      <description>&lt;P&gt;Once a variable is created you cannot change its type from character to numeric or the other way. You need to create new variables.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Best practice is to insure that data is read from external sources as needed.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;This adds numeric variables to your data set named NUMQ1 to NUMQ32.&lt;/P&gt;
&lt;PRE&gt;data t1_2;
   set t1;
   array q{32} q1-q32;
   array numQ{32};
   do i = 1 to dim(q);
      numq[i] = input(q[i],best5.);
   end;
run;&lt;/PRE&gt;
&lt;P&gt;You could drop the Q variables if desired. Or go through some renaming if you really want the variables to be named Q1 to Q32, though I would have ensured my data was read correctly.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 11 Oct 2018 16:56:44 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Do-Loop-to-change-variables-from-character-to-numeric/m-p/503472#M134536</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2018-10-11T16:56:44Z</dc:date>
    </item>
    <item>
      <title>Re: Do Loop to change variables from character to numeric</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Do-Loop-to-change-variables-from-character-to-numeric/m-p/503474#M134538</link>
      <description>&lt;P&gt;Thank you! How would I ensure that my data is read in correctly, if SAS imports it as a character variable?&lt;/P&gt;</description>
      <pubDate>Thu, 11 Oct 2018 17:02:40 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Do-Loop-to-change-variables-from-character-to-numeric/m-p/503474#M134538</guid>
      <dc:creator>kmardinian</dc:creator>
      <dc:date>2018-10-11T17:02:40Z</dc:date>
    </item>
    <item>
      <title>Re: Do Loop to change variables from character to numeric</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Do-Loop-to-change-variables-from-character-to-numeric/m-p/503554#M134580</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/139341"&gt;@kmardinian&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;Thank you! How would I ensure that my data is read in correctly, if SAS imports it as a character variable?&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;How did you read the data to begin with?&lt;/P&gt;
&lt;P&gt;We might need to see some example from the source file.&lt;/P&gt;
&lt;P&gt;Hint: if the data originated as a spreadsheet (Excel) then you may be better of using Excel to save the file as CSV. You can then write a data step to read things as needed. Proc Import on a CSV file will generate data step code to read the file. Add the GUESSINGROWS option to be large (close to the number of rows)&amp;nbsp;to ensure that most of the data is examined before assigning types.&lt;/P&gt;
&lt;P&gt;You could then copy the code from the log and paste into the editor and modify the code as needed.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Depending on the data source and how read things that can affect importing data are 2 or more rows of text headers (with a CSV or other delimited file,&amp;nbsp;you can tell Proc Import to start on the 2nd , 3rd or what ever row to examine data with the datarow= statement) , or many missing values for a variable in the first few records. Also the lengths of your character variables may be "short" if the first few records do not happen to hold the longest values.&lt;/P&gt;</description>
      <pubDate>Thu, 11 Oct 2018 20:31:05 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Do-Loop-to-change-variables-from-character-to-numeric/m-p/503554#M134580</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2018-10-11T20:31:05Z</dc:date>
    </item>
  </channel>
</rss>

