<?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: I need help creating a new categorical variable in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/I-need-help-creating-a-new-categorical-variable/m-p/709059#M217946</link>
    <description>&lt;P&gt;For your first two sets of conditions, this would also work:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;
if VarA = VarB = 1 then VarC = "Both";
else if VarA = VarB = 2 then VarC = "None";&lt;/CODE&gt;&lt;/PRE&gt;</description>
    <pubDate>Sat, 02 Jan 2021 09:11:55 GMT</pubDate>
    <dc:creator>Astounding</dc:creator>
    <dc:date>2021-01-02T09:11:55Z</dc:date>
    <item>
      <title>I need help creating a new categorical variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/I-need-help-creating-a-new-categorical-variable/m-p/709045#M217941</link>
      <description>&lt;DIV&gt;&lt;DIV class=""&gt;&lt;DIV class="public-DraftStyleDefault-block public-DraftStyleDefault-ltr"&gt;&lt;SPAN&gt;I've been trying to do this for the past hour. I'm trying to create a variable with 4 levels/categories from two categorical variables with two levels each.&lt;/SPAN&gt;&lt;/DIV&gt;&lt;DIV class="public-DraftStyleDefault-block public-DraftStyleDefault-ltr"&gt;&amp;nbsp;&lt;/DIV&gt;&lt;/DIV&gt;&lt;DIV class=""&gt;&lt;DIV class="public-DraftStyleDefault-block public-DraftStyleDefault-ltr"&gt;&lt;SPAN&gt;Let's call the original variables "VarA" and "VarB" and the new variable "VarC"&lt;/SPAN&gt;&lt;/DIV&gt;&lt;/DIV&gt;&lt;DIV class=""&gt;&lt;DIV class="public-DraftStyleDefault-block public-DraftStyleDefault-ltr"&gt;&lt;SPAN&gt;- VarA and VarB have two categories: 1 and 2.&lt;/SPAN&gt;&lt;/DIV&gt;&lt;DIV class="public-DraftStyleDefault-block public-DraftStyleDefault-ltr"&gt;&amp;nbsp;&lt;/DIV&gt;&lt;/DIV&gt;&lt;DIV class=""&gt;&lt;DIV class="public-DraftStyleDefault-block public-DraftStyleDefault-ltr"&gt;&lt;SPAN&gt;I'm trying to satisfy the following criteria for VarC:&lt;/SPAN&gt;&lt;/DIV&gt;&lt;DIV class="public-DraftStyleDefault-block public-DraftStyleDefault-ltr"&gt;&amp;nbsp;&lt;/DIV&gt;&lt;/DIV&gt;&lt;DIV class=""&gt;&lt;DIV class="public-DraftStyleDefault-block public-DraftStyleDefault-ltr"&gt;&lt;SPAN&gt;- If VarA and VarB = 1 then VarC = "Both"&lt;/SPAN&gt;&lt;/DIV&gt;&lt;/DIV&gt;&lt;DIV class=""&gt;&lt;DIV class="public-DraftStyleDefault-block public-DraftStyleDefault-ltr"&gt;&lt;SPAN&gt;- If VarA and VarB = 2 then VarC = "None"&lt;/SPAN&gt;&lt;/DIV&gt;&lt;/DIV&gt;&lt;DIV class=""&gt;&lt;DIV class="public-DraftStyleDefault-block public-DraftStyleDefault-ltr"&gt;&lt;SPAN&gt;- If VarA = 1 and VarB = 2 then VarC = "VarA"&lt;/SPAN&gt;&lt;/DIV&gt;&lt;/DIV&gt;&lt;DIV class=""&gt;&lt;DIV class="public-DraftStyleDefault-block public-DraftStyleDefault-ltr"&gt;&lt;SPAN&gt;- If Var B = 1 and VarA = 2 then VarC = "VarB"&lt;/SPAN&gt;&lt;/DIV&gt;&lt;DIV class="public-DraftStyleDefault-block public-DraftStyleDefault-ltr"&gt;&amp;nbsp;&lt;/DIV&gt;&lt;/DIV&gt;&lt;DIV class=""&gt;&lt;DIV class="public-DraftStyleDefault-block public-DraftStyleDefault-ltr"&gt;&lt;SPAN&gt;Can someone help me out with the right "if then else" statements or the best way to so this?&lt;/SPAN&gt;&lt;/DIV&gt;&lt;/DIV&gt;&lt;DIV class=""&gt;&lt;DIV class="public-DraftStyleDefault-block public-DraftStyleDefault-ltr"&gt;&lt;SPAN&gt;&amp;nbsp;&lt;/SPAN&gt;&lt;/DIV&gt;&lt;/DIV&gt;&lt;DIV class=""&gt;&lt;DIV class="public-DraftStyleDefault-block public-DraftStyleDefault-ltr"&gt;&lt;SPAN&gt;Thanks!&lt;/SPAN&gt;&lt;/DIV&gt;&lt;/DIV&gt;&lt;/DIV&gt;</description>
      <pubDate>Sat, 02 Jan 2021 05:52:27 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/I-need-help-creating-a-new-categorical-variable/m-p/709045#M217941</guid>
      <dc:creator>ajames2020</dc:creator>
      <dc:date>2021-01-02T05:52:27Z</dc:date>
    </item>
    <item>
      <title>Re: I need help creating a new categorical variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/I-need-help-creating-a-new-categorical-variable/m-p/709048#M217942</link>
      <description>&lt;P&gt;Just convert your logic into SAS statements.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
  set have;
  length varc $4 ;
  if VarA=1 and VarB=1 then VarC = "Both";
  else if VarA=2 and VarB=2 then VarC = "None";
  else if VarA=1 and VarB=2 then VarC = "VarA";
  else if VarA=2 and VarB=1 then VarC = "VarB" ;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;If you just put AND between two variables then SAS&amp;nbsp; just test if the value of the variable is true or false.&amp;nbsp; SAS will treat any values of VARA that are zero or missing as FALSE and any other value as TRUE.&amp;nbsp; So your first two conditions as you wrote them were just testing the value of VARB since either 1 or 2 in VARA would be considered TRUE.&lt;/P&gt;</description>
      <pubDate>Sat, 02 Jan 2021 06:27:44 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/I-need-help-creating-a-new-categorical-variable/m-p/709048#M217942</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2021-01-02T06:27:44Z</dc:date>
    </item>
    <item>
      <title>Re: I need help creating a new categorical variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/I-need-help-creating-a-new-categorical-variable/m-p/709059#M217946</link>
      <description>&lt;P&gt;For your first two sets of conditions, this would also work:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;
if VarA = VarB = 1 then VarC = "Both";
else if VarA = VarB = 2 then VarC = "None";&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Sat, 02 Jan 2021 09:11:55 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/I-need-help-creating-a-new-categorical-variable/m-p/709059#M217946</guid>
      <dc:creator>Astounding</dc:creator>
      <dc:date>2021-01-02T09:11:55Z</dc:date>
    </item>
    <item>
      <title>Re: I need help creating a new categorical variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/I-need-help-creating-a-new-categorical-variable/m-p/709069#M217953</link>
      <description>&lt;P&gt;Another approach (untested)&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc format;
  value $C
    "11"="Both"
    "22"="None"
    "12"="VarA"
    "21"="VarB"
    ;
run;
data want;
  set have;
  length varc $2 ;
	VarC=cats(put(VarA,$1.),put(VarB,$1.));
	format VarC $C.;
/*  
  if VarA=1 and VarB=1 then VarC = "Both";
  else if VarA=2 and VarB=2 then VarC = "None";
  else if VarA=1 and VarB=2 then VarC = "VarA";
  else if VarA=2 and VarB=1 then VarC = "VarB" ;
  */
run;
&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Sat, 02 Jan 2021 16:17:08 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/I-need-help-creating-a-new-categorical-variable/m-p/709069#M217953</guid>
      <dc:creator>ghosh</dc:creator>
      <dc:date>2021-01-02T16:17:08Z</dc:date>
    </item>
    <item>
      <title>Re: I need help creating a new categorical variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/I-need-help-creating-a-new-categorical-variable/m-p/709102#M217968</link>
      <description>&lt;P&gt;Interesting, thank you.&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sat, 02 Jan 2021 21:42:30 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/I-need-help-creating-a-new-categorical-variable/m-p/709102#M217968</guid>
      <dc:creator>ajames2020</dc:creator>
      <dc:date>2021-01-02T21:42:30Z</dc:date>
    </item>
    <item>
      <title>Re: I need help creating a new categorical variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/I-need-help-creating-a-new-categorical-variable/m-p/709103#M217969</link>
      <description>&lt;P&gt;Thank you.&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sat, 02 Jan 2021 21:43:55 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/I-need-help-creating-a-new-categorical-variable/m-p/709103#M217969</guid>
      <dc:creator>ajames2020</dc:creator>
      <dc:date>2021-01-02T21:43:55Z</dc:date>
    </item>
    <item>
      <title>Re: I need help creating a new categorical variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/I-need-help-creating-a-new-categorical-variable/m-p/709144#M218006</link>
      <description>&lt;P&gt;To elaborate a little on the solution by&amp;nbsp;@Tom:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;Data want;
  set have;
  if VarA=1 then do;
    if VarB=1 then VarC='Both';
    else VarC='VarA';
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;end;
&amp;nbsp;&amp;nbsp;else&amp;nbsp;if&amp;nbsp;VarB=1&amp;nbsp;then
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;VarC='VarB';
  else
    VarC='None';
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;This runs slightly faster, as you do not repeatedly test the same condition. The results can be different if you have other categories than 1 and 2 (everything but 1 is considered "not-one" and treated like a 2), &lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/159"&gt;@Tom&lt;/a&gt;&amp;nbsp;'s solution will leave such exceptions blank.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If you want to explicitly test for such category errors and flag them, it can be done like this:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
  set have;
  select(catx(VarA,VarB));
    when('11') VarC='Both';
    when('12') VarC='VarA';
    when('21') VarC='VarB';
    when('11') VarC='None';
    otherwise VarC='Err';
    end;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;If you leave out the OTHERWISE statement, the program will fail (log error) if there are other values than 1 or 2. Which may be what you want in that case.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sun, 03 Jan 2021 13:26:28 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/I-need-help-creating-a-new-categorical-variable/m-p/709144#M218006</guid>
      <dc:creator>s_lassen</dc:creator>
      <dc:date>2021-01-03T13:26:28Z</dc:date>
    </item>
  </channel>
</rss>

