<?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: How to create dummy variables for common codition in New SAS User</title>
    <link>https://communities.sas.com/t5/New-SAS-User/How-to-create-dummy-variables-for-common-codition/m-p/596846#M16035</link>
    <description>&lt;P&gt;If you don't have a big table, otherwise I would try Hash Table.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data test;
input id $ group condition1 condition2;
datalines;
a 1 123 9
b 1 123 5
c 1 234 7
d 0 456 8
e 0 123 8
f 0 234 9
;
run;
proc sql;
create table want as
select *,case when
(select count(distinct group) from test where condition1=a.condition1)=
(select count(distinct group) from test ) then 1
else 0 end as dummy1,
case when
(select count(distinct group) from test where condition2=a.condition2)=
(select count(distinct group) from test ) then 1
else 0 end as dummy2
 from test as a;
quit;&lt;/CODE&gt;&lt;/PRE&gt;</description>
    <pubDate>Wed, 16 Oct 2019 12:25:56 GMT</pubDate>
    <dc:creator>Ksharp</dc:creator>
    <dc:date>2019-10-16T12:25:56Z</dc:date>
    <item>
      <title>How to create dummy variables for common codition</title>
      <link>https://communities.sas.com/t5/New-SAS-User/How-to-create-dummy-variables-for-common-codition/m-p/596620#M15972</link>
      <description>&lt;P&gt;Hi masters,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I am wondering how to create dummy variables for matched conditions.&amp;nbsp;&lt;/P&gt;&lt;P&gt;The data looks like..&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;data test;&lt;BR /&gt;input id group condition1 condition2;&lt;BR /&gt;datalines;&lt;BR /&gt;a 1 123 9&lt;BR /&gt;b 1 123 5&lt;BR /&gt;c 1 234 7&lt;BR /&gt;d 0 456 8&lt;BR /&gt;e 0 123 8&lt;/P&gt;&lt;P&gt;f 0 234 9&lt;BR /&gt;;&lt;BR /&gt;run;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I want to create two dummies indicating the common conditions between two groups. Dummy1 takes 1 if condition1 is a common condition with any obs in another group. Similarly, dummy1 takes 1 if condition2 is a common condition with any obs in another group. After creating these dummies, I want to have the following dataset:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;data test;&lt;BR /&gt;input id group condition1 condition2 dummy1 dummy2;&lt;BR /&gt;datalines;&lt;BR /&gt;a 1 123 9 1 1&lt;BR /&gt;b 1 123 5 1 0&lt;BR /&gt;c 1 234 7 1 0&lt;BR /&gt;d 0 456 8 0 0&lt;BR /&gt;e 0 123 8 1 0&lt;/P&gt;&lt;P&gt;f 0 234 9 1 1&lt;BR /&gt;;&lt;BR /&gt;run;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thank you for your help!&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 15 Oct 2019 18:15:20 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/How-to-create-dummy-variables-for-common-codition/m-p/596620#M15972</guid>
      <dc:creator>hkim3677</dc:creator>
      <dc:date>2019-10-15T18:15:20Z</dc:date>
    </item>
    <item>
      <title>Re: How to create dummy variables for common codition</title>
      <link>https://communities.sas.com/t5/New-SAS-User/How-to-create-dummy-variables-for-common-codition/m-p/596636#M15978</link>
      <description>&lt;P&gt;If your data is representative of your actual problem, you can do like this&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data test;
input id $ group condition1 condition2;
datalines;
a 1 123 9
b 1 123 5
c 1 234 7
d 0 456 8
e 0 123 8
f 0 234 9
;
run;

data want(drop=i);
   set test;
   array _1{0:99, 0:999} _temporary_;
   array _2{0:99, 0:999} _temporary_;

   do until (lr1);
      set test end=lr1;
      _1[group, condition1]=1;
      _2[group, condition2]=1;
   end;

   do until (lr2);
      set test end=lr2;
      dummy1=0; dummy2=0;
      do i=0 to 99;
         dummy1=max(dummy1, (_1[i, condition1]=1 &amp;amp; group ne i));
         dummy2=max(dummy2, (_2[i, condition2]=1 &amp;amp; group ne i));
      end;
      output;
   end;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Result:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;id group condition1 condition2 dummy1 dummy2 
a  1     123        9          1      1 
b  1     123        5          1      0 
c  1     234        7          1      0 
d  0     456        8          0      0 
e  0     123        8          1      0 
f  0     234        9          1      1 &lt;/PRE&gt;</description>
      <pubDate>Tue, 15 Oct 2019 19:26:58 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/How-to-create-dummy-variables-for-common-codition/m-p/596636#M15978</guid>
      <dc:creator>PeterClemmensen</dc:creator>
      <dc:date>2019-10-15T19:26:58Z</dc:date>
    </item>
    <item>
      <title>Re: How to create dummy variables for common codition</title>
      <link>https://communities.sas.com/t5/New-SAS-User/How-to-create-dummy-variables-for-common-codition/m-p/596638#M15979</link>
      <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/198000"&gt;@hkim3677&lt;/a&gt;,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Or like this:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql;
create table want as
select *, count(distinct group)&amp;gt;1 as dummy2
from (select *, count(distinct group)&amp;gt;1 as dummy1
      from have
      group by condition1)
group by condition2
order by id;
quit;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;(using the standard names HAVE and WANT for input and output datasets, resp.)&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;PS: Note that in your test data &lt;FONT face="courier new,courier"&gt;id&lt;/FONT&gt; must be defined as a character variable (e.g. &lt;FONT face="courier new,courier"&gt;input id &lt;STRONG&gt;$&lt;/STRONG&gt;&lt;/FONT&gt; ...).&lt;/P&gt;</description>
      <pubDate>Tue, 15 Oct 2019 19:34:35 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/How-to-create-dummy-variables-for-common-codition/m-p/596638#M15979</guid>
      <dc:creator>FreelanceReinh</dc:creator>
      <dc:date>2019-10-15T19:34:35Z</dc:date>
    </item>
    <item>
      <title>Re: How to create dummy variables for common codition</title>
      <link>https://communities.sas.com/t5/New-SAS-User/How-to-create-dummy-variables-for-common-codition/m-p/596648#M15981</link>
      <description>&lt;P&gt;Something like below should work.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data test;
  input id $ group condition1 condition2;
  datalines;
a 1 123 9
b 1 123 5
c 1 234 7
d 0 456 8
e 0 123 8
f 0 234 9
;

data want;
  if _n_=1 then
    do;
      if 0 then set test(keep=group rename=(group=_group));
      dcl hash h1 (dataset:'test(keep=group condition1 rename=(group=_group)))', multidata:'y');
      h1.defineKey('condition1');
      h1.defineData('_group');
      h1.defineDone();
      dcl hash h2 (dataset:'test(keep=group condition2 rename=(group=_group)))', multidata:'y');
      h2.defineKey('condition2');
      h2.defineData('_group');
      h2.defineDone();
    end;

  set test;

  Dummy1 =0;
  h1.reset_dup();
  do while(h1.do_over() eq 0);
    if group ne _group then
      do;
        Dummy1 =1;
        leave;
      end;
  end;

  Dummy2 =0;
  h2.reset_dup();
  do while(h2.do_over() eq 0);
    if group ne _group then
      do;
        Dummy2 =1;
        leave;
      end;
  end;

run;

proc print;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Tue, 15 Oct 2019 20:01:24 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/How-to-create-dummy-variables-for-common-codition/m-p/596648#M15981</guid>
      <dc:creator>Patrick</dc:creator>
      <dc:date>2019-10-15T20:01:24Z</dc:date>
    </item>
    <item>
      <title>Re: How to create dummy variables for common codition</title>
      <link>https://communities.sas.com/t5/New-SAS-User/How-to-create-dummy-variables-for-common-codition/m-p/596846#M16035</link>
      <description>&lt;P&gt;If you don't have a big table, otherwise I would try Hash Table.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data test;
input id $ group condition1 condition2;
datalines;
a 1 123 9
b 1 123 5
c 1 234 7
d 0 456 8
e 0 123 8
f 0 234 9
;
run;
proc sql;
create table want as
select *,case when
(select count(distinct group) from test where condition1=a.condition1)=
(select count(distinct group) from test ) then 1
else 0 end as dummy1,
case when
(select count(distinct group) from test where condition2=a.condition2)=
(select count(distinct group) from test ) then 1
else 0 end as dummy2
 from test as a;
quit;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 16 Oct 2019 12:25:56 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/How-to-create-dummy-variables-for-common-codition/m-p/596846#M16035</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2019-10-16T12:25:56Z</dc:date>
    </item>
  </channel>
</rss>

