<?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: Unexpected result using informat to create numeric variable from character variable in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Unexpected-result-using-informat-to-create-numeric-variable-from/m-p/327083#M72933</link>
    <description>&lt;P&gt;The INPUT function sometimes behaves a bit differently than an input statement. Please see this:&lt;/P&gt;
&lt;PRE&gt;proc format library=work;
INVALUE my_order
"D01a_Tot" = 1
other = .
;
run;
data temp;
   informat x my_order.;
   input x;
datalines;
D01a_Tot_D02
D01a_TotP09
D01a_TotP12
D01a_TotP15
D01a_TotP16
D01a_Tot
run;
&lt;/PRE&gt;
&lt;P&gt;Note that to assign other values such as missing it is a good idea to explicitly set that in the INVALUE so there isn't any doubt of intent.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;It helps to post code into a code box opened using either the {i} or "running man" icon in this forum.&lt;/P&gt;</description>
    <pubDate>Tue, 24 Jan 2017 15:37:11 GMT</pubDate>
    <dc:creator>ballardw</dc:creator>
    <dc:date>2017-01-24T15:37:11Z</dc:date>
    <item>
      <title>Unexpected result using informat to create numeric variable from character variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Unexpected-result-using-informat-to-create-numeric-variable-from/m-p/326917#M72893</link>
      <description>&lt;P&gt;Hi.&lt;/P&gt;
&lt;P&gt;One of my SAS users has asked me to explain why the code below does not do what they expect - ie create a numeric variable (newvar) which is equal to 1 when the character variable myvar has a value of D01a_tot and is missing otherwise.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Now I realise there are many workarounds to this, which I have already fedback, and also that using the informat to code just one value is probably not necessary.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;But I'd still be keen to know why ALL records get assigned a value&amp;nbsp;of 1 when the numeric variable is created.&amp;nbsp;Clearly all the records start with "D01a_Tot"&amp;nbsp;and I guess that is causing the issue.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Can anyone explain why its happening and whether&amp;nbsp;it's actually not "unexpected".&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Thanks!&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Code below&amp;nbsp;(running on SAS 9.4M3)&lt;/P&gt;
&lt;P&gt;&lt;FONT color="#000080" face="Courier New" size="3"&gt;&lt;STRONG&gt;data&lt;/STRONG&gt;&lt;/FONT&gt;&lt;FONT face="Courier New" size="3"&gt; temp;&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT color="#0000ff" face="Courier New" size="3"&gt;length&lt;/FONT&gt;&lt;FONT face="Courier New" size="3"&gt; myvar $&lt;/FONT&gt;&lt;STRONG&gt;&lt;FONT color="#008080" face="Courier New" size="3"&gt;20&lt;/FONT&gt;&lt;/STRONG&gt;&lt;FONT face="Courier New" size="3"&gt;;&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face="Courier New" size="3"&gt;myvar=&lt;/FONT&gt;&lt;FONT color="#800080" face="Courier New" size="3"&gt;"D01a_Tot_D02"&lt;/FONT&gt;&lt;FONT face="Courier New" size="3"&gt;; &lt;/FONT&gt;&lt;FONT color="#0000ff" face="Courier New" size="3"&gt;output&lt;/FONT&gt;&lt;FONT face="Courier New" size="3"&gt;;&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face="Courier New" size="3"&gt;myvar=&lt;/FONT&gt;&lt;FONT color="#800080" face="Courier New" size="3"&gt;"D01a_TotP09"&lt;/FONT&gt;&lt;FONT face="Courier New" size="3"&gt;; &lt;/FONT&gt;&lt;FONT color="#0000ff" face="Courier New" size="3"&gt;output&lt;/FONT&gt;&lt;FONT face="Courier New" size="3"&gt;;&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face="Courier New" size="3"&gt;myvar=&lt;/FONT&gt;&lt;FONT color="#800080" face="Courier New" size="3"&gt;"D01a_TotP12"&lt;/FONT&gt;&lt;FONT face="Courier New" size="3"&gt;; &lt;/FONT&gt;&lt;FONT color="#0000ff" face="Courier New" size="3"&gt;output&lt;/FONT&gt;&lt;FONT face="Courier New" size="3"&gt;;&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face="Courier New" size="3"&gt;myvar=&lt;/FONT&gt;&lt;FONT color="#800080" face="Courier New" size="3"&gt;"D01a_TotP15"&lt;/FONT&gt;&lt;FONT face="Courier New" size="3"&gt;; &lt;/FONT&gt;&lt;FONT color="#0000ff" face="Courier New" size="3"&gt;output&lt;/FONT&gt;&lt;FONT face="Courier New" size="3"&gt;;&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face="Courier New" size="3"&gt;myvar=&lt;/FONT&gt;&lt;FONT color="#800080" face="Courier New" size="3"&gt;"D01a_TotP16"&lt;/FONT&gt;&lt;FONT face="Courier New" size="3"&gt;; &lt;/FONT&gt;&lt;FONT color="#0000ff" face="Courier New" size="3"&gt;output&lt;/FONT&gt;&lt;FONT face="Courier New" size="3"&gt;;&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face="Courier New" size="3"&gt;myvar=&lt;/FONT&gt;&lt;FONT color="#800080" face="Courier New" size="3"&gt;"D01a_Tot"&lt;/FONT&gt;&lt;FONT face="Courier New" size="3"&gt;; &lt;/FONT&gt;&lt;FONT color="#0000ff" face="Courier New" size="3"&gt;output&lt;/FONT&gt;&lt;FONT face="Courier New" size="3"&gt;;&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT color="#000080" face="Courier New" size="3"&gt;&lt;STRONG&gt;run&lt;/STRONG&gt;&lt;/FONT&gt;&lt;FONT face="Courier New" size="3"&gt;;&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;FONT color="#000080" face="Courier New" size="3"&gt;&lt;STRONG&gt;proc&lt;/STRONG&gt;&lt;/FONT&gt; &lt;STRONG&gt;&lt;FONT color="#000080" face="Courier New" size="3"&gt;format&lt;/FONT&gt;&lt;/STRONG&gt;&lt;FONT face="Courier New" size="3"&gt;;&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT color="#0000ff" face="Courier New" size="3"&gt;INVALUE&lt;/FONT&gt;&lt;FONT face="Courier New" size="3"&gt; my_order&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT color="#800080" face="Courier New" size="3"&gt;"D01a_Tot"&lt;/FONT&gt;&lt;FONT face="Courier New" size="3"&gt; = &lt;/FONT&gt;&lt;STRONG&gt;&lt;FONT color="#008080" face="Courier New" size="3"&gt;1&lt;/FONT&gt;&lt;/STRONG&gt;&lt;FONT face="Courier New" size="3"&gt;;&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT color="#000080" face="Courier New" size="3"&gt;&lt;STRONG&gt;run&lt;/STRONG&gt;&lt;/FONT&gt;&lt;FONT face="Courier New" size="3"&gt;;&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;FONT color="#000080" face="Courier New" size="3"&gt;&lt;STRONG&gt;data&lt;/STRONG&gt;&lt;/FONT&gt;&lt;FONT face="Courier New" size="3"&gt; temp2;&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT color="#0000ff" face="Courier New" size="3"&gt;set&lt;/FONT&gt;&lt;FONT face="Courier New" size="3"&gt; temp; &lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face="Courier New" size="3"&gt; newvar=input(myvar,&lt;/FONT&gt;&lt;FONT color="#008080" face="Courier New" size="3"&gt;my_order.&lt;/FONT&gt;&lt;FONT face="Courier New" size="3"&gt;);&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT color="#000080" face="Courier New" size="3"&gt;&lt;STRONG&gt;run&lt;/STRONG&gt;&lt;/FONT&gt;&lt;FONT face="Courier New" size="3"&gt;;&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 24 Jan 2017 02:50:19 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Unexpected-result-using-informat-to-create-numeric-variable-from/m-p/326917#M72893</guid>
      <dc:creator>ABritinAus</dc:creator>
      <dc:date>2017-01-24T02:50:19Z</dc:date>
    </item>
    <item>
      <title>Re: Unexpected result using informat to create numeric variable from character variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Unexpected-result-using-informat-to-create-numeric-variable-from/m-p/326981#M72917</link>
      <description>&lt;P&gt;The default length of the invalue is the length of D01a_Tot - 8, which is the length of the comparison being made on your&amp;nbsp;&lt;EM&gt;myvar&lt;/EM&gt; variable; everything after that is ignored.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If you change the format to&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc format;
INVALUE my_order(default=20)
"D01a_Tot" = 1
other = .;             /* Stops error messages */
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;or the data step to:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data temp2;
set temp;
newvar=input(myvar, my_order20.);
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;then you'll get the result you require. Note that the second suggestion doesn't stop error messages.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I would recommend the former, because you can reuse the format in all your other data steps without thinking about it.&lt;/P&gt;</description>
      <pubDate>Tue, 24 Jan 2017 09:11:09 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Unexpected-result-using-informat-to-create-numeric-variable-from/m-p/326981#M72917</guid>
      <dc:creator>LaurieF</dc:creator>
      <dc:date>2017-01-24T09:11:09Z</dc:date>
    </item>
    <item>
      <title>Re: Unexpected result using informat to create numeric variable from character variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Unexpected-result-using-informat-to-create-numeric-variable-from/m-p/327083#M72933</link>
      <description>&lt;P&gt;The INPUT function sometimes behaves a bit differently than an input statement. Please see this:&lt;/P&gt;
&lt;PRE&gt;proc format library=work;
INVALUE my_order
"D01a_Tot" = 1
other = .
;
run;
data temp;
   informat x my_order.;
   input x;
datalines;
D01a_Tot_D02
D01a_TotP09
D01a_TotP12
D01a_TotP15
D01a_TotP16
D01a_Tot
run;
&lt;/PRE&gt;
&lt;P&gt;Note that to assign other values such as missing it is a good idea to explicitly set that in the INVALUE so there isn't any doubt of intent.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;It helps to post code into a code box opened using either the {i} or "running man" icon in this forum.&lt;/P&gt;</description>
      <pubDate>Tue, 24 Jan 2017 15:37:11 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Unexpected-result-using-informat-to-create-numeric-variable-from/m-p/327083#M72933</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2017-01-24T15:37:11Z</dc:date>
    </item>
    <item>
      <title>Re: Unexpected result using informat to create numeric variable from character variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Unexpected-result-using-informat-to-create-numeric-variable-from/m-p/327207#M72973</link>
      <description>&lt;P&gt;Thanks.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;That all makes sense.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 24 Jan 2017 22:06:35 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Unexpected-result-using-informat-to-create-numeric-variable-from/m-p/327207#M72973</guid>
      <dc:creator>ABritinAus</dc:creator>
      <dc:date>2017-01-24T22:06:35Z</dc:date>
    </item>
    <item>
      <title>Re: Unexpected result using informat to create numeric variable from character variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Unexpected-result-using-informat-to-create-numeric-variable-from/m-p/327209#M72974</link>
      <description>&lt;P&gt;Thanks for the prompt reply - the time zone here is handy for overnight responses&lt;img id="smileyhappy" class="emoticon emoticon-smileyhappy" src="https://communities.sas.com/i/smilies/16x16_smiley-happy.png" alt="Smiley Happy" title="Smiley Happy" /&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Will use that for reading in data using informats.&amp;nbsp;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;And thanks for the tip re inserting the code: will note for future.&lt;/P&gt;</description>
      <pubDate>Tue, 24 Jan 2017 22:10:48 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Unexpected-result-using-informat-to-create-numeric-variable-from/m-p/327209#M72974</guid>
      <dc:creator>ABritinAus</dc:creator>
      <dc:date>2017-01-24T22:10:48Z</dc:date>
    </item>
    <item>
      <title>Re: Unexpected result using informat to create numeric variable from character variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Unexpected-result-using-informat-to-create-numeric-variable-from/m-p/327211#M72975</link>
      <description>&lt;P&gt;I'm only two hours (probably) ahead of you - Wellington!&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Glad to have helped.&lt;/P&gt;</description>
      <pubDate>Tue, 24 Jan 2017 22:13:51 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Unexpected-result-using-informat-to-create-numeric-variable-from/m-p/327211#M72975</guid>
      <dc:creator>LaurieF</dc:creator>
      <dc:date>2017-01-24T22:13:51Z</dc:date>
    </item>
  </channel>
</rss>

