<?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 Format a Variable That is Both Numeric and Categorical in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Format-a-Variable-That-is-Both-Numeric-and-Categorical/m-p/515162#M138977</link>
    <description>&lt;P&gt;&lt;STRONG&gt;Question:&amp;nbsp;&lt;/STRONG&gt;I have a character variable where some responses are coded as all numbers, and some responses include letters. Is there a way I can tell SAS to format only the responses coded as numbers as numeric?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;Context:&amp;nbsp;&lt;/STRONG&gt;My variable of interest is &lt;STRONG&gt;"CAUSE_DEATH"&lt;/STRONG&gt; (cause of death) which is a character variable; length 4, format/informat $4.&amp;nbsp;&lt;/P&gt;&lt;P&gt;Causes of death are coded using ICD-9 and ICD-10 classification system depending on what year my respondent died. ICD 9 codes are 4 digit numbers, whereas ICD 10 codes include a letter as the first digit.&lt;/P&gt;&lt;P&gt;I want to group these deaths into broad categories&amp;nbsp;in the most efficient way possible. Is there a way to tell SAS to format the responses with only numbers (i.e. ICD 9 codes) as numeric, while keeping the ICD 10-coded responses as character? I am using SAS Enterprise Guide 7.1.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;Example:&amp;nbsp;&lt;/STRONG&gt;One broad cause of death category&amp;nbsp;is&amp;nbsp;&lt;STRONG&gt;Infectious Diseases&lt;/STRONG&gt;. ICD 9 codes for this are between 0010 and 1399. ICD 10 codes for this are between A000 and B999. The following code works (for the ICD 10 codes):&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc freq data=&lt;EM&gt;lib.datafile&lt;/EM&gt;;
table CAUSE_DEATH;
where substr(CAUSE_DEATH,1,1='A') or substr(CAUSE_DEATH,1,1='B');
run;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;The following code obviously does not work (for the ICD 9 codes):&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc freq data=&lt;EM&gt;lib.datafile&lt;/EM&gt;;
table CAUSE_DEATH;
where 0010&amp;lt;=CAUSE_DEATH&amp;lt;=1399;
run;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;My goal is to be able to do this:&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data causedeath; set &lt;EM&gt;lib.datafile&lt;/EM&gt;;
infectious_disease=&lt;STRONG&gt;.&lt;/STRONG&gt;;
if 0010&amp;lt;=CAUSE_DEATH&amp;lt;=1399 OR substr(CAUSE_DEATH,1,1='A') OR substr(CAUSE_DEATH,1,1='B') then infectious_disease=1; else infectious_disease=0;
run;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;Can someone please let me know the best way to make this possible? I would not want to write out each individual ICD 9 code in my syntax since that would be very time consuming.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thank you for your time and help!&lt;/P&gt;</description>
    <pubDate>Wed, 21 Nov 2018 17:09:52 GMT</pubDate>
    <dc:creator>TL93</dc:creator>
    <dc:date>2018-11-21T17:09:52Z</dc:date>
    <item>
      <title>Format a Variable That is Both Numeric and Categorical</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Format-a-Variable-That-is-Both-Numeric-and-Categorical/m-p/515162#M138977</link>
      <description>&lt;P&gt;&lt;STRONG&gt;Question:&amp;nbsp;&lt;/STRONG&gt;I have a character variable where some responses are coded as all numbers, and some responses include letters. Is there a way I can tell SAS to format only the responses coded as numbers as numeric?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;Context:&amp;nbsp;&lt;/STRONG&gt;My variable of interest is &lt;STRONG&gt;"CAUSE_DEATH"&lt;/STRONG&gt; (cause of death) which is a character variable; length 4, format/informat $4.&amp;nbsp;&lt;/P&gt;&lt;P&gt;Causes of death are coded using ICD-9 and ICD-10 classification system depending on what year my respondent died. ICD 9 codes are 4 digit numbers, whereas ICD 10 codes include a letter as the first digit.&lt;/P&gt;&lt;P&gt;I want to group these deaths into broad categories&amp;nbsp;in the most efficient way possible. Is there a way to tell SAS to format the responses with only numbers (i.e. ICD 9 codes) as numeric, while keeping the ICD 10-coded responses as character? I am using SAS Enterprise Guide 7.1.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;Example:&amp;nbsp;&lt;/STRONG&gt;One broad cause of death category&amp;nbsp;is&amp;nbsp;&lt;STRONG&gt;Infectious Diseases&lt;/STRONG&gt;. ICD 9 codes for this are between 0010 and 1399. ICD 10 codes for this are between A000 and B999. The following code works (for the ICD 10 codes):&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc freq data=&lt;EM&gt;lib.datafile&lt;/EM&gt;;
table CAUSE_DEATH;
where substr(CAUSE_DEATH,1,1='A') or substr(CAUSE_DEATH,1,1='B');
run;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;The following code obviously does not work (for the ICD 9 codes):&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc freq data=&lt;EM&gt;lib.datafile&lt;/EM&gt;;
table CAUSE_DEATH;
where 0010&amp;lt;=CAUSE_DEATH&amp;lt;=1399;
run;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;My goal is to be able to do this:&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data causedeath; set &lt;EM&gt;lib.datafile&lt;/EM&gt;;
infectious_disease=&lt;STRONG&gt;.&lt;/STRONG&gt;;
if 0010&amp;lt;=CAUSE_DEATH&amp;lt;=1399 OR substr(CAUSE_DEATH,1,1='A') OR substr(CAUSE_DEATH,1,1='B') then infectious_disease=1; else infectious_disease=0;
run;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;Can someone please let me know the best way to make this possible? I would not want to write out each individual ICD 9 code in my syntax since that would be very time consuming.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thank you for your time and help!&lt;/P&gt;</description>
      <pubDate>Wed, 21 Nov 2018 17:09:52 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Format-a-Variable-That-is-Both-Numeric-and-Categorical/m-p/515162#M138977</guid>
      <dc:creator>TL93</dc:creator>
      <dc:date>2018-11-21T17:09:52Z</dc:date>
    </item>
    <item>
      <title>Re: Format a Variable That is Both Numeric and Categorical</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Format-a-Variable-That-is-Both-Numeric-and-Categorical/m-p/515168#M138979</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/227543"&gt;@TL93&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The following code obviously does not work (for the ICD 9 codes):&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc freq data=&lt;EM&gt;lib.datafile&lt;/EM&gt;;
table CAUSE_DEATH;
where 0010&amp;lt;=CAUSE_DEATH&amp;lt;=1399;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;Why&amp;nbsp;doesn't it work? Because the variable CAUSE_DEATH is character and not numeric?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If so, the you can convert CAUSE_DEATH to numeric to do the comparison:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;where 10&amp;lt;=input(CAUSE_DEATH,4.)&amp;lt;=1399;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;and this solves the problem, you can now modify your final code this way as well.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Incidentally, you can also use this to find strings which begin with letters A or B:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;where CAUSE_DEATH=:'A' or CAUSE_DEATH=:'B';&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 21 Nov 2018 17:16:43 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Format-a-Variable-That-is-Both-Numeric-and-Categorical/m-p/515168#M138979</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2018-11-21T17:16:43Z</dc:date>
    </item>
    <item>
      <title>Re: Format a Variable That is Both Numeric and Categorical</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Format-a-Variable-That-is-Both-Numeric-and-Categorical/m-p/515180#M138983</link>
      <description>&lt;P&gt;Hi Paige,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thank you so much for your response, I think it has answered my question. Yes, CAUSE_DEATH is a character variable. I used the following syntax with your suggestions and it worked:&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data causedeath; set &lt;EM&gt;lib.datafile&lt;/EM&gt;;
infectious_disease=&lt;STRONG&gt;.&lt;/STRONG&gt;;
if 0010&amp;lt;=input(CAUSE_DEATH,4.)&amp;lt;=1399 or CAUSE_DEATH=:'A' or CAUSE_DEATH=:'B' then infectious_disease=1; else infectious_disease=0;
run;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;Sorry for my ignorance, I wasn't aware that I can do the conversion in a "where" (or in this case "if") statement. Does this permanently change my CAUSE_DEATH format? I would like to keep it as a character variable so that my ICD 10-coded responses are not altered. I need to have the letter in front of the ICD 10-coded responses so that I can continue to make other groupings.&lt;/P&gt;</description>
      <pubDate>Wed, 21 Nov 2018 17:41:27 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Format-a-Variable-That-is-Both-Numeric-and-Categorical/m-p/515180#M138983</guid>
      <dc:creator>TL93</dc:creator>
      <dc:date>2018-11-21T17:41:27Z</dc:date>
    </item>
    <item>
      <title>Re: Format a Variable That is Both Numeric and Categorical</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Format-a-Variable-That-is-Both-Numeric-and-Categorical/m-p/515183#M138984</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/227543"&gt;@TL93&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;I wasn't aware that I can do the conversion in a "where" (or in this case "if") statement. Does this permanently change my CAUSE_DEATH format? I would like to keep it as a character variable so that my ICD 10-coded responses are not altered. I need to have the letter in front of the ICD 10-coded responses so that I can continue to make other groupings.&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;CAUSE_DEATH is unchanged (which you could confirm simply by looking at the data set).&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;A numeric version of CAUSE_DEATH is used in the IF statement, but this numeric version of CAUSE_DEATH is not stored in the data set.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 21 Nov 2018 17:47:12 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Format-a-Variable-That-is-Both-Numeric-and-Categorical/m-p/515183#M138984</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2018-11-21T17:47:12Z</dc:date>
    </item>
    <item>
      <title>Re: Format a Variable That is Both Numeric and Categorical</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Format-a-Variable-That-is-Both-Numeric-and-Categorical/m-p/515184#M138985</link>
      <description>Got it! Again, thank you very much for your help!</description>
      <pubDate>Wed, 21 Nov 2018 17:48:34 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Format-a-Variable-That-is-Both-Numeric-and-Categorical/m-p/515184#M138985</guid>
      <dc:creator>TL93</dc:creator>
      <dc:date>2018-11-21T17:48:34Z</dc:date>
    </item>
    <item>
      <title>Re: Format a Variable That is Both Numeric and Categorical</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Format-a-Variable-That-is-Both-Numeric-and-Categorical/m-p/515257#M138993</link>
      <description>Looks like a candidate for special user-defined in/formats.&lt;BR /&gt;That separates the definition of the "group" from the coding which is logically straightforward&lt;BR /&gt;..... Is this candidate one of the required group .....&lt;BR /&gt;     If input( cause_death, user_informat. ) ;&lt;BR /&gt;</description>
      <pubDate>Wed, 21 Nov 2018 22:29:22 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Format-a-Variable-That-is-Both-Numeric-and-Categorical/m-p/515257#M138993</guid>
      <dc:creator>Peter_C</dc:creator>
      <dc:date>2018-11-21T22:29:22Z</dc:date>
    </item>
    <item>
      <title>Re: Format a Variable That is Both Numeric and Categorical</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Format-a-Variable-That-is-Both-Numeric-and-Categorical/m-p/515264#M138995</link>
      <description>&lt;P&gt;FYI: You can do size comparisions of character variables . e.g. '1' &amp;lt; '2' .&amp;nbsp; &amp;nbsp;Characters have an order, and one can be less than another.&lt;/P&gt;&lt;P&gt;However be careful , as '10' &amp;lt; '2' also .&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;In your case you are OK though because you have leading zeros . Therfore you could just do this.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE class=" language-sas"&gt;&lt;CODE class="  language-sas"&gt;&lt;SPAN class="token procnames"&gt;data&lt;/SPAN&gt; causedeath&lt;SPAN class="token punctuation"&gt;;&lt;/SPAN&gt; &lt;SPAN class="token keyword"&gt;set&lt;/SPAN&gt; lib&lt;SPAN class="token punctuation"&gt;.&lt;/SPAN&gt;datafile&lt;SPAN class="token punctuation"&gt;;&lt;/SPAN&gt;
infectious_disease&lt;SPAN class="token operator"&gt;=&lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;.&lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;;&lt;/SPAN&gt;
&lt;SPAN class="token keyword"&gt;if&lt;/SPAN&gt; '&lt;SPAN class="token number"&gt;0010'&lt;/SPAN&gt;&lt;SPAN class="token operator"&gt;&amp;lt;=&lt;/SPAN&gt;CAUSE_DEATH&lt;SPAN class="token operator"&gt;&amp;lt;='&lt;/SPAN&gt;&lt;SPAN class="token number"&gt;1399'&lt;/SPAN&gt; OR &lt;SPAN class="token function"&gt;substr&lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;(&lt;/SPAN&gt;CAUSE_DEATH&lt;SPAN class="token punctuation"&gt;,&lt;/SPAN&gt;&lt;SPAN class="token number"&gt;1&lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;,&lt;/SPAN&gt;&lt;SPAN class="token number"&gt;1&lt;/SPAN&gt;&lt;SPAN class="token operator"&gt;=&lt;/SPAN&gt;&lt;SPAN class="token string"&gt;'A'&lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;)&lt;/SPAN&gt; OR &lt;SPAN class="token function"&gt;substr&lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;(&lt;/SPAN&gt;CAUSE_DEATH&lt;SPAN class="token punctuation"&gt;,&lt;/SPAN&gt;&lt;SPAN class="token number"&gt;1&lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;,&lt;/SPAN&gt;&lt;SPAN class="token number"&gt;1&lt;/SPAN&gt;&lt;SPAN class="token operator"&gt;=&lt;/SPAN&gt;&lt;SPAN class="token string"&gt;'B'&lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;)&lt;/SPAN&gt; &lt;SPAN class="token keyword"&gt;then&lt;/SPAN&gt; infectious_disease&lt;SPAN class="token operator"&gt;=&lt;/SPAN&gt;&lt;SPAN class="token number"&gt;1&lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;;&lt;/SPAN&gt; &lt;SPAN class="token keyword"&gt;else&lt;/SPAN&gt; infectious_disease&lt;SPAN class="token operator"&gt;=&lt;/SPAN&gt;&lt;SPAN class="token number"&gt;0&lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;;&lt;/SPAN&gt;
&lt;SPAN class="token procnames"&gt;run&lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;;&lt;/SPAN&gt;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 21 Nov 2018 23:27:47 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Format-a-Variable-That-is-Both-Numeric-and-Categorical/m-p/515264#M138995</guid>
      <dc:creator>34reqrwe</dc:creator>
      <dc:date>2018-11-21T23:27:47Z</dc:date>
    </item>
  </channel>
</rss>

