<?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 Creating multiple numeric columns from multiple character columns in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Creating-multiple-numeric-columns-from-multiple-character/m-p/889733#M351553</link>
    <description>&lt;P&gt;I have this below dataset&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;data have;&lt;BR /&gt;input col1 $1-8 col2 $9-16 col3 $17-25 col4 $26-34 col5 $35-41;&lt;BR /&gt;datalines;&lt;BR /&gt;5(25.8) 2(6.25) 8(25.0) 10(14.4) 9(25.8)&lt;BR /&gt;56(12.3) 1(3.13) 19(14.4) 56(12.3) 0&lt;BR /&gt;87(6.0) 1(3.13) 11(34.4) 1(3.13) 0&lt;BR /&gt;90(87.0) 2(6.25) 1(3.13) 1(3.13) 0&lt;BR /&gt;;&lt;BR /&gt;run;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I would like to take the nuemric part from each of these columns exist, and create nuemric columns for each that contains only the value before bracket starts "(".&amp;nbsp;&lt;/P&gt;
&lt;P&gt;eg: for the 1st row there will be 5 numeric columns with value in first column will be 5 and in the last column the value will be 9.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I have written a code to solve this,&amp;nbsp;&lt;/P&gt;
&lt;P&gt;data want;&lt;/P&gt;
&lt;P&gt;set have;&lt;/P&gt;
&lt;P&gt;array char(*) COL1 COL2 COL3 COL4 COL5; /*columns I have */&lt;BR /&gt;array new(*) COL1N COL2N COL3N COL4N COL5N;/*numeric columns to be produced*/&lt;/P&gt;
&lt;P&gt;do i=1 to dim(char);&lt;BR /&gt;if char[i] ~="" then new[i]=scan(char[i],1,'(');&lt;BR /&gt;end;&lt;/P&gt;
&lt;P&gt;run;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;but I'm getting&amp;nbsp; ERROR: Array subscript out of range at line 35 column 22.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;and for the last column since I have '0', need to add a consition to handle the same.&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Thu, 17 Aug 2023 19:43:01 GMT</pubDate>
    <dc:creator>sahoositaram555</dc:creator>
    <dc:date>2023-08-17T19:43:01Z</dc:date>
    <item>
      <title>Creating multiple numeric columns from multiple character columns</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Creating-multiple-numeric-columns-from-multiple-character/m-p/889733#M351553</link>
      <description>&lt;P&gt;I have this below dataset&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;data have;&lt;BR /&gt;input col1 $1-8 col2 $9-16 col3 $17-25 col4 $26-34 col5 $35-41;&lt;BR /&gt;datalines;&lt;BR /&gt;5(25.8) 2(6.25) 8(25.0) 10(14.4) 9(25.8)&lt;BR /&gt;56(12.3) 1(3.13) 19(14.4) 56(12.3) 0&lt;BR /&gt;87(6.0) 1(3.13) 11(34.4) 1(3.13) 0&lt;BR /&gt;90(87.0) 2(6.25) 1(3.13) 1(3.13) 0&lt;BR /&gt;;&lt;BR /&gt;run;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I would like to take the nuemric part from each of these columns exist, and create nuemric columns for each that contains only the value before bracket starts "(".&amp;nbsp;&lt;/P&gt;
&lt;P&gt;eg: for the 1st row there will be 5 numeric columns with value in first column will be 5 and in the last column the value will be 9.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I have written a code to solve this,&amp;nbsp;&lt;/P&gt;
&lt;P&gt;data want;&lt;/P&gt;
&lt;P&gt;set have;&lt;/P&gt;
&lt;P&gt;array char(*) COL1 COL2 COL3 COL4 COL5; /*columns I have */&lt;BR /&gt;array new(*) COL1N COL2N COL3N COL4N COL5N;/*numeric columns to be produced*/&lt;/P&gt;
&lt;P&gt;do i=1 to dim(char);&lt;BR /&gt;if char[i] ~="" then new[i]=scan(char[i],1,'(');&lt;BR /&gt;end;&lt;/P&gt;
&lt;P&gt;run;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;but I'm getting&amp;nbsp; ERROR: Array subscript out of range at line 35 column 22.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;and for the last column since I have '0', need to add a consition to handle the same.&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 17 Aug 2023 19:43:01 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Creating-multiple-numeric-columns-from-multiple-character/m-p/889733#M351553</guid>
      <dc:creator>sahoositaram555</dc:creator>
      <dc:date>2023-08-17T19:43:01Z</dc:date>
    </item>
    <item>
      <title>Re: Creating multiple numeric columns from multiple character columns</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Creating-multiple-numeric-columns-from-multiple-character/m-p/889734#M351554</link>
      <description>&lt;P&gt;When you get an error in the log, SHOW US the log. We need to see the entire log for this data step. Please copy the log as text and paste it into the window that appears when you click on the &amp;lt;/&amp;gt; icon.&lt;/P&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="PaigeMiller_0-1663012019648.png" style="width: 859px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/75161i0E71B1489A6C9839/image-size/large?v=v2&amp;amp;px=999" role="button" title="PaigeMiller_0-1663012019648.png" alt="PaigeMiller_0-1663012019648.png" /&gt;&lt;/span&gt;&lt;/P&gt;</description>
      <pubDate>Thu, 17 Aug 2023 19:47:27 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Creating-multiple-numeric-columns-from-multiple-character/m-p/889734#M351554</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2023-08-17T19:47:27Z</dc:date>
    </item>
    <item>
      <title>Re: Creating multiple numeric columns from multiple character columns</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Creating-multiple-numeric-columns-from-multiple-character/m-p/889758#M351565</link>
      <description>&lt;P&gt;Suggestion: Do not use those Col1N Col2N names. It makes it hard to use several of the tools that SAS provides to treat variables as lists. If you are using N to indicate a numeric variable it may be easier on you in the long run to use NCol1.&lt;/P&gt;
&lt;P&gt;For one thing you can define the list of variable as&lt;/P&gt;
&lt;PRE&gt;Array new(*) Ncol1-Ncol5 ;&lt;/PRE&gt;
&lt;P&gt;You may also want to try copying your pasted code for the first data step back into your SAS session and running it.&lt;/P&gt;
&lt;P&gt;The values I get include spaces in the middle of several variables because the column counts are not correct for the code as shown. For example the 10 that should start variable Col4 on the first row has the 1 in column 25. Which means it is read into Col3 with a space preceding. With the first digit read into the end of the previous variable then some of your values in Col4 (or Col5) in the example have nothing before the (. So you get missing values in the numeric because you converted a zero length string with no characters into a "number".&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;It also quite often happens that you will have issues with the automatic conversion of character to numeric.&lt;/P&gt;
&lt;P&gt;You always want to check the result of any such conversion if you see this in your log:&lt;/P&gt;
&lt;PRE&gt;NOTE: Character values have been converted to numeric
      values at the places given by: (Line):(Colum
&lt;/PRE&gt;
&lt;P&gt;You do not need any "condition" to read the 0 values without the (. Example (note use of INPUT to do an explicit character to numeric which expects at most 2 digits to use for numeric value).&lt;/P&gt;
&lt;PRE&gt;data example;
   x = '0';
   y = input(scan(x,1,'('),2.);
run;&lt;/PRE&gt;
&lt;P&gt;If the end of the string is encountered before any specified delimiter then the whole string is returned as the first SCAN function result.&lt;/P&gt;</description>
      <pubDate>Thu, 17 Aug 2023 22:57:43 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Creating-multiple-numeric-columns-from-multiple-character/m-p/889758#M351565</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2023-08-17T22:57:43Z</dc:date>
    </item>
    <item>
      <title>Re: Creating multiple numeric columns from multiple character columns</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Creating-multiple-numeric-columns-from-multiple-character/m-p/889760#M351566</link>
      <description>&lt;P&gt;Seems trivial to me.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
  length col1-col5 $20;
  input col1-col5;
datalines;
5(25.8) 2(6.25) 8(25.0) 10(14.4) 9(25.8)
56(12.3) 1(3.13) 19(14.4) 56(12.3) 0
87(6.0) 1(3.13) 11(34.4) 1(3.13) 0
90(87.0) 2(6.25) 1(3.13) 1(3.13) 0
;

data want;
  set have;
  array col[5];
  array ncol[5];
  do index=1 to dim(col);
    ncol[index]=input(scan(col[index],1,'('),32.);
  end;
  drop index;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Result&lt;/P&gt;
&lt;PRE&gt;Obs      col1       col2        col3        col4      col5       ncol1    ncol2    ncol3    ncol4    ncol5

 1     5(25.8)     2(6.25)    8(25.0)     10(14.4)    9(25.8)       5       2         8       10       9
 2     56(12.3)    1(3.13)    19(14.4)    56(12.3)    0            56       1        19       56       0
 3     87(6.0)     1(3.13)    11(34.4)    1(3.13)     0            87       1        11        1       0
 4     90(87.0)    2(6.25)    1(3.13)     1(3.13)     0            90       2         1        1       0
&lt;/PRE&gt;</description>
      <pubDate>Thu, 17 Aug 2023 23:07:43 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Creating-multiple-numeric-columns-from-multiple-character/m-p/889760#M351566</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2023-08-17T23:07:43Z</dc:date>
    </item>
  </channel>
</rss>

