<?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 variables to Numeric in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Converting-Character-variables-to-Numeric/m-p/705930#M216624</link>
    <description>&lt;P&gt;Let's look at some details of your existing data:&lt;/P&gt;
&lt;PRE&gt;zipcode=92365-4856 q1=1 q2=1 q3=1 q4=1 q5=1 q6=1 q7=0 q8=0 q9=0 q10=0 COVIDscore=86
survdate=11/02/2020 &lt;/PRE&gt;
&lt;P&gt;Zipcode = 92365-4856&amp;nbsp;&amp;nbsp; is not a number. So attempting to convert it using any of the numeric informats fails. The - either has to precede digits to indicate negative values, -12345 OR in scientific notation to precede the exponent to indicate decimal places&amp;nbsp; 1.5E-5 for example.&lt;/P&gt;
&lt;P&gt;f you want Zipcode to be numeric you need to make a decision of what you expect for a numeric value and write specific code for that. Possibly : newzip = input(scan(zipcode,1,'-'),f5.) would be one way to create a five-digit numeric zip.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Similarly&amp;nbsp; survdate is not a simple number because of the / characters and can't be dealt with the same numeric informat. That would take a date informat such as mmddyy10. (and should have a date format associated so people can understand what is stored).&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Tue, 15 Dec 2020 05:35:43 GMT</pubDate>
    <dc:creator>ballardw</dc:creator>
    <dc:date>2020-12-15T05:35:43Z</dc:date>
    <item>
      <title>Converting Character variables to Numeric</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Converting-Character-variables-to-Numeric/m-p/705917#M216615</link>
      <description>&lt;P&gt;Hello all,&lt;/P&gt;&lt;P&gt;I am having some trouble converting my character variables to numerics. I have about 13 variables to convert so I was trying to use an array.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I currently have this array set up:&lt;/P&gt;&lt;P&gt;Array X[13] $ zipcode survdate q1 q2 q3 q4 q5 q6 q7 q8 q9 q10 COVIDscore;&lt;BR /&gt;Array Y[13] Newzipcode Newsurvdate Newq1 Newq2 Newq3 Newq4 Newq5 Newq6 Newq7 Newq8 Newq9 Newq10 NewCOVIDscore;&lt;BR /&gt;Do I=1 to 13;&lt;BR /&gt;Y(I) = input(X(I),best32.);&lt;BR /&gt;End;&lt;BR /&gt;Drop I&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;But I keep getting warning statements in my log:&lt;/P&gt;&lt;P&gt;WARNING: Limit set by ERRORS= option reached. Further errors of this type will not be printed.&lt;/P&gt;&lt;P&gt;NOTE: Mathematical operations could not be performed at the following places. The results of the operations have been set to&lt;BR /&gt;missing values.&lt;BR /&gt;Each place is given by: (Number of times) at (Line):(Column).&lt;BR /&gt;4052 at 686:18&lt;/P&gt;&lt;P&gt;686 Y(I) = input(X(I),best32.);&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 15 Dec 2020 03:45:18 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Converting-Character-variables-to-Numeric/m-p/705917#M216615</guid>
      <dc:creator>shortyofhb</dc:creator>
      <dc:date>2020-12-15T03:45:18Z</dc:date>
    </item>
    <item>
      <title>Re: Converting Character variables to Numeric</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Converting-Character-variables-to-Numeric/m-p/705920#M216616</link>
      <description>&lt;P&gt;You need to look at that messages before that in the log.&amp;nbsp; Basically it is saying the string does not contain a number.&lt;/P&gt;
&lt;PRE&gt;28   data test;
29     array X $32 string;
30     array Y number;
31     input string ;
32     y[1]=input(x[1],32.);
33   cards;

NOTE: Invalid argument to function INPUT at line 32 column 8.
RULE:      ----+----1----+----2----+----3----+----4----+----5----+----6----+----7----+----8----+----9----+----0
35         abc
_I_=. string=abc number=. _ERROR_=1 _N_=2
NOTE: Mathematical operations could not be performed at the following places. The results of the operations have been set to missing values.
      Each place is given by: (Number of times) at (Line):(Column).
      1 at 32:8
NOTE: The data set WORK.TEST has 2 observations and 2 variables.
NOTE: DATA statement used (Total process time):
      real time           0.01 seconds
      cpu time            0.01 seconds


36   ;&lt;/PRE&gt;
&lt;P&gt;If you don't care about the invalid strings and just want to create missing values without SAS writing notes into the SAS log you can use the ? or ?? modifier in front of the informat in the&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;37   data test;
38     array X $32 string;
39     array Y number;
40     input string ;
41     y[1]=input(x[1],?32.);
42   cards;

RULE:      ----+----1----+----2----+----3----+----4----+----5----+----6----+----7----+----8----+----9----+----0
44         abc
_I_=. string=abc number=. _ERROR_=1 _N_=2
NOTE: The data set WORK.TEST has 2 observations and 2 variables.
NOTE: DATA statement used (Total process time):
      real time           0.00 seconds
      cpu time            0.00 seconds


45   ;
46   data test;
47     array X $32 string;
48     array Y number;
49     input string ;
50     y[1]=input(x[1],??32.);
51   cards;

NOTE: The data set WORK.TEST has 2 observations and 2 variables.
NOTE: DATA statement used (Total process time):
      real time           0.00 seconds
      cpu time            0.00 seconds

54   ;&lt;/PRE&gt;
&lt;P&gt;Note there is not a separate informat named BEST. If you ask for it you just get the normal numeric informat.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 15 Dec 2020 04:06:46 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Converting-Character-variables-to-Numeric/m-p/705920#M216616</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2020-12-15T04:06:46Z</dc:date>
    </item>
    <item>
      <title>Re: Converting Character variables to Numeric</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Converting-Character-variables-to-Numeric/m-p/705927#M216621</link>
      <description>&lt;P&gt;The log messages above what I initially posted are:&lt;/P&gt;&lt;P&gt;id=3276 city=Otside La County age=50-59 gender= zipcode=92365-4856 q1=1 q2=1 q3=1 q4=1 q5=1 q6=1 q7=0 q8=0 q9=0 q10=0 COVIDscore=86&lt;BR /&gt;survdate=11/02/2020 Newgender= Newcity=Outside of Los Angeles Newq1=. Newq2=. Newq3=. Newq4=. Newq5=. Newq6=. Newq7=. Newq8=.&lt;BR /&gt;Newq9=. Newq10=. NewCOVIDscore=. Newsurvdate=. I=14 var1=. var2=. var3=1 var4=1 var5=1 var6=1 var7=1 var8=1 var9=0 var10=0 var11=0&lt;BR /&gt;var12=0 var13=86 _ERROR_=1 _N_=19&lt;BR /&gt;NOTE: Invalid argument to function INPUT at line 686 column 18.&lt;BR /&gt;NOTE: Invalid argument to function INPUT at line 686 column 18.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;It does that a bunch of times. Do you think its because the format of my zipcode and survdate? (zipcode in the dataset is 9 digits but I was instructed to change it to 5, and survdate in the dataset is MMDDYY10, but I was instructed to change it to weekday.&lt;/P&gt;</description>
      <pubDate>Tue, 15 Dec 2020 05:17:41 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Converting-Character-variables-to-Numeric/m-p/705927#M216621</guid>
      <dc:creator>shortyofhb</dc:creator>
      <dc:date>2020-12-15T05:17:41Z</dc:date>
    </item>
    <item>
      <title>Re: Converting Character variables to Numeric</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Converting-Character-variables-to-Numeric/m-p/705930#M216624</link>
      <description>&lt;P&gt;Let's look at some details of your existing data:&lt;/P&gt;
&lt;PRE&gt;zipcode=92365-4856 q1=1 q2=1 q3=1 q4=1 q5=1 q6=1 q7=0 q8=0 q9=0 q10=0 COVIDscore=86
survdate=11/02/2020 &lt;/PRE&gt;
&lt;P&gt;Zipcode = 92365-4856&amp;nbsp;&amp;nbsp; is not a number. So attempting to convert it using any of the numeric informats fails. The - either has to precede digits to indicate negative values, -12345 OR in scientific notation to precede the exponent to indicate decimal places&amp;nbsp; 1.5E-5 for example.&lt;/P&gt;
&lt;P&gt;f you want Zipcode to be numeric you need to make a decision of what you expect for a numeric value and write specific code for that. Possibly : newzip = input(scan(zipcode,1,'-'),f5.) would be one way to create a five-digit numeric zip.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Similarly&amp;nbsp; survdate is not a simple number because of the / characters and can't be dealt with the same numeric informat. That would take a date informat such as mmddyy10. (and should have a date format associated so people can understand what is stored).&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 15 Dec 2020 05:35:43 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Converting-Character-variables-to-Numeric/m-p/705930#M216624</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2020-12-15T05:35:43Z</dc:date>
    </item>
    <item>
      <title>Re: Converting Character variables to Numeric</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Converting-Character-variables-to-Numeric/m-p/705936#M216627</link>
      <description>&lt;P&gt;Maybe this is homework, if it is ignore the following.&lt;/P&gt;
&lt;P&gt;There is just one way to solve problems like yours: fix the data import!&lt;/P&gt;</description>
      <pubDate>Tue, 15 Dec 2020 05:58:10 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Converting-Character-variables-to-Numeric/m-p/705936#M216627</guid>
      <dc:creator>andreas_lds</dc:creator>
      <dc:date>2020-12-15T05:58:10Z</dc:date>
    </item>
    <item>
      <title>Re: Converting Character variables to Numeric</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Converting-Character-variables-to-Numeric/m-p/705940#M216629</link>
      <description>&lt;P&gt;Ah I see, so for the variables zipcode and survdate I would have to use the input function to convert it to a new variable. I cannot use the array for those 2? But for q1-q10 I can use the array?&lt;/P&gt;</description>
      <pubDate>Tue, 15 Dec 2020 06:17:13 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Converting-Character-variables-to-Numeric/m-p/705940#M216629</guid>
      <dc:creator>shortyofhb</dc:creator>
      <dc:date>2020-12-15T06:17:13Z</dc:date>
    </item>
    <item>
      <title>Re: Converting Character variables to Numeric</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Converting-Character-variables-to-Numeric/m-p/705941#M216630</link>
      <description>&lt;P&gt;It is a homework assignment unfortunately haha, the dataset is meant to be a mess!&lt;/P&gt;</description>
      <pubDate>Tue, 15 Dec 2020 06:17:44 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Converting-Character-variables-to-Numeric/m-p/705941#M216630</guid>
      <dc:creator>shortyofhb</dc:creator>
      <dc:date>2020-12-15T06:17:44Z</dc:date>
    </item>
    <item>
      <title>Re: Converting Character variables to Numeric</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Converting-Character-variables-to-Numeric/m-p/705945#M216634</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/360880"&gt;@shortyofhb&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;Ah I see, so for the variables zipcode and survdate I would have to use the input function to convert it to a new variable. I cannot use the array for those 2? But for q1-q10 I can use the array?&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;Correct.&lt;/P&gt;
&lt;P&gt;SAS is very nice when you have sequentially numbered variables to place in an array. You can use shorthand like: array &amp;lt;name&amp;gt; q1-q10. And for your new variables if you name an array like:&lt;/P&gt;
&lt;P&gt;Array newq(10) that will create 10 numeric variables named newq1, newq2, newq3 etc. It is a good idea to understand the individual naming though to get used to thinking of order of definition is important.&lt;/P&gt;</description>
      <pubDate>Tue, 15 Dec 2020 06:39:55 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Converting-Character-variables-to-Numeric/m-p/705945#M216634</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2020-12-15T06:39:55Z</dc:date>
    </item>
    <item>
      <title>Re: Converting Character variables to Numeric</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Converting-Character-variables-to-Numeric/m-p/705947#M216635</link>
      <description>&lt;P&gt;Thank you so much for the help!!&lt;/P&gt;&lt;P&gt;So I converted created a new variable for zipcode and survdate with the input function. That clarifies it up alot!&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;One more quick question (I hope). How would I go about using an array to recode a variable to saying something else? For example, in my Proc Format I have 1="Yes" and 0="No" my instructions are however are to "use an array to recode 0="No" to 2="No."&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Would I create an array and have it so when 0 is shown it gets replaced with 2? And then use a format to change 2 to "No"?&lt;/P&gt;</description>
      <pubDate>Tue, 15 Dec 2020 06:45:40 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Converting-Character-variables-to-Numeric/m-p/705947#M216635</guid>
      <dc:creator>shortyofhb</dc:creator>
      <dc:date>2020-12-15T06:45:40Z</dc:date>
    </item>
  </channel>
</rss>

