<?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 Characterizing using IF-THEN STATEMENTS in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Characterizing-using-IF-THEN-STATEMENTS/m-p/675341#M203453</link>
    <description>&lt;P&gt;Hello,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp; I have a huge dataset with a ton of different injury types, and am wondering if there is an easier way to create a different variable other than a bunch of of-then statements. I obviously don't need every type of injury to have its own category, and was hoping to group similar ones together, but it's taking so long.&lt;/P&gt;&lt;P&gt;I attached the cause of injury list below.&amp;nbsp;&lt;/P&gt;&lt;P&gt;Here is the code I started typing:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;if&lt;/SPAN&gt; cause_of_injury in (&lt;SPAN&gt;"FALL"&lt;/SPAN&gt;) &lt;SPAN&gt;then&lt;/SPAN&gt; cause=&lt;SPAN&gt;"FALL"&lt;/SPAN&gt;;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;if&lt;/SPAN&gt; cause_of_injury in (&lt;SPAN&gt;"FALL SL"&lt;/SPAN&gt;) &lt;SPAN&gt;then&lt;/SPAN&gt; cause=&lt;SPAN&gt;"FALL_SL"&lt;/SPAN&gt;;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;if&lt;/SPAN&gt; cause_of_injury in (&lt;SPAN&gt;"MVA"&lt;/SPAN&gt;) &lt;SPAN&gt;then&lt;/SPAN&gt; cause=&lt;SPAN&gt;"MVA"&lt;/SPAN&gt;;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;if&lt;/SPAN&gt; cause_of_injury in (&lt;SPAN&gt;"FALL HEIGHT"&lt;/SPAN&gt;) &lt;SPAN&gt;then&lt;/SPAN&gt; cause=&lt;SPAN&gt;"FALL_HEIGHT"&lt;/SPAN&gt;;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;if&lt;/SPAN&gt; cause_of_injury in (&lt;SPAN&gt;"FALL SL"&lt;/SPAN&gt;) &lt;SPAN&gt;then&lt;/SPAN&gt; cause=&lt;SPAN&gt;"FALL_SL"&lt;/SPAN&gt;;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;if&lt;/SPAN&gt; cause_of_injury in (&lt;SPAN&gt;"MCA"&lt;/SPAN&gt;) &lt;SPAN&gt;then&lt;/SPAN&gt; cause=&lt;SPAN&gt;"MCA"&lt;/SPAN&gt;;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;if&lt;/SPAN&gt; cause_of_injury in (&lt;SPAN&gt;"SKI"&lt;/SPAN&gt;, &lt;SPAN&gt;"SNOWB"&lt;/SPAN&gt;, &lt;SPAN&gt;"SMOB"&lt;/SPAN&gt;, &lt;SPAN&gt;"SLED"&lt;/SPAN&gt;, &lt;SPAN&gt;"SNOWB"&lt;/SPAN&gt;) &lt;SPAN&gt;then&lt;/SPAN&gt; cause=&lt;SPAN&gt;"SNOW"&lt;/SPAN&gt;;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;if&lt;/SPAN&gt; cause_of_injury in (&lt;SPAN&gt;"ASLT"&lt;/SPAN&gt;) &lt;SPAN&gt;then&lt;/SPAN&gt; cause=&lt;SPAN&gt;"ASLT"&lt;/SPAN&gt;;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;if&lt;/SPAN&gt; cause_of_injury in (&lt;SPAN&gt;"BIKE"&lt;/SPAN&gt;) &lt;SPAN&gt;then&lt;/SPAN&gt; cause=&lt;SPAN&gt;"BIKE"&lt;/SPAN&gt;;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;There are sooo many categories. Any tips/ideas?&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>Fri, 07 Aug 2020 23:16:35 GMT</pubDate>
    <dc:creator>stancemcgraw</dc:creator>
    <dc:date>2020-08-07T23:16:35Z</dc:date>
    <item>
      <title>Characterizing using IF-THEN STATEMENTS</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Characterizing-using-IF-THEN-STATEMENTS/m-p/675341#M203453</link>
      <description>&lt;P&gt;Hello,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp; I have a huge dataset with a ton of different injury types, and am wondering if there is an easier way to create a different variable other than a bunch of of-then statements. I obviously don't need every type of injury to have its own category, and was hoping to group similar ones together, but it's taking so long.&lt;/P&gt;&lt;P&gt;I attached the cause of injury list below.&amp;nbsp;&lt;/P&gt;&lt;P&gt;Here is the code I started typing:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;if&lt;/SPAN&gt; cause_of_injury in (&lt;SPAN&gt;"FALL"&lt;/SPAN&gt;) &lt;SPAN&gt;then&lt;/SPAN&gt; cause=&lt;SPAN&gt;"FALL"&lt;/SPAN&gt;;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;if&lt;/SPAN&gt; cause_of_injury in (&lt;SPAN&gt;"FALL SL"&lt;/SPAN&gt;) &lt;SPAN&gt;then&lt;/SPAN&gt; cause=&lt;SPAN&gt;"FALL_SL"&lt;/SPAN&gt;;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;if&lt;/SPAN&gt; cause_of_injury in (&lt;SPAN&gt;"MVA"&lt;/SPAN&gt;) &lt;SPAN&gt;then&lt;/SPAN&gt; cause=&lt;SPAN&gt;"MVA"&lt;/SPAN&gt;;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;if&lt;/SPAN&gt; cause_of_injury in (&lt;SPAN&gt;"FALL HEIGHT"&lt;/SPAN&gt;) &lt;SPAN&gt;then&lt;/SPAN&gt; cause=&lt;SPAN&gt;"FALL_HEIGHT"&lt;/SPAN&gt;;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;if&lt;/SPAN&gt; cause_of_injury in (&lt;SPAN&gt;"FALL SL"&lt;/SPAN&gt;) &lt;SPAN&gt;then&lt;/SPAN&gt; cause=&lt;SPAN&gt;"FALL_SL"&lt;/SPAN&gt;;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;if&lt;/SPAN&gt; cause_of_injury in (&lt;SPAN&gt;"MCA"&lt;/SPAN&gt;) &lt;SPAN&gt;then&lt;/SPAN&gt; cause=&lt;SPAN&gt;"MCA"&lt;/SPAN&gt;;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;if&lt;/SPAN&gt; cause_of_injury in (&lt;SPAN&gt;"SKI"&lt;/SPAN&gt;, &lt;SPAN&gt;"SNOWB"&lt;/SPAN&gt;, &lt;SPAN&gt;"SMOB"&lt;/SPAN&gt;, &lt;SPAN&gt;"SLED"&lt;/SPAN&gt;, &lt;SPAN&gt;"SNOWB"&lt;/SPAN&gt;) &lt;SPAN&gt;then&lt;/SPAN&gt; cause=&lt;SPAN&gt;"SNOW"&lt;/SPAN&gt;;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;if&lt;/SPAN&gt; cause_of_injury in (&lt;SPAN&gt;"ASLT"&lt;/SPAN&gt;) &lt;SPAN&gt;then&lt;/SPAN&gt; cause=&lt;SPAN&gt;"ASLT"&lt;/SPAN&gt;;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;if&lt;/SPAN&gt; cause_of_injury in (&lt;SPAN&gt;"BIKE"&lt;/SPAN&gt;) &lt;SPAN&gt;then&lt;/SPAN&gt; cause=&lt;SPAN&gt;"BIKE"&lt;/SPAN&gt;;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;There are sooo many categories. Any tips/ideas?&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>Fri, 07 Aug 2020 23:16:35 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Characterizing-using-IF-THEN-STATEMENTS/m-p/675341#M203453</guid>
      <dc:creator>stancemcgraw</dc:creator>
      <dc:date>2020-08-07T23:16:35Z</dc:date>
    </item>
    <item>
      <title>Re: Characterizing using IF-THEN STATEMENTS</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Characterizing-using-IF-THEN-STATEMENTS/m-p/675346#M203457</link>
      <description>&lt;P&gt;According to sample given it seems that in most cases the&amp;nbsp;&lt;STRONG&gt;cause = cause_of_injury,&lt;/STRONG&gt;&lt;/P&gt;
&lt;P&gt;in other cases the different &lt;STRONG&gt;cause_of_injury&amp;nbsp;&lt;/STRONG&gt;are gathered to one&amp;nbsp;&lt;STRONG&gt;cause&lt;/STRONG&gt;&lt;STRONG&gt;&amp;nbsp;&lt;/STRONG&gt;then you can write:&lt;/P&gt;
&lt;P&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; if&amp;nbsp;&lt;STRONG&gt;cause_of_injury&amp;nbsp;&lt;/STRONG&gt; in (list 1 of "value"s) then&amp;nbsp;&lt;STRONG&gt;cause&lt;/STRONG&gt; = &amp;lt;value1&amp;gt;; else&lt;/P&gt;
&lt;P&gt;&amp;nbsp; &amp;nbsp;&amp;nbsp; &amp;nbsp;if&amp;nbsp;&lt;STRONG&gt;cause_of_injury&amp;nbsp;&lt;/STRONG&gt; in (list 2 of "value"s) then&amp;nbsp;&lt;STRONG&gt;cause&lt;/STRONG&gt; = &amp;lt;value2&amp;gt;; else&lt;/P&gt;
&lt;P&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; ..... up to last group list .... ; else&amp;nbsp;&lt;STRONG&gt;&amp;nbsp;cause = cause_of_injury;&lt;/STRONG&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Alternatively create a table of distinct values of &lt;STRONG&gt;cause_of_injury&lt;/STRONG&gt;&lt;STRONG&gt;&amp;nbsp;&lt;/STRONG&gt;&lt;/P&gt;
&lt;P&gt;and assign a second variable the wanted category &lt;STRONG&gt;cause&amp;nbsp;&lt;/STRONG&gt;.&lt;/P&gt;
&lt;P&gt;Having that table - then run SQL JOIN of your data with categorized table.&lt;/P&gt;</description>
      <pubDate>Sat, 08 Aug 2020 00:03:27 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Characterizing-using-IF-THEN-STATEMENTS/m-p/675346#M203457</guid>
      <dc:creator>Shmuel</dc:creator>
      <dc:date>2020-08-08T00:03:27Z</dc:date>
    </item>
    <item>
      <title>Re: Characterizing using IF-THEN STATEMENTS</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Characterizing-using-IF-THEN-STATEMENTS/m-p/675347#M203458</link>
      <description>&lt;P&gt;Use PROC FREQ on the cause_of_injury type and pipe it to Excel. In Excel, map your categories to the new ones which is a lot easier because you can sort and group them visually. Then re-import that file as a lookup table and merge them together.&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sat, 08 Aug 2020 00:02:11 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Characterizing-using-IF-THEN-STATEMENTS/m-p/675347#M203458</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2020-08-08T00:02:11Z</dc:date>
    </item>
    <item>
      <title>Re: Characterizing using IF-THEN STATEMENTS</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Characterizing-using-IF-THEN-STATEMENTS/m-p/675350#M203460</link>
      <description>&lt;P&gt;I often use a mix of excel and sas to do something like this.&lt;/P&gt;
&lt;P&gt;For example, I generate the unique categories with sas (like with proc freq) then copy them to a column in excel, then in second column, I enter the new value.&amp;nbsp; Then in third column, I create the sas code statement, referring to the excel cells.&amp;nbsp; Excel is very easy to copy/paste cells so this is pretty efficient.&amp;nbsp; For example (and need to pay attention to the quotes so the text is properly quoted in the code):&lt;/P&gt;
&lt;P&gt;&amp;nbsp;="if cause_of_injury in (' " &amp;amp; a1 &amp;amp; " ') then cause=' " &amp;amp; b1 " ';' "&amp;nbsp; &lt;/P&gt;
&lt;P&gt;Then copy all rows back to SAS code.&amp;nbsp; You can even store in a separate .sas file and use the %include statement which helps keep code cleaner.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Sometimes I create a giant proc format statement with this same method, and then use PUT statement to assign the new variable.&lt;/P&gt;
&lt;P&gt;For example, in excel:&lt;/P&gt;
&lt;P&gt;=" ' " &amp;amp; a1 &amp;amp; " ' = ' "&amp;nbsp; &amp;amp; b1&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Then in the sas code:&lt;BR /&gt;proc format;&lt;/P&gt;
&lt;P&gt;value $causefmt;&lt;/P&gt;
&lt;P&gt;[copy all the lines from excel]&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;data tmp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp; set data;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;cause=put(cause_of_injury,$causefmt.);&lt;/P&gt;
&lt;P&gt;run;&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>Sat, 08 Aug 2020 00:49:51 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Characterizing-using-IF-THEN-STATEMENTS/m-p/675350#M203460</guid>
      <dc:creator>Sascoder</dc:creator>
      <dc:date>2020-08-08T00:49:51Z</dc:date>
    </item>
    <item>
      <title>Re: Characterizing using IF-THEN STATEMENTS</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Characterizing-using-IF-THEN-STATEMENTS/m-p/675388#M203490</link>
      <description>&lt;P&gt;Depending on what you want to do, there are several options.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If you want to simplify the code a bit, you can use a SELECT statement instead of all the IFs:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;select (cause_of_injury);
  when ("FALL SL") cause="FALL_SL";
  when ("MVA") cause="MVA";
  when ("FALL") cause="FALL";
  when ("FALL HEIGHT") cause="FALL_HEIGHT";
  when ("FALL SL") cause="FALL_SL";
  when ("MCA") cause="MCA";
  when ("SKI", "SNOWB", "SMOB", "SLED", "SNOWB") cause="SNOW";
  when ("ASLT") cause="ASLT";
  when ("BIKE") cause="BIKE";
  end;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;And then there are many cases where the two variables end up the same, and some where blanks are converted to underscores, no other change. You can put all of those into an OTHERWISE statement:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;select (cause_of_injury);
  when ("SKI", "SNOWB", "SMOB", "SLED", "SNOWB") cause="SNOW";
  otherwise cause=translate(trim(cause_of_injury),'_',' ');
  end;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;I know, this is not all of them, just those in your example. But it makes the code a lot simpler, nevertheless.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sat, 08 Aug 2020 13:48:42 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Characterizing-using-IF-THEN-STATEMENTS/m-p/675388#M203490</guid>
      <dc:creator>s_lassen</dc:creator>
      <dc:date>2020-08-08T13:48:42Z</dc:date>
    </item>
    <item>
      <title>Re: Characterizing using IF-THEN STATEMENTS</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Characterizing-using-IF-THEN-STATEMENTS/m-p/675402#M203499</link>
      <description>&lt;P&gt;If you import that look up table you can create a format from a data set as well using the CNTLIN option.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/191849"&gt;@Sascoder&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;I often use a mix of excel and sas to do something like this.&lt;/P&gt;
&lt;P&gt;For example, I generate the unique categories with sas (like with proc freq) then copy them to a column in excel, then in second column, I enter the new value.&amp;nbsp; Then in third column, I create the sas code statement, referring to the excel cells.&amp;nbsp; Excel is very easy to copy/paste cells so this is pretty efficient.&amp;nbsp; For example (and need to pay attention to the quotes so the text is properly quoted in the code):&lt;/P&gt;
&lt;P&gt;&amp;nbsp;="if cause_of_injury in (' " &amp;amp; a1 &amp;amp; " ') then cause=' " &amp;amp; b1 " ';' "&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Then copy all rows back to SAS code.&amp;nbsp; You can even store in a separate .sas file and use the %include statement which helps keep code cleaner.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Sometimes I create a giant proc format statement with this same method, and then use PUT statement to assign the new variable.&lt;/P&gt;
&lt;P&gt;For example, in excel:&lt;/P&gt;
&lt;P&gt;=" ' " &amp;amp; a1 &amp;amp; " ' = ' "&amp;nbsp; &amp;amp; b1&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Then in the sas code:&lt;BR /&gt;proc format;&lt;/P&gt;
&lt;P&gt;value $causefmt;&lt;/P&gt;
&lt;P&gt;[copy all the lines from excel]&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;data tmp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp; set data;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;cause=put(cause_of_injury,$causefmt.);&lt;/P&gt;
&lt;P&gt;run;&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;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sat, 08 Aug 2020 16:14:58 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Characterizing-using-IF-THEN-STATEMENTS/m-p/675402#M203499</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2020-08-08T16:14:58Z</dc:date>
    </item>
  </channel>
</rss>

