<?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: Identifying treatment groups in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Identifying-treatment-groups/m-p/368074#M87735</link>
    <description>&lt;P&gt;You can combine the next two steps:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;/*Here I am selecting the Managers with a Source=A since I will compare the 
firms this Manager controls with the firms the other Manager controls. T
stands for Target and A for Acquirer.*/
data all_acq;
	set work.list_noduplicates;
	if Source='A';
run;

/*Here I am selecting the Managers with a Source=T since I will compare the 
firms this Manager controls with the firms the other Manager controls*/
data all_tar;
	set work.list_noduplicates;
	if Source='T';
run;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;into one step:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;
data all_acq   all_tar;
	set work.list_noduplicates;
	if Source='A' then output all_acq; else
        if Source='T' then output all_tar;   else put '**Error: ' source=;
run;

&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;I shall try later to simplify the code, as much as I succeed.&lt;/P&gt;</description>
    <pubDate>Sun, 18 Jun 2017 10:07:10 GMT</pubDate>
    <dc:creator>Shmuel</dc:creator>
    <dc:date>2017-06-18T10:07:10Z</dc:date>
    <item>
      <title>Identifying treatment groups</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Identifying-treatment-groups/m-p/368068#M87733</link>
      <description>&lt;P&gt;I am trying to define a treatment group based on the following relationship.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;U&gt;&lt;STRONG&gt;&lt;IMG src="https://communities.sas.com/t5/image/serverpage/image-id/9465iB43110F18ED83BE6/image-size/original?v=1.0&amp;amp;px=-1" border="0" alt="Diagram" title="Diagram" /&gt;&lt;/STRONG&gt;&lt;/U&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;U&gt;&lt;STRONG&gt;What's going on in the diagram?&amp;nbsp;&lt;/STRONG&gt;&lt;/U&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;/P&gt;&lt;OL&gt;&lt;LI&gt;This diagram illustrates the relationship between two Managers. The analysis&amp;nbsp;is at the Group level and there are only two managers within one group.&amp;nbsp;&lt;/LI&gt;&lt;LI&gt;The big black colored cirlce contains all the firms controlled by that specific manager.&lt;/LI&gt;&lt;LI&gt;The smaller circles in the big circle are divided based on their industry codes.&amp;nbsp;&lt;/LI&gt;&lt;LI&gt;Within these small circles, all the firms that belong to that industry are included.&lt;/LI&gt;&lt;LI&gt;The relationships illustrate whether the firms should be compared or not.&amp;nbsp;&lt;/LI&gt;&lt;/OL&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;U&gt;&lt;STRONG&gt;What am I trying to do?&amp;nbsp;&lt;/STRONG&gt;&lt;/U&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;OL&gt;&lt;LI&gt;I am comparing the small circles of one Manager to the same circles that the other Manager may or may not control.&lt;/LI&gt;&lt;LI&gt;Whenever there are comparable&amp;nbsp;industries, then start go to Step 3. If not, then ignore that industry and assign treated=0 for all rows with that particular industry number and group number (all analysis is at the group level).&lt;/LI&gt;&lt;LI&gt;Check whether the two managers are controlling&amp;nbsp;the same firm, if yes then assign treated =1.&amp;nbsp;&lt;/LI&gt;&lt;LI&gt;If all firms are different (distinct) in that specific industry (i.e., no firm is controlled by both managers), then still assign treated = 1.&lt;/LI&gt;&lt;LI&gt;So for Step 3 and 4, it is basically, whenever both managers control firms&amp;nbsp;in the same industry, then assign 1 as treated for all.&lt;/LI&gt;&lt;/OL&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;U&gt;&lt;STRONG&gt;What did I do in the code?&lt;/STRONG&gt;&lt;/U&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;OL&gt;&lt;LI&gt;&lt;SPAN&gt;Partition all firms&amp;nbsp;in both A and T (I define A as one manager and T as the other, where A means acquiring manager and T means Targeted manager), in A only, and in B only. &amp;nbsp;For those in both A and T, treated=1 for all firms in this subgroup.&lt;BR /&gt;&lt;/SPAN&gt;&lt;/LI&gt;&lt;LI&gt;&lt;SPAN&gt;For firms in &lt;STRONG&gt;A&lt;/STRONG&gt; &lt;STRONG&gt;ONLY&lt;/STRONG&gt;, merge its industry with &lt;STRONG&gt;ALL&lt;/STRONG&gt;&amp;nbsp;industries that &lt;STRONG&gt;T covers&lt;/STRONG&gt;. If merged, treated=1.&lt;BR /&gt;&lt;/SPAN&gt;&lt;/LI&gt;&lt;LI&gt;&lt;SPAN&gt;Symmetrically, for firms in &lt;STRONG&gt;T ONLY&lt;/STRONG&gt;, merge its industry with &lt;STRONG&gt;ALL&lt;/STRONG&gt;&amp;nbsp;industries that &lt;STRONG&gt;A covers&lt;/STRONG&gt;, if merged, treated=1.&lt;BR /&gt;&lt;/SPAN&gt;&lt;/LI&gt;&lt;LI&gt;&lt;SPAN&gt;All other firms get treated=0.&lt;/SPAN&gt;&lt;/LI&gt;&lt;/OL&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;Below is what I have done so far as well as my sample. This is an extension&amp;nbsp;to a &lt;A href="https://communities.sas.com/t5/Base-SAS-Programming/Complex-cross-referencing-within-groups/m-p/366863#M87297" target="_self"&gt;previous problem&lt;/A&gt; that&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/88384"&gt;@Shmuel&lt;/a&gt;&amp;nbsp;and&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/30622"&gt;@gamotte&lt;/a&gt;&amp;nbsp;have provided extensive help with. I am posting this as a new question since defining the control and treatment group is often used by researchers&amp;nbsp;as well as practitioners and alternative codes may help others as well. I was able to write a code that gets the result, but I would like to know whether there are alternative and more efficient way to get the result. &amp;nbsp;&lt;BR /&gt;&lt;/SPAN&gt;&lt;/P&gt;</description>
      <pubDate>Sun, 18 Jun 2017 07:27:11 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Identifying-treatment-groups/m-p/368068#M87733</guid>
      <dc:creator>Yegen</dc:creator>
      <dc:date>2017-06-18T07:27:11Z</dc:date>
    </item>
    <item>
      <title>Re: Identifying treatment groups</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Identifying-treatment-groups/m-p/368069#M87734</link>
      <description>&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;FONT color="#0000FF"&gt;&lt;STRONG&gt;Here is what I have:&lt;/STRONG&gt;&lt;/FONT&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;DATA have;
	INPUT Mgrno Group ID Industry Source $;
	DATALINES; 
	9999 1 00462610 1842 A
	9999 1 01737210 1842 A
	9999 1 12690310 2832 A
	8888 1 12690310 2832 T
	8888 1 12690311 2832 T
	8888 1 37158620 2124 T
	9999 1 23298321 4238 A
	9999 1 53228424 4238 A
	8888 1 53228424 4238 T
	8888 1 23298321 4238 T
	9999 1 12290311 1233 A
	8888 1 01737211 1233 T
	8888 1 99998881 1233 T
	7777 2 12690310 2832 A
	3333 2 32690311 2832 T
	7777 2 37158620 2124 A
	7777 2 38138222 3321 A
	3333 2 44134422 3321 T
	7777 2 53228424 4238 A
	3333 2 77233421 4238 T
	3333 2 12290311 1233 T
	3333 2 93234320 1333 T
;
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;&lt;STRONG&gt;&lt;FONT color="#0000FF"&gt;Here is the want dataset:&lt;/FONT&gt;&lt;/STRONG&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;DATA want;
 INPUT Mgrno Group ID Industry Control;
 DATALINES; 
 9999 1 00462610 1842 0
 9999 1 01737210 1842 0
 9999 1 12690310 2832 0
 8888 1 12690310 2832 0 
 8888 1 12690311 2832 0 
 8888 1 37158620 2124 0 
 9999 1 23298321 4238 1 
 9999 1 53228424 4238 1 
 8888 1 53228424 4238 1
 8888 1 23298321 4238 1 
 9999 1 12290311 1233 1 
 8888 1 01737211 1233 1 
 8888 1 99998881 1233 1
 7777 2 12690310 2832 1 
 3333 2 32690311 2832 1 
 7777 2 37158620 2124 0 
 7777 2 38138222 3321 1
 3333 2 44134422 3321 1 
 7777 2 53228424 4238 1 
 3333 2 77233421 4238 1 
 3333 2 12290311 1233 0 
 3333 2 93234320 1333 0 
;
RUN;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&lt;BR /&gt;&lt;FONT color="#0000FF"&gt;&lt;STRONG&gt;Here is my code:&lt;/STRONG&gt;&lt;/FONT&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sort data=work.have nouniquekeys uniqueout=singles out=dup;
	by group id;
quit;


proc sort data=work.have out=list_noduplicates nodupkey;
	by group id;
quit;



data both;
	set dup;
	cont=1;
run;

/*The firms controlled by both managers are control*/
proc sql;
	create table distinct_both as
	select distinct group, industry, 1 as control
	from both;
quit;


/*Here I am selecting the Managers with a Source=A since I will compare the 
firms this Manager controls with the firms the other Manager controls. T
stands for Target and A for Acquirer.*/
data all_acq;
	set work.list_noduplicates;
	if Source='A';
run;

/*Here I am selecting the Managers with a Source=T since I will compare the 
firms this Manager controls with the firms the other Manager controls*/
data all_tar;
	set work.list_noduplicates;
	if Source='T';
run;



/*Distinct cases start here, first compare distinct industries of target to 
all industries of the acquirer*/

data distinct_tar;
	set singles;
	if source='T';
	distinct_tar=1;
	industry_target=industry;
	cusip_tar=ncusip;
run;

proc sql;
	create table treated_tar_V1 as 
	select * 
	from all_acq as a 
	left join distinct_tar as b 
	on a.industry=b.industry_target and a.group=b.group;
quit;

/*These are some of the treated firms*/
data treated_tar_V2(keep=group industry);
	set treated_tar_V1;
	if industry_target^=.;
run;

proc sort data=treated_tar_V2 out=distinct_target nodupkey;
	by group industry;
run;

/*Second compare distinct firms controlled only by acquirer to all firms controlled by target*/

data distinct_acq;
	set singles;
	if source='A';
	distinct_acq=1;
	industry_acq=industry;
	cusip_acq=ncusip;
run;

proc sql;
	create table treated_acq_V1 as 
	select * 
	from all_tar as a 
	left join distinct_acq as b 
	on a.industry=b.industry_acq and a.group=b.group;
quit;

/*These are the treated firms*/
data treated_acq_V2(keep=group industry);
	set treated_acq_V1;
	if industry_acq^=.;
run;

proc sort data=treated_acq_V2 nodupkey;
	by group industry;
run;


data all_treated(keep=group industry treated);
	set distinct_both treated_tar_V2 treated_acq_V2;
	treated=1;
run;


proc sql;
	create table all_treated_distinct as 
	select distinct group, industry, 1 as treated
	from all_treated;
quit;&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;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 19 Jun 2017 04:56:32 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Identifying-treatment-groups/m-p/368069#M87734</guid>
      <dc:creator>Yegen</dc:creator>
      <dc:date>2017-06-19T04:56:32Z</dc:date>
    </item>
    <item>
      <title>Re: Identifying treatment groups</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Identifying-treatment-groups/m-p/368074#M87735</link>
      <description>&lt;P&gt;You can combine the next two steps:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;/*Here I am selecting the Managers with a Source=A since I will compare the 
firms this Manager controls with the firms the other Manager controls. T
stands for Target and A for Acquirer.*/
data all_acq;
	set work.list_noduplicates;
	if Source='A';
run;

/*Here I am selecting the Managers with a Source=T since I will compare the 
firms this Manager controls with the firms the other Manager controls*/
data all_tar;
	set work.list_noduplicates;
	if Source='T';
run;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;into one step:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;
data all_acq   all_tar;
	set work.list_noduplicates;
	if Source='A' then output all_acq; else
        if Source='T' then output all_tar;   else put '**Error: ' source=;
run;

&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;I shall try later to simplify the code, as much as I succeed.&lt;/P&gt;</description>
      <pubDate>Sun, 18 Jun 2017 10:07:10 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Identifying-treatment-groups/m-p/368074#M87735</guid>
      <dc:creator>Shmuel</dc:creator>
      <dc:date>2017-06-18T10:07:10Z</dc:date>
    </item>
    <item>
      <title>Re: Identifying treatment groups</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Identifying-treatment-groups/m-p/368112#M87750</link>
      <description>&lt;P&gt;Your replies are always very helpful,&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/88384"&gt;@Shmuel&lt;/a&gt;. Thank you.&amp;nbsp;I think the same result may be obtained by looking at a given group and selecting the industries where there are only two managers. It actually is much simpler this way than comparing the industries of the two managers within a given group. I will try to write a SQL code to try it out.&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sun, 18 Jun 2017 23:29:13 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Identifying-treatment-groups/m-p/368112#M87750</guid>
      <dc:creator>Yegen</dc:creator>
      <dc:date>2017-06-18T23:29:13Z</dc:date>
    </item>
    <item>
      <title>Re: Identifying treatment groups</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Identifying-treatment-groups/m-p/368154#M87769</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/88384"&gt;@Shmuel&lt;/a&gt;, I just found an&amp;nbsp;alternative way that mainly relies on&amp;nbsp;proc summary and proc sql. This code&amp;nbsp;is much easier to follow. First, I identify the industries where there is only one manager, and then identify the industries where there is only one firm. It's actually pretty simple this way although I went backwards by identifying the treated=0 (or control=0) group. The other codes identifies the treated=1 group and both ways produce the same result, which is great! Made me quite happy. &lt;span class="lia-unicode-emoji" title=":slightly_smiling_face:"&gt;🙂&lt;/span&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;FONT color="#FF0000"&gt;&lt;STRONG&gt;Update:&lt;/STRONG&gt;&lt;/FONT&gt; Apparently, I should have not been so quick with being happy about the code. It turns out that when I use the code below in the actual data, I get very different results. The logic of the original code is exactly as the one in the diagram. In the code below, I tried a different way, but it turns out that this did not work well. I am still not sure where the difference is though.&amp;nbsp;&lt;BR /&gt;&lt;BR /&gt;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
	set have;
run;

/*Counting the number of firms within a given group-industry pair*/
proc summary nway;
	class group industry;
	var id;
	output out=count_firms(drop=_:) 
	n=count_firms;
run;

/*Adding it back to the original sample*/
proc sql;
	create table work.have1 as 
	select * 
	from work.have as a 
	left join work.count_firms as b 
	on a.group=b.group &amp;amp; a.industry=b.industry;
quit;

proc sort data=have1 out=dup nodupkey;
	by group industry mgrno;
quit;

/*Counting the number of distinct managers within a given group-industry pair*/

proc summary nway;
	class group industry;
	var MGRNO;
	output out=count_managers(drop=_:) 
	n=count_managers;
run;

/*Adding it back to the original sample*/
proc sql;
	create table work.output_V2 as 
	select * 
	from work.have1 as a 
	left join work.count_managers as b 
	on a.group=b.group &amp;amp; a.industry=b.industry;
quit;

/*Whenever there is a single manager or whenever there is only one firm in a given group-industry , treated=0*/
data work.output_V3;
	set work.output_V2;
	if count_firms=1 or count_managers=1 then treated=0; else treated=1;
run;

proc sql;
	create table all_treated_distinct1 as 
	select distinct group, industry, treated
	from output_V3
	where treated=1;
quit;

proc sql;
	create table want as 
	select *
	from have as a 
	left join all_treated_distinct1 as b
	on (a.group=b.group) &amp;amp; (a.industry=b.industry);
quit;

proc stdize data = work.want out=work.want reponly missing=0;
	var treated;
run; &lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 19 Jun 2017 06:29:34 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Identifying-treatment-groups/m-p/368154#M87769</guid>
      <dc:creator>Yegen</dc:creator>
      <dc:date>2017-06-19T06:29:34Z</dc:date>
    </item>
    <item>
      <title>Re: Identifying treatment groups</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Identifying-treatment-groups/m-p/368336#M87836</link>
      <description>&lt;P&gt;I'm not going to check your code. I know you can do it yourself.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Just for study of methods, though my results don't fit your WANT results, I attach here an&lt;/P&gt;
&lt;P&gt;absolutely different approach. It is more similar to the last table I posted:&lt;/P&gt;
&lt;TABLE width="710"&gt;
&lt;TBODY&gt;
&lt;TR&gt;
&lt;TD width="97"&gt;Category&lt;/TD&gt;
&lt;TD width="72"&gt;&lt;FONT color="#008000"&gt;&lt;STRONG&gt;Group&lt;/STRONG&gt;&lt;/FONT&gt;&lt;/TD&gt;
&lt;TD width="72"&gt;&lt;FONT color="#008000"&gt;&lt;STRONG&gt;Industry&lt;/STRONG&gt;&lt;/FONT&gt;&lt;/TD&gt;
&lt;TD width="72"&gt;&lt;FONT color="#008000"&gt;&lt;STRONG&gt;FirmID&lt;/STRONG&gt;&lt;/FONT&gt;&lt;/TD&gt;
&lt;TD width="89"&gt;&lt;FONT color="#FF9900"&gt;&lt;STRONG&gt;Count_Mgrs&lt;/STRONG&gt;&lt;/FONT&gt;&lt;/TD&gt;
&lt;TD width="89"&gt;&lt;FONT color="#FF9900"&gt;&lt;STRONG&gt;Count 2+&lt;/STRONG&gt;&lt;/FONT&gt;&lt;/TD&gt;
&lt;TD width="87"&gt;&lt;FONT color="#FF9900"&gt;&lt;STRONG&gt;Control&lt;/STRONG&gt;&lt;/FONT&gt;&lt;/TD&gt;
&lt;TD width="37"&gt;&amp;nbsp;&lt;/TD&gt;
&lt;TD width="95"&gt;Industry e.g.&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD&gt;A&lt;/TD&gt;
&lt;TD&gt;any&lt;/TD&gt;
&lt;TD&gt;any&lt;/TD&gt;
&lt;TD&gt;any&lt;/TD&gt;
&lt;TD&gt;1&lt;/TD&gt;
&lt;TD&gt;&amp;nbsp;&lt;/TD&gt;
&lt;TD&gt;0&lt;/TD&gt;
&lt;TD&gt;&amp;nbsp;&lt;/TD&gt;
&lt;TD&gt;2124&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD&gt;B&lt;/TD&gt;
&lt;TD&gt;any&lt;/TD&gt;
&lt;TD&gt;any&lt;/TD&gt;
&lt;TD&gt;any&amp;nbsp;&lt;/TD&gt;
&lt;TD&gt;2+&lt;/TD&gt;
&lt;TD&gt;&amp;nbsp;&lt;/TD&gt;
&lt;TD&gt;&amp;nbsp;see below&lt;/TD&gt;
&lt;TD&gt;&amp;nbsp;&lt;/TD&gt;
&lt;TD&gt;1842&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD&gt;C&lt;/TD&gt;
&lt;TD&gt;any&lt;/TD&gt;
&lt;TD&gt;any&lt;/TD&gt;
&lt;TD&gt;&amp;nbsp;&lt;/TD&gt;
&lt;TD&gt;max=1&lt;/TD&gt;
&lt;TD&gt;&amp;nbsp;&lt;/TD&gt;
&lt;TD&gt;1&lt;/TD&gt;
&lt;TD&gt;&amp;nbsp;&lt;/TD&gt;
&lt;TD&gt;1233&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD&gt;D&lt;/TD&gt;
&lt;TD&gt;any&lt;/TD&gt;
&lt;TD&gt;any&lt;/TD&gt;
&lt;TD&gt;&amp;nbsp;&lt;/TD&gt;
&lt;TD&gt;max=2+&lt;/TD&gt;
&lt;TD&gt;1&lt;/TD&gt;
&lt;TD&gt;0&lt;/TD&gt;
&lt;TD&gt;&amp;nbsp;&lt;/TD&gt;
&lt;TD&gt;2832&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD&gt;E&lt;/TD&gt;
&lt;TD&gt;any&lt;/TD&gt;
&lt;TD&gt;any&lt;/TD&gt;
&lt;TD&gt;&amp;nbsp;&lt;/TD&gt;
&lt;TD&gt;max=2+&lt;/TD&gt;
&lt;TD&gt;2+&lt;/TD&gt;
&lt;TD&gt;1&lt;/TD&gt;
&lt;TD&gt;&amp;nbsp;&lt;/TD&gt;
&lt;TD&gt;1842&lt;/TD&gt;
&lt;/TR&gt;
&lt;/TBODY&gt;
&lt;/TABLE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Pay attention to:&amp;nbsp;my notes, especially the &lt;FONT color="#FF0000"&gt;&lt;STRONG&gt;red&lt;/STRONG&gt; &lt;/FONT&gt;emphasized:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;DATA have;
    INPUT Mgrno Group ID Industry Source $;  &lt;STRONG&gt;&lt;FONT color="#FF0000"&gt;/* line updated */&lt;/FONT&gt;&lt;/STRONG&gt;
    drop source;  &lt;FONT color="#FF0000"&gt;&lt;STRONG&gt;/* no need as it is equivalent to Group-Mgrno */&lt;/STRONG&gt;&lt;/FONT&gt;
    DATALINES; 
    9999 1 00462610 1842 A
    9999 1 01737210 1842 A
    9999 1 12690310 2832 A
    8888 1 12690310 2832 T
    8888 1 12690311 2832 T
    8888 1 37158620 2124 T
    9999 1 23298321 4238 A
    9999 1 53228424 4238 A
    8888 1 53228424 4238 T
    8888 1 23298321 4238 T
    9999 1 12290311 1233 A
    8888 1 01737211 1233 T
    8888 1 99998881 1233 T
    7777 2 12690310 2832 A
    3333 2 32690311 2832 T
    7777 2 37158620 2124 A
    7777 2 38138222 3321 A
    3333 2 44134422 3321 T
    7777 2 53228424 4238 A
    3333 2 77233421 4238 T
    3333 2 12290311 1233 T
    3333 2 93234320 1333 T
;
RUN;

proc sort data=have;
    by Group Industry ID MgrnO;
quit;

data keys (keep=Group Industry ID MgrnO)
     temp (drop=c_mgr_ind)
     c_per_id (drop=mgrno c_mgr_ind control)
     c_per_ind(drop=mgrno ID c_mgr_ID ID control)
   ;
  format Group Industry ID MgrnO; &lt;FONT color="#FF0000"&gt;&lt;STRONG&gt;/* force order of variables */&lt;/STRONG&gt;&lt;/FONT&gt;
  set have;
    by Group Industry ID;
       &lt;FONT color="#FF0000"&gt;&lt;STRONG&gt;retain&lt;/STRONG&gt; &lt;/FONT&gt;c_mgr_ID c_mgr_ind;
       output keys;
       if first.Industry then do; 
          c_mgr_ID=0;    /* count managers per ID */
          c_mgr_ind=0;   /* Count managers per Industry */
       end;
       if first.ID and last.ID 
       then do;
          c_mgr_ID=1; 
          c_mgr_ind +1;
          control=0; 
          output temp; 
       end; 
       else do;
          c_mgr_ID +1;
          if last.ID then do;
             output c_per_id; 
             c_mgr_ind + c_mgr_ID;
             c_mgr_ID =0;
          end;
       end;
       if last.industry then output c_per_ind;
run;


data temp1;
 merge keys temp(in=intmp);
 by Group Industry ID Mgrno;
run;

data temp2;
 merge temp1(in=intmp) c_per_id;
 by Group Industry ID;
    if not intmp then control=.;
run;
 
data temp3;
 merge temp2 
       c_per_ind;
 by Group Industry;
run;

data want_new;
 set temp3;
    if control=. then do;&lt;BR /&gt;       if c_mgr_ind = 1 then control=1; else
       if int(c_mgr_ind/2) le 1 then control=0; else  /* zero or one pair only */
       if c_mgr_ind ge 4 then control=1; else control=0;
    end;
run; 

/*========= checking/comparing to wanted results =======================*/
DATA want;
 INPUT Mgrno Group ID Industry Control;
 DATALINES; 
 9999 1 00462610 1842 0
 9999 1 01737210 1842 0
 9999 1 12690310 2832 0
 8888 1 12690310 2832 0 
 8888 1 12690311 2832 0 
 8888 1 37158620 2124 0 
 9999 1 23298321 4238 1 
 9999 1 53228424 4238 1 
 8888 1 53228424 4238 1
 8888 1 23298321 4238 1 
 9999 1 12290311 1233 1 
 8888 1 01737211 1233 1 
 8888 1 99998881 1233 1
 7777 2 12690310 2832 1 
 3333 2 32690311 2832 1 
 7777 2 37158620 2124 0 
 7777 2 38138222 3321 1
 3333 2 44134422 3321 1 
 7777 2 53228424 4238 1 
 3333 2 77233421 4238 1 
 3333 2 12290311 1233 0 
 3333 2 93234320 1333 0 
;
RUN;   
       
proc sort data=want;
    by Group Industry ID MgrnO;
quit;

proc sort data=want_new;
    by Group Industry ID MgrnO;
quit;

&lt;FONT color="#FF0000"&gt;&lt;STRONG&gt;proc compare&lt;/STRONG&gt;&lt;/FONT&gt; data=want compare=want_new criteria=1.0;
 ID Group Industry ID MgrnO;
 format ID 9.;
run;

data diff;
 merge want (in=in1) 
       want_new(in=in2 rename=(control=c_control));
 by Group Industry ID MgrnO;
   if in1 and not in2 then stat='IN1'; else
   if in2 and not in1 then stat='IN2'; 
   if not (in1 and in2);
run;

  


&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Mon, 19 Jun 2017 14:32:56 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Identifying-treatment-groups/m-p/368336#M87836</guid>
      <dc:creator>Shmuel</dc:creator>
      <dc:date>2017-06-19T14:32:56Z</dc:date>
    </item>
    <item>
      <title>Re: Identifying treatment groups</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Identifying-treatment-groups/m-p/368338#M87837</link>
      <description>&lt;P&gt;When we use different code we often get different results. It is not easy to be a programmer and not easy to understand why. You need mathematitian logic to understand why. If results are equal then either are both OK or both with same logic error.&lt;/P&gt;</description>
      <pubDate>Mon, 19 Jun 2017 14:36:26 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Identifying-treatment-groups/m-p/368338#M87837</guid>
      <dc:creator>Shmuel</dc:creator>
      <dc:date>2017-06-19T14:36:26Z</dc:date>
    </item>
    <item>
      <title>Re: Identifying treatment groups</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Identifying-treatment-groups/m-p/369257#M88137</link>
      <description>&lt;P&gt;You are certainly right,&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/88384"&gt;@Shmuel&lt;/a&gt;. I agree with you that having the correct logic is key. I think the first code should be correct since in the other one I may have not deleted some firms that were supposed to be deleted. So the logic in the second one may be incorrect.&amp;nbsp;&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;/P&gt;</description>
      <pubDate>Wed, 21 Jun 2017 18:18:46 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Identifying-treatment-groups/m-p/369257#M88137</guid>
      <dc:creator>Yegen</dc:creator>
      <dc:date>2017-06-21T18:18:46Z</dc:date>
    </item>
    <item>
      <title>Re: Identifying treatment groups</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Identifying-treatment-groups/m-p/369264#M88141</link>
      <description>&lt;P&gt;That is beyond SAS and I believe you are able to struggle the logic better than me.&lt;/P&gt;</description>
      <pubDate>Wed, 21 Jun 2017 18:29:42 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Identifying-treatment-groups/m-p/369264#M88141</guid>
      <dc:creator>Shmuel</dc:creator>
      <dc:date>2017-06-21T18:29:42Z</dc:date>
    </item>
    <item>
      <title>Re: Identifying treatment groups</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Identifying-treatment-groups/m-p/369347#M88165</link>
      <description>&lt;P&gt;You are right that the logic factor is beyong SAS, but I would argue that your better at logic than me. &lt;span class="lia-unicode-emoji" title=":slightly_smiling_face:"&gt;🙂&lt;/span&gt; Plus, your help with finding alternative ways to solve&amp;nbsp;the issues&amp;nbsp;has been very helpful. You are honestly a great SAS mentor and teacher! I benefit a lot from your&amp;nbsp;comments.&lt;BR /&gt;&lt;BR /&gt;&lt;/P&gt;</description>
      <pubDate>Wed, 21 Jun 2017 21:55:01 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Identifying-treatment-groups/m-p/369347#M88165</guid>
      <dc:creator>Yegen</dc:creator>
      <dc:date>2017-06-21T21:55:01Z</dc:date>
    </item>
    <item>
      <title>Re: Identifying treatment groups</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Identifying-treatment-groups/m-p/369834#M88343</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/88384"&gt;@Shmuel&lt;/a&gt;, I think the earlier code works correctly so we can ignore the second (simplified) code since it produces incorrect results. I think I am failing&amp;nbsp;to exclude cases that should not be in the final sample. It seems like identifying directly the treatment group is a safer way compared&amp;nbsp;to deleting the control group to identify the treatment (indirect&amp;nbsp;way).&lt;BR /&gt;&lt;BR /&gt;If you prefer and have not much time, I can accept your modification (i.e., simplification) of my code as the answer or just simply wait until you may have something. A colleague&amp;nbsp;of mine wrote another code that produces almost the same outcome as the first code (only off by one&amp;nbsp;observation).&amp;nbsp;&lt;BR /&gt;&lt;BR /&gt;To be frank, I have to state that without your comments I would have not been able to go to the correct direction. So your earlier replies have really helped out to come up with these codes! Thank you!!&lt;/P&gt;</description>
      <pubDate>Fri, 23 Jun 2017 11:46:33 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Identifying-treatment-groups/m-p/369834#M88343</guid>
      <dc:creator>Yegen</dc:creator>
      <dc:date>2017-06-23T11:46:33Z</dc:date>
    </item>
  </channel>
</rss>

