<?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: need help creating a binary variable from two categorical variables with if-else and wildcards in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/need-help-creating-a-binary-variable-from-two-categorical/m-p/249513#M46948</link>
    <description>&lt;P&gt;Ah, thank you, I overlooked that when re-typing it.&lt;/P&gt;</description>
    <pubDate>Thu, 11 Feb 2016 18:55:01 GMT</pubDate>
    <dc:creator>kjamesm</dc:creator>
    <dc:date>2016-02-11T18:55:01Z</dc:date>
    <item>
      <title>need help creating a binary variable from two categorical variables with if-else and wildcards</title>
      <link>https://communities.sas.com/t5/SAS-Programming/need-help-creating-a-binary-variable-from-two-categorical/m-p/249495#M46938</link>
      <description>&lt;P&gt;Hi all,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I'm trying to create a binary (dummy) variable with information from two categorical variables, but I'm having trouble.&amp;nbsp; I need to use wildcards because var1 has multiple variables that start with the same letters, each of which match var2.&amp;nbsp; If I need to I will type them all out completely but I feel sure there is a way to do this (and the code for this one bit is already over 100 lines).&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data sample; 
 format var1 $11. var2 $4.; 
 input var1 $ var2 $; 
 datalines;
aa1 aaa
aa2 aaa
aa bbb
abdfgdfh bbb
abdfttyrty bbb
abwww bbb 
abc aaa
abd aaa
adrtrtrt dd
adyuyuii ddd
adxx bbb
adrr ccc
;
data sample;
set sample;
if (var1 = aa1 or aa2) and (var2 = aaa) then match=1;
else if (var1 = ab:) and (var2 = bbb) then match=1;
else if (var1 = ad:) and (var2 = ddd or dd) then match=1;
else match=0;
run;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;which gives me&lt;/P&gt;&lt;P&gt;ERROR 388-185: Expecting an arithmetic operator.&lt;/P&gt;&lt;P&gt;ERROR 200-322: The symbol is not recognized and will be ignored.&lt;/P&gt;&lt;P&gt;ERROR 76-322: Syntax error, statement will be ignored.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I've also tried it like this&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;if var1= 'aa1' or 'aa2' and var2 = 'aaa' then match=1;
else if var1 =: 'ab%' and var2 ='bbb' then match=1;
else if var1 =: 'ad%' and (var2 ='ddd' or 'dd') then match=1;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp; which gives me NOTE: Invalid numeric data, but creates the variable -- with only 1 one, the first observation, and the rest all as zeros.&amp;nbsp;&lt;BR /&gt;&lt;BR /&gt;&lt;/P&gt;&lt;P&gt;and like this&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;if (var1= aa1 or aa2) and var2 = aaa then match=1;
else if var1 =: ab% and var2 =bbb then match=1;
else if var1 =: ad% and (var2 =ddd or dd) then match=1;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;which gives me&lt;/P&gt;&lt;P&gt;ERROR 388-185: Expecting an arithmetic operator.&lt;/P&gt;&lt;P&gt;ERROR 200-322: The symbol is not recognized and will be ignored.&lt;/P&gt;&lt;P&gt;ERROR 76-322: Syntax error, statement will be ignored.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Anyone have a clue as to my mistake?&lt;/P&gt;&lt;P&gt;I'm using SAS 9.2 on Windows 7.&lt;BR /&gt;&lt;BR /&gt;&lt;/P&gt;&lt;P&gt;EDIT: after reading your comments (thank you!) I have working code:&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;if var1 in ("aa1" "aa2") and var2 = "aaa" then match=1;
else if var1 =: "ab" and var2 ="bbb" then match=1;
else if var1 =: "ad" and var2 in ("ddd" "dd") then match=1;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 11 Feb 2016 18:57:29 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/need-help-creating-a-binary-variable-from-two-categorical/m-p/249495#M46938</guid>
      <dc:creator>kjamesm</dc:creator>
      <dc:date>2016-02-11T18:57:29Z</dc:date>
    </item>
    <item>
      <title>Re: need help creating a binary variable from two categorical variables with if-else and wildcards</title>
      <link>https://communities.sas.com/t5/SAS-Programming/need-help-creating-a-binary-variable-from-two-categorical/m-p/249497#M46939</link>
      <description>&lt;PRE class=" language-sas"&gt;&lt;CODE class="  language-sas"&gt;&lt;SPAN class="token keyword"&gt;if&lt;/SPAN&gt; var1 &lt;SPAN class="token operator"&gt;in ("&lt;/SPAN&gt;aa1" "aa2"&lt;SPAN class="token punctuation"&gt;)&lt;/SPAN&gt; and &lt;SPAN class="token punctuation"&gt;(&lt;/SPAN&gt;var2 &lt;SPAN class="token operator"&gt;=&lt;/SPAN&gt; "aaa"&lt;SPAN class="token punctuation"&gt;)&lt;/SPAN&gt; &lt;SPAN class="token keyword"&gt;then&lt;/SPAN&gt; match&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;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;For starters here's one correction. Some similar ones need to be carried through as well.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If you're comparing to a text value you need to include quotes and if checking for multiple variables use IN ()&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Another change, the colon goes with the = sign, not the&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;(var1 =: "ab") and (var2 = "bbb")&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Thu, 11 Feb 2016 17:50:40 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/need-help-creating-a-binary-variable-from-two-categorical/m-p/249497#M46939</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2016-02-11T17:50:40Z</dc:date>
    </item>
    <item>
      <title>Re: need help creating a binary variable from two categorical variables with if-else and wildcards</title>
      <link>https://communities.sas.com/t5/SAS-Programming/need-help-creating-a-binary-variable-from-two-categorical/m-p/249498#M46940</link>
      <description>&lt;P&gt;First issue:&lt;/P&gt;
&lt;P&gt;&lt;SPAN class="token keyword"&gt;if&lt;/SPAN&gt; &lt;SPAN class="token punctuation"&gt;(&lt;/SPAN&gt;var1 &lt;SPAN class="token operator"&gt;=&lt;/SPAN&gt; aa1 or aa2&lt;SPAN class="token punctuation"&gt;)&lt;/SPAN&gt; is using aa1 and aa2 as VARIABLES, which do not exist&lt;/P&gt;
&lt;P&gt;Second you want to reference the Values of concern, text literals require quotes to tell SAS you are looking for specific strings&lt;/P&gt;
&lt;P&gt;If you want to see if var1 has a value of either aa1 or aa2 here are two ways:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;if (var1 = 'aa1' or var1='aa2')&lt;/P&gt;
&lt;P&gt;or&lt;/P&gt;
&lt;P&gt;if var1 in ('aa1' 'aa2').&lt;/P&gt;
&lt;P&gt;The value vs variable has to be addressed in all of your code.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;When you use the ab: construct it is looking for VARIABLES that start with ap, not values. You would use var1 =: 'ab' to look for strings starting with 'ab'.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;In future posts with errors please post the log including the procedure or datastep. There are things that tell us which line and likely specific causes that cannot be determined by just posting the error.&lt;/P&gt;</description>
      <pubDate>Thu, 11 Feb 2016 17:51:17 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/need-help-creating-a-binary-variable-from-two-categorical/m-p/249498#M46940</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2016-02-11T17:51:17Z</dc:date>
    </item>
    <item>
      <title>Re: need help creating a binary variable from two categorical variables with if-else and wildcards</title>
      <link>https://communities.sas.com/t5/SAS-Programming/need-help-creating-a-binary-variable-from-two-categorical/m-p/249502#M46943</link>
      <description>&lt;P&gt;Thank you, this was helpful.&amp;nbsp; I am no longer getting an error code, but it is not catching all of the matches. Do you see a problem in the following code?&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;if var1 in ("aa1" "aa2") and var2 = "aaa" then match=1;
if var1 =: "ab" and var2 ="bbb" then match=1;
if var1 =: "ad" and var2 in ("ddd" "dd") then match=1;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Thu, 11 Feb 2016 18:10:54 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/need-help-creating-a-binary-variable-from-two-categorical/m-p/249502#M46943</guid>
      <dc:creator>kjamesm</dc:creator>
      <dc:date>2016-02-11T18:10:54Z</dc:date>
    </item>
    <item>
      <title>Re: need help creating a binary variable from two categorical variables with if-else and wildcards</title>
      <link>https://communities.sas.com/t5/SAS-Programming/need-help-creating-a-binary-variable-from-two-categorical/m-p/249506#M46944</link>
      <description>&lt;P&gt;There's no problem with that code.&amp;nbsp; The problem is what you added after that code:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;else match=0;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;That ELSE applies only to the last statement before it.&amp;nbsp; If you want to link all the statements together, you have to add "else" a few more times:&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 keyword"&gt;if&lt;/SPAN&gt; var1 &lt;SPAN class="token operator"&gt;in&lt;/SPAN&gt; &lt;SPAN class="token punctuation"&gt;(&lt;/SPAN&gt;&lt;SPAN class="token string"&gt;"aa1"&lt;/SPAN&gt; &lt;SPAN class="token string"&gt;"aa2"&lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;)&lt;/SPAN&gt; and var2 &lt;SPAN class="token operator"&gt;=&lt;/SPAN&gt; &lt;SPAN class="token string"&gt;"aaa"&lt;/SPAN&gt; &lt;SPAN class="token keyword"&gt;then&lt;/SPAN&gt; match&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;
else &lt;SPAN class="token keyword"&gt;if&lt;/SPAN&gt; var1 &lt;SPAN class="token operator"&gt;=&lt;/SPAN&gt;: &lt;SPAN class="token string"&gt;"ab"&lt;/SPAN&gt; and var2 &lt;SPAN class="token operator"&gt;=&lt;/SPAN&gt;&lt;SPAN class="token string"&gt;"bbb"&lt;/SPAN&gt; &lt;SPAN class="token keyword"&gt;then&lt;/SPAN&gt; match&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;
else &lt;SPAN class="token keyword"&gt;if&lt;/SPAN&gt; var1 &lt;SPAN class="token operator"&gt;=&lt;/SPAN&gt;: &lt;SPAN class="token string"&gt;"ad"&lt;/SPAN&gt; and var2 &lt;SPAN class="token operator"&gt;in&lt;/SPAN&gt; &lt;SPAN class="token punctuation"&gt;(&lt;/SPAN&gt;&lt;SPAN class="token string"&gt;"ddd"&lt;/SPAN&gt; &lt;SPAN class="token string"&gt;"dd"&lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;)&lt;/SPAN&gt; &lt;SPAN class="token keyword"&gt;then&lt;/SPAN&gt; match&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;BR /&gt;&lt;BR /&gt;else match=0;&lt;BR /&gt;&lt;/SPAN&gt;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;When&amp;nbsp; you remember those who helped you, please mark the other poster's answer as correct.&amp;nbsp; He did most of the work and gave you the right tools to use.&lt;/P&gt;</description>
      <pubDate>Thu, 11 Feb 2016 18:18:08 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/need-help-creating-a-binary-variable-from-two-categorical/m-p/249506#M46944</guid>
      <dc:creator>Astounding</dc:creator>
      <dc:date>2016-02-11T18:18:08Z</dc:date>
    </item>
    <item>
      <title>Re: need help creating a binary variable from two categorical variables with if-else and wildcards</title>
      <link>https://communities.sas.com/t5/SAS-Programming/need-help-creating-a-binary-variable-from-two-categorical/m-p/249512#M46947</link>
      <description>&lt;P&gt;Thank you, I did not realize that I did not need the % when using =:&lt;/P&gt;</description>
      <pubDate>Thu, 11 Feb 2016 18:51:18 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/need-help-creating-a-binary-variable-from-two-categorical/m-p/249512#M46947</guid>
      <dc:creator>kjamesm</dc:creator>
      <dc:date>2016-02-11T18:51:18Z</dc:date>
    </item>
    <item>
      <title>Re: need help creating a binary variable from two categorical variables with if-else and wildcards</title>
      <link>https://communities.sas.com/t5/SAS-Programming/need-help-creating-a-binary-variable-from-two-categorical/m-p/249513#M46948</link>
      <description>&lt;P&gt;Ah, thank you, I overlooked that when re-typing it.&lt;/P&gt;</description>
      <pubDate>Thu, 11 Feb 2016 18:55:01 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/need-help-creating-a-binary-variable-from-two-categorical/m-p/249513#M46948</guid>
      <dc:creator>kjamesm</dc:creator>
      <dc:date>2016-02-11T18:55:01Z</dc:date>
    </item>
  </channel>
</rss>

