<?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: Create new variable with a combination of categories in SAS Procedures</title>
    <link>https://communities.sas.com/t5/SAS-Procedures/Create-new-variable-with-a-combination-of-categories/m-p/418586#M67758</link>
    <description>&lt;P&gt;Starting point:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
input TR1:$1. TR2:$1. TR3:$1.;
cards;
P P P
P N N
N N N
N P N 
M M M
I I I 
D D D
D I M
;
run;

data want;
	set have;
	trall = 'M';
	if (tr1='P' or tr2='P' or tr3='P') then trall = 'P';
	if (tr1='N' or tr2='N' or tr3='N') and (tr1 ne 'P' and tr2 ne 'P' and tr3 ne 'P')  then trall = 'N';
	if (tr1='I' and tr2='I' and tr3='I') then trall = 'I';
	if (tr1='D' and tr2='D' and tr3='D') then trall = 'D';
run;

&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Yields&lt;/P&gt;
&lt;TABLE width="195"&gt;
&lt;TBODY&gt;
&lt;TR&gt;
&lt;TD width="105"&gt;The SAS System&lt;/TD&gt;
&lt;TD width="29"&gt;&amp;nbsp;&lt;/TD&gt;
&lt;TD width="29"&gt;&amp;nbsp;&lt;/TD&gt;
&lt;TD width="32"&gt;&amp;nbsp;&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD&gt;&amp;nbsp;&lt;/TD&gt;
&lt;TD&gt;&amp;nbsp;&lt;/TD&gt;
&lt;TD&gt;&amp;nbsp;&lt;/TD&gt;
&lt;TD&gt;&amp;nbsp;&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD width="105"&gt;TR1&lt;/TD&gt;
&lt;TD width="29"&gt;TR2&lt;/TD&gt;
&lt;TD width="29"&gt;TR3&lt;/TD&gt;
&lt;TD width="32"&gt;trall&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD width="105"&gt;P&lt;/TD&gt;
&lt;TD width="29"&gt;P&lt;/TD&gt;
&lt;TD width="29"&gt;P&lt;/TD&gt;
&lt;TD width="32"&gt;P&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD width="105"&gt;P&lt;/TD&gt;
&lt;TD width="29"&gt;N&lt;/TD&gt;
&lt;TD width="29"&gt;N&lt;/TD&gt;
&lt;TD width="32"&gt;P&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD width="105"&gt;N&lt;/TD&gt;
&lt;TD width="29"&gt;N&lt;/TD&gt;
&lt;TD width="29"&gt;N&lt;/TD&gt;
&lt;TD width="32"&gt;N&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD width="105"&gt;N&lt;/TD&gt;
&lt;TD width="29"&gt;P&lt;/TD&gt;
&lt;TD width="29"&gt;N&lt;/TD&gt;
&lt;TD width="32"&gt;P&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD width="105"&gt;M&lt;/TD&gt;
&lt;TD width="29"&gt;M&lt;/TD&gt;
&lt;TD width="29"&gt;M&lt;/TD&gt;
&lt;TD width="32"&gt;M&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD width="105"&gt;I&lt;/TD&gt;
&lt;TD width="29"&gt;I&lt;/TD&gt;
&lt;TD width="29"&gt;I&lt;/TD&gt;
&lt;TD width="32"&gt;I&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD width="105"&gt;D&lt;/TD&gt;
&lt;TD width="29"&gt;D&lt;/TD&gt;
&lt;TD width="29"&gt;D&lt;/TD&gt;
&lt;TD width="32"&gt;D&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD width="105"&gt;D&lt;/TD&gt;
&lt;TD width="29"&gt;I&lt;/TD&gt;
&lt;TD width="29"&gt;M&lt;/TD&gt;
&lt;TD width="32"&gt;M&lt;/TD&gt;
&lt;/TR&gt;
&lt;/TBODY&gt;
&lt;/TABLE&gt;</description>
    <pubDate>Tue, 05 Dec 2017 19:35:01 GMT</pubDate>
    <dc:creator>HB</dc:creator>
    <dc:date>2017-12-05T19:35:01Z</dc:date>
    <item>
      <title>Create new variable with a combination of categories</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Create-new-variable-with-a-combination-of-categories/m-p/418573#M67756</link>
      <description>&lt;P&gt;I'm creating a new variable test_result_all based on test_result_1, test_result_2, and test_result_3. Each variable contains 4 categories: positive, negative, indeterminate, and Not Done.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;For any positive result in the 3 variables, test_result_all='positive'. For any test result with negative but not positive, the result is&amp;nbsp;negative. For all results with Not Done=Not Done. For all results with indeterminate=indeterminate. Otherwise (mixed between indeterminate and Not Done, or missing), it's missing.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;How should I code that? I was thinking of doing an if-then but it seems complicated to do it.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I started the following but couldn't figure out what's next:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;if test_result_1='P' or test_result_2='P' or test_result_3='P' then test_result_all='P';&lt;/P&gt;&lt;P&gt;else if...&lt;/P&gt;</description>
      <pubDate>Tue, 05 Dec 2017 18:56:51 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Create-new-variable-with-a-combination-of-categories/m-p/418573#M67756</guid>
      <dc:creator>jcapua2</dc:creator>
      <dc:date>2017-12-05T18:56:51Z</dc:date>
    </item>
    <item>
      <title>Re: Create new variable with a combination of categories</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Create-new-variable-with-a-combination-of-categories/m-p/418583#M67757</link>
      <description>&lt;P&gt;Hi, May i request you to please post your question with a sample HAVE dataset and a sample WANT dataset. I am apologetically too lazy to write it down. I prefer copy/paste. Thank you!&lt;/P&gt;</description>
      <pubDate>Tue, 05 Dec 2017 19:27:16 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Create-new-variable-with-a-combination-of-categories/m-p/418583#M67757</guid>
      <dc:creator>novinosrin</dc:creator>
      <dc:date>2017-12-05T19:27:16Z</dc:date>
    </item>
    <item>
      <title>Re: Create new variable with a combination of categories</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Create-new-variable-with-a-combination-of-categories/m-p/418586#M67758</link>
      <description>&lt;P&gt;Starting point:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
input TR1:$1. TR2:$1. TR3:$1.;
cards;
P P P
P N N
N N N
N P N 
M M M
I I I 
D D D
D I M
;
run;

data want;
	set have;
	trall = 'M';
	if (tr1='P' or tr2='P' or tr3='P') then trall = 'P';
	if (tr1='N' or tr2='N' or tr3='N') and (tr1 ne 'P' and tr2 ne 'P' and tr3 ne 'P')  then trall = 'N';
	if (tr1='I' and tr2='I' and tr3='I') then trall = 'I';
	if (tr1='D' and tr2='D' and tr3='D') then trall = 'D';
run;

&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Yields&lt;/P&gt;
&lt;TABLE width="195"&gt;
&lt;TBODY&gt;
&lt;TR&gt;
&lt;TD width="105"&gt;The SAS System&lt;/TD&gt;
&lt;TD width="29"&gt;&amp;nbsp;&lt;/TD&gt;
&lt;TD width="29"&gt;&amp;nbsp;&lt;/TD&gt;
&lt;TD width="32"&gt;&amp;nbsp;&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD&gt;&amp;nbsp;&lt;/TD&gt;
&lt;TD&gt;&amp;nbsp;&lt;/TD&gt;
&lt;TD&gt;&amp;nbsp;&lt;/TD&gt;
&lt;TD&gt;&amp;nbsp;&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD width="105"&gt;TR1&lt;/TD&gt;
&lt;TD width="29"&gt;TR2&lt;/TD&gt;
&lt;TD width="29"&gt;TR3&lt;/TD&gt;
&lt;TD width="32"&gt;trall&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD width="105"&gt;P&lt;/TD&gt;
&lt;TD width="29"&gt;P&lt;/TD&gt;
&lt;TD width="29"&gt;P&lt;/TD&gt;
&lt;TD width="32"&gt;P&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD width="105"&gt;P&lt;/TD&gt;
&lt;TD width="29"&gt;N&lt;/TD&gt;
&lt;TD width="29"&gt;N&lt;/TD&gt;
&lt;TD width="32"&gt;P&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD width="105"&gt;N&lt;/TD&gt;
&lt;TD width="29"&gt;N&lt;/TD&gt;
&lt;TD width="29"&gt;N&lt;/TD&gt;
&lt;TD width="32"&gt;N&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD width="105"&gt;N&lt;/TD&gt;
&lt;TD width="29"&gt;P&lt;/TD&gt;
&lt;TD width="29"&gt;N&lt;/TD&gt;
&lt;TD width="32"&gt;P&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD width="105"&gt;M&lt;/TD&gt;
&lt;TD width="29"&gt;M&lt;/TD&gt;
&lt;TD width="29"&gt;M&lt;/TD&gt;
&lt;TD width="32"&gt;M&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD width="105"&gt;I&lt;/TD&gt;
&lt;TD width="29"&gt;I&lt;/TD&gt;
&lt;TD width="29"&gt;I&lt;/TD&gt;
&lt;TD width="32"&gt;I&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD width="105"&gt;D&lt;/TD&gt;
&lt;TD width="29"&gt;D&lt;/TD&gt;
&lt;TD width="29"&gt;D&lt;/TD&gt;
&lt;TD width="32"&gt;D&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD width="105"&gt;D&lt;/TD&gt;
&lt;TD width="29"&gt;I&lt;/TD&gt;
&lt;TD width="29"&gt;M&lt;/TD&gt;
&lt;TD width="32"&gt;M&lt;/TD&gt;
&lt;/TR&gt;
&lt;/TBODY&gt;
&lt;/TABLE&gt;</description>
      <pubDate>Tue, 05 Dec 2017 19:35:01 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Create-new-variable-with-a-combination-of-categories/m-p/418586#M67758</guid>
      <dc:creator>HB</dc:creator>
      <dc:date>2017-12-05T19:35:01Z</dc:date>
    </item>
    <item>
      <title>Re: Create new variable with a combination of categories</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Create-new-variable-with-a-combination-of-categories/m-p/418591#M67759</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/176467"&gt;@jcapua2&lt;/a&gt; wrote:&lt;BR /&gt;
&lt;P&gt;I'm creating a new variable test_result_all based on test_result_1, test_result_2, and test_result_3. Each variable contains 4 categories: positive, negative, indeterminate, and Not Done.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;For any positive result in the 3 variables, test_result_all='positive'. For any test result with negative but not positive, the result is&amp;nbsp;negative. For all results with Not Done=Not Done. For all results with indeterminate=indeterminate. Otherwise (mixed between indeterminate and Not Done, or missing), it's missing.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;How should I code that? I was thinking of doing an if-then but it seems complicated to do it.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I started the following but couldn't figure out what's next:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;if test_result_1='P' or test_result_2='P' or test_result_3='P' then test_result_all='P';&lt;/P&gt;
&lt;P&gt;else if...&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;You say 4 categories: &amp;nbsp;positive, negative, indeterminate, and Not Done but then reference missing. That makes 5 categories.&lt;/P&gt;
&lt;P&gt;You also say those values but then post code testing for a value of 'P'. So which are the actual values of the categories?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Else if should be similar to the test for 'P' but use the value for negative.&lt;/P&gt;
&lt;P&gt;You would then have further "else if".&lt;/P&gt;
&lt;P&gt;If I understand "For all results with Not Done=Not Done. For all results with indeterminate=indeterminate" then instead of 'or' the requirement would be to use 'and' between all of the tests similar to your example.&lt;/P&gt;
&lt;P&gt;If none of the above is true then the result will be missing. No need to create an explicit assignment but it would look like:&lt;/P&gt;
&lt;P&gt;else test_result_all='';&lt;/P&gt;
&lt;P&gt;or&lt;/P&gt;
&lt;P&gt;else call missing(test_result_all);&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 05 Dec 2017 19:59:19 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Create-new-variable-with-a-combination-of-categories/m-p/418591#M67759</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2017-12-05T19:59:19Z</dc:date>
    </item>
    <item>
      <title>Re: Create new variable with a combination of categories</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Create-new-variable-with-a-combination-of-categories/m-p/418599#M67761</link>
      <description>&lt;P&gt;Why do you test test_result_1='P'&amp;nbsp;if each variable contains 4 categories: positive, negative, indeterminate, and Not Done? It can never be true.&lt;/P&gt;</description>
      <pubDate>Tue, 05 Dec 2017 20:36:09 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Create-new-variable-with-a-combination-of-categories/m-p/418599#M67761</guid>
      <dc:creator>PGStats</dc:creator>
      <dc:date>2017-12-05T20:36:09Z</dc:date>
    </item>
    <item>
      <title>Re: Create new variable with a combination of categories</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Create-new-variable-with-a-combination-of-categories/m-p/418622#M67765</link>
      <description>&lt;P&gt;Let's assume your codes are single letters code, with letters&amp;nbsp;'P' (for positive), 'N' (negative), 'D' (not done), or 'I' (indeterminate).&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;And you want:&lt;/P&gt;
&lt;OL&gt;
&lt;LI&gt;If there is at least 1 'P' the overall result is 'P'&lt;/LI&gt;
&lt;LI&gt;otherwise if there is at least 1 'N' --&amp;gt; 'N'&lt;/LI&gt;
&lt;LI&gt;otherwise if you have three 'D's or three 'I's then the overall result is correspondingly a 'D' or 'I'&lt;/LI&gt;
&lt;LI&gt;otherwise missing&lt;/LI&gt;
&lt;/OL&gt;
&lt;P&gt;If that's correct, then collapse your three codes into a single 3-letter code&amp;nbsp; as in&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;EM&gt;&lt;STRONG&gt;three_letters=cats(test_result1,test_result2,test_result3);&lt;/STRONG&gt;&lt;/EM&gt;&lt;/P&gt;
&lt;P&gt;Then you can use the index function to good effect.&amp;nbsp; For example&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp; &lt;EM&gt;&lt;STRONG&gt;if index(three_letters,'P')&amp;gt;0 then test_result_all='P';&lt;/STRONG&gt;&lt;/EM&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have (drop=i j k);
  array tr {3} $1 test_result1-test_result3;
  do i='P','N','D','I'; tr{1}=i;
    do j='P','N','D','I'; tr{2}=j;
      do k='P','N','D','I'; tr{3}=k;
	    output;
	  end;
	end;
  end;
run;

data want;
  set have;
  three_letters=cats(test_result1,test_result2,test_result3);

  if index(three_letters,'P')&amp;gt;0 then test_result_all='P'; else
  if (some condition) then test_result_all='N'; else
  if (some condition) then test_result_all='D'; else 
  if (some condition) then test_result_all='I';
run;&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;Print out the resulting list of &lt;EM&gt;&lt;STRONG&gt;three_letters&lt;/STRONG&gt;&lt;/EM&gt; and &lt;EM&gt;&lt;STRONG&gt;test_result_all&lt;/STRONG&gt;&lt;/EM&gt; to see if it does what you want.&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;&amp;nbsp;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 05 Dec 2017 22:14:15 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Create-new-variable-with-a-combination-of-categories/m-p/418622#M67765</guid>
      <dc:creator>mkeintz</dc:creator>
      <dc:date>2017-12-05T22:14:15Z</dc:date>
    </item>
    <item>
      <title>Re: Create new variable with a combination of categories</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Create-new-variable-with-a-combination-of-categories/m-p/419233#M67792</link>
      <description>&lt;P&gt;It worked! Thank you very much!&lt;/P&gt;</description>
      <pubDate>Thu, 07 Dec 2017 16:12:58 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Create-new-variable-with-a-combination-of-categories/m-p/419233#M67792</guid>
      <dc:creator>jcapua2</dc:creator>
      <dc:date>2017-12-07T16:12:58Z</dc:date>
    </item>
  </channel>
</rss>

