<?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 successive columns into numeric in New SAS User</title>
    <link>https://communities.sas.com/t5/New-SAS-User/Convert-successive-columns-into-numeric/m-p/865051#M38231</link>
    <description>&lt;P&gt;You can use a variable list to find all of the character variables between those two variables easy enough.&amp;nbsp; But to convert then you need to also know how many there are and what their names are.&amp;nbsp; One easy way to find out is to use PROC TRANSPOSE.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc transpose data=have(obs=0) out=names;
  var s09912_1-character-s16015_1 ;
run;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Once you have the names it is not hard to generate code to convert the values to numeric.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;filename code temp;
data _null_;
  set names ;
  file code ;
  length nliteral $60;
  nliteral=nliteral(_name_);
  put '_' _n_ '=input(' nliteral ',32.);'
    / 'drop ' nliteral ';'
    / 'rename _' _n_ '=' nliteral ';' 
  ;
run;

data want;
  set have;
  %include code / source2 ;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;But I think the real solution is to define the variables properly to begin with.&amp;nbsp; How did you create the dataset that has the wrong variable type for so many variables?&amp;nbsp; Did you perhaps use PROC IMPORT to make guesses about how to read a text file?&amp;nbsp; If so then just write your own data step to read the text file instead and you will have complete control over how the variables are created.&lt;/P&gt;</description>
    <pubDate>Sat, 18 Mar 2023 13:40:30 GMT</pubDate>
    <dc:creator>Tom</dc:creator>
    <dc:date>2023-03-18T13:40:30Z</dc:date>
    <item>
      <title>Convert successive columns into numeric</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Convert-successive-columns-into-numeric/m-p/865037#M38227</link>
      <description>&lt;P&gt;Hi everone,&lt;/P&gt;&lt;P&gt;Please let me know to convert multiple and successive columns (with type of char and num) into numeric? The variables name are 's09912_1--s16015_1' which including 1900 columns.&lt;/P&gt;&lt;P&gt;I tried ARRAY statement, but it can't be use with both character and numeric type.&lt;/P&gt;&lt;P&gt;And INPUT statement, can only convert one columns a time, and selecting character columns is too time-consuming.&amp;nbsp;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Simple code better. Thanks in advanced!&lt;/P&gt;</description>
      <pubDate>Sat, 18 Mar 2023 08:47:31 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Convert-successive-columns-into-numeric/m-p/865037#M38227</guid>
      <dc:creator>Ann297</dc:creator>
      <dc:date>2023-03-18T08:47:31Z</dc:date>
    </item>
    <item>
      <title>Re: Convert successive columns into numeric</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Convert-successive-columns-into-numeric/m-p/865038#M38228</link>
      <description>&lt;P&gt;That many variables of the wrong type means how ever you read the data was likely sub-optimal.&lt;/P&gt;
&lt;P&gt;It is very likely that reading the data correctly in the first place would be preferred.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Please describe how you created the data set with the wrong types.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;You also cannot 'convert' an existing variable, one the type is set it stays. There are ways that involve renaming the the variables and creating a new variable of the correct type with the old name. However that typically doesn't keep column order if that is important.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Note that for arrays it is very easy&amp;nbsp; to get all the character variables:&lt;/P&gt;
&lt;PRE&gt;array x (*) _character_;&lt;/PRE&gt;
&lt;P&gt;Your problem is actually how to get a matching number of new numeric variables.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Also, what do these character values that should be numeric actually look like? Best would be to copy the first 5 rows of data from the source file and paste them into a text box opened on the forum with the &amp;lt;/&amp;gt; icon that appears above the message window.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;IF your file stared as a spreadsheet you may be much better off saving the file to a CSV format and read that file as spreadsheets are full of potential landmines when it comes to data consistency.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sat, 18 Mar 2023 09:08:20 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Convert-successive-columns-into-numeric/m-p/865038#M38228</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2023-03-18T09:08:20Z</dc:date>
    </item>
    <item>
      <title>Re: Convert successive columns into numeric</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Convert-successive-columns-into-numeric/m-p/865045#M38229</link>
      <description>&lt;P&gt;Go back to the process that brought the data into SAS, and fix it there.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Which information do the numbers in the variable names represent? Is this some kind of date-related stuff?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sat, 18 Mar 2023 11:01:06 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Convert-successive-columns-into-numeric/m-p/865045#M38229</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2023-03-18T11:01:06Z</dc:date>
    </item>
    <item>
      <title>Re: Convert successive columns into numeric</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Convert-successive-columns-into-numeric/m-p/865046#M38230</link>
      <description>&lt;P&gt;You can create an array holding only the variables that need to be converted.&amp;nbsp; This suggestion comes close but may include extra variables:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;array charvals {*} _character_;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;That array might also include character variables from other parts of your data set, character variables that you may not want to convert.&amp;nbsp; A more accurate array statement would be:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;array chars {*} s09912_1-character-s16015_1;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Note that you still need new variable names to hold the numeric result of your conversion.&amp;nbsp; An existing variable cannot be changed from character to numeric.&amp;nbsp; So there is more work to be done to accurately define those new names.&amp;nbsp; It would help if you show us how you plan to convert a single variable from character to numeric.&lt;/P&gt;</description>
      <pubDate>Sat, 18 Mar 2023 11:26:59 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Convert-successive-columns-into-numeric/m-p/865046#M38230</guid>
      <dc:creator>Astounding</dc:creator>
      <dc:date>2023-03-18T11:26:59Z</dc:date>
    </item>
    <item>
      <title>Re: Convert successive columns into numeric</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Convert-successive-columns-into-numeric/m-p/865051#M38231</link>
      <description>&lt;P&gt;You can use a variable list to find all of the character variables between those two variables easy enough.&amp;nbsp; But to convert then you need to also know how many there are and what their names are.&amp;nbsp; One easy way to find out is to use PROC TRANSPOSE.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc transpose data=have(obs=0) out=names;
  var s09912_1-character-s16015_1 ;
run;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Once you have the names it is not hard to generate code to convert the values to numeric.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;filename code temp;
data _null_;
  set names ;
  file code ;
  length nliteral $60;
  nliteral=nliteral(_name_);
  put '_' _n_ '=input(' nliteral ',32.);'
    / 'drop ' nliteral ';'
    / 'rename _' _n_ '=' nliteral ';' 
  ;
run;

data want;
  set have;
  %include code / source2 ;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;But I think the real solution is to define the variables properly to begin with.&amp;nbsp; How did you create the dataset that has the wrong variable type for so many variables?&amp;nbsp; Did you perhaps use PROC IMPORT to make guesses about how to read a text file?&amp;nbsp; If so then just write your own data step to read the text file instead and you will have complete control over how the variables are created.&lt;/P&gt;</description>
      <pubDate>Sat, 18 Mar 2023 13:40:30 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Convert-successive-columns-into-numeric/m-p/865051#M38231</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2023-03-18T13:40:30Z</dc:date>
    </item>
    <item>
      <title>Re: Convert successive columns into numeric</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Convert-successive-columns-into-numeric/m-p/865122#M38232</link>
      <description>&lt;P&gt;Thank you for you patience! The type of the file i got is 'sas7dbat'. This dataset was convert from character to numeric, so maybe that's reason. And i don't know how they converted this dataset. Anyway, i 'll try to ask for the original dataset first.&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sun, 19 Mar 2023 22:54:46 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Convert-successive-columns-into-numeric/m-p/865122#M38232</guid>
      <dc:creator>Ann297</dc:creator>
      <dc:date>2023-03-19T22:54:46Z</dc:date>
    </item>
    <item>
      <title>Re: Convert successive columns into numeric</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Convert-successive-columns-into-numeric/m-p/865124#M38233</link>
      <description>Thank you! From original is a way, but this code worked ! And totally like magic!</description>
      <pubDate>Sun, 19 Mar 2023 23:50:48 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Convert-successive-columns-into-numeric/m-p/865124#M38233</guid>
      <dc:creator>Ann297</dc:creator>
      <dc:date>2023-03-19T23:50:48Z</dc:date>
    </item>
  </channel>
</rss>

