<?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: Creating a new variable based on repeated common values in two columns in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Creating-a-new-variable-based-on-repeated-common-values-in-two/m-p/778426#M247772</link>
    <description>&lt;P&gt;Below one way to go. I've created a key with a distinct value for any combination of drugs. I've used md5() to create this key as this allows to define the length of the variable without having to know the lengths of the variables where you store the drug names.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
  input Sno DrugA $ DrugB $;
datalines;
1 A1 A2
2 B2 B3
3 C3 C4
4 A2 A4
5 A5 A18
6 A7 A17
7 A9 A81
8 K7 L9
9 L9 L19
10 L8 L3
11 A2 A1
12 A18 A5
13 A81 A9
14 A1 A2
15 B2 B3
;

data want(drop=_:);
  set have;
  _DrugA=upcase(compress(DrugA));
  _DrugB=upcase(compress(DrugB));
  call sortc(_DrugA,_DrugB);
  hash_key=put(md5(catx('|',_DrugA,_DrugB)),hex32.);
run;

proc sql;
  select 
    sno,
    DrugA,
    DrugB,
    hash_key,
    count(*) as n_same_drugs
  from want
  group by hash_key
  order by sno
  ;
quit;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Patrick_0-1636007601910.png" style="width: 558px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/65398i6C2BADB198CEC05F/image-dimensions/558x421?v=v2" width="558" height="421" role="button" title="Patrick_0-1636007601910.png" alt="Patrick_0-1636007601910.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Thu, 04 Nov 2021 06:33:38 GMT</pubDate>
    <dc:creator>Patrick</dc:creator>
    <dc:date>2021-11-04T06:33:38Z</dc:date>
    <item>
      <title>Creating a new variable based on repeated common values in two columns</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Creating-a-new-variable-based-on-repeated-common-values-in-two/m-p/778411#M247766</link>
      <description>&lt;P&gt;Hello, I am working on a dataset that has 1000's of drugs.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I created a sample dataset and have attached it.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I am trying to create a variable that identifies if a combination of DrugA and DrugB is repeated in the attached dataset. For instance row 1, 11 and 14 have the same combination of drugs A1 and A2 (Also repeated combinations are seen in- row 7 and 13; row 5 and 12; 2 and 15).&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I know it can be done for two possible combinations (A1 &amp;amp; A2; B2 &amp;amp; B3) through:&lt;/P&gt;
&lt;PRE&gt;data test;
input Sno DrugA $ DrugB $;
datalines;
1 A1 A2
2 B2 B3
3 C3 C4
4 A2 A4
5 A5 A18
6 A7 A17
7 A9 A81
8 K7 L9
9 L9 L19
10 L8 L3
11 A2 A1
12 A18 A5
13 A81 A9
14 A1 A2
15 B2 B3;


data test1;
set test;
If DrugA= "A1" then C1=1;
else if DrugA="A2" then C1=1;
if DrugB=&amp;nbsp;"A1" then C1=1;
else if DrugB="A2" then C1=1;&lt;BR /&gt;If DrugA= "B2" then C1=2; &lt;BR /&gt;else if DrugA="B3" then C1=2; &lt;BR /&gt;if DrugB=&amp;nbsp;"B2" then C1=2; &lt;BR /&gt;else if DrugB="B3" then C1=2;
run;&lt;/PRE&gt;
&lt;P&gt;But I have 1000's of possible combinations in the real dataset and would like to know if there is a shortcut to do this?&lt;CODE class=" language-sas"&gt;&lt;/CODE&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Any help is appreciated.&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 04 Nov 2021 04:03:22 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Creating-a-new-variable-based-on-repeated-common-values-in-two/m-p/778411#M247766</guid>
      <dc:creator>ihtishamsultan</dc:creator>
      <dc:date>2021-11-04T04:03:22Z</dc:date>
    </item>
    <item>
      <title>Re: Creating a new variable based on repeated common values in two columns</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Creating-a-new-variable-based-on-repeated-common-values-in-two/m-p/778414#M247768</link>
      <description>&lt;P&gt;You example code doesn't really explain anything. It doesn't actually look at any combination of anything. It is testing piece wise. If DrugB = "A1" you get the exact same result without any consideration of what DrugA might be.&lt;/P&gt;
&lt;PRE&gt;data example;
   input druga $ drugb $;
   If DrugA= "A1" then C1=1;
   else if DrugA="A2" then C1=1;
   if DrugB= "A1" then C1=1;
   else if DrugB="A2" then C1=1;
datalines;
abc A1
.   A1
pdq A1
B   C
;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Many users here don't want to download Excel files because of virus potential, others have such things blocked by security software. Also if you give us Excel we have to create a SAS data set and due to the non-existent constraints on Excel data cells the result we end up with may not have variables of the same type (numeric or character) and even values.&lt;/P&gt;</description>
      <pubDate>Thu, 04 Nov 2021 03:20:16 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Creating-a-new-variable-based-on-repeated-common-values-in-two/m-p/778414#M247768</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2021-11-04T03:20:16Z</dc:date>
    </item>
    <item>
      <title>Re: Creating a new variable based on repeated common values in two columns</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Creating-a-new-variable-based-on-repeated-common-values-in-two/m-p/778426#M247772</link>
      <description>&lt;P&gt;Below one way to go. I've created a key with a distinct value for any combination of drugs. I've used md5() to create this key as this allows to define the length of the variable without having to know the lengths of the variables where you store the drug names.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
  input Sno DrugA $ DrugB $;
datalines;
1 A1 A2
2 B2 B3
3 C3 C4
4 A2 A4
5 A5 A18
6 A7 A17
7 A9 A81
8 K7 L9
9 L9 L19
10 L8 L3
11 A2 A1
12 A18 A5
13 A81 A9
14 A1 A2
15 B2 B3
;

data want(drop=_:);
  set have;
  _DrugA=upcase(compress(DrugA));
  _DrugB=upcase(compress(DrugB));
  call sortc(_DrugA,_DrugB);
  hash_key=put(md5(catx('|',_DrugA,_DrugB)),hex32.);
run;

proc sql;
  select 
    sno,
    DrugA,
    DrugB,
    hash_key,
    count(*) as n_same_drugs
  from want
  group by hash_key
  order by sno
  ;
quit;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Patrick_0-1636007601910.png" style="width: 558px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/65398i6C2BADB198CEC05F/image-dimensions/558x421?v=v2" width="558" height="421" role="button" title="Patrick_0-1636007601910.png" alt="Patrick_0-1636007601910.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 04 Nov 2021 06:33:38 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Creating-a-new-variable-based-on-repeated-common-values-in-two/m-p/778426#M247772</guid>
      <dc:creator>Patrick</dc:creator>
      <dc:date>2021-11-04T06:33:38Z</dc:date>
    </item>
  </channel>
</rss>

