<?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: Set condition in same column and then create a new variable in SAS Software for Learning Community</title>
    <link>https://communities.sas.com/t5/SAS-Software-for-Learning/Set-condition-in-same-column-and-then-create-a-new-variable/m-p/899581#M1468</link>
    <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/459025"&gt;@Olynn&lt;/a&gt;&amp;nbsp;See if below returns what you're after. I've used this time a SQL because I find it often easier to implement the logic when it comes to joins over date ranges.&lt;/P&gt;
&lt;P&gt;If you don't want the last case with companyid 9999 then change the SQL to an inner join - but then column ed_dummy wouldn't make much sense anymore as it would always be 1.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
  infile datalines truncover dsd;
  input companyid $ awarddate:date9. directorid $;
  format awarddate date9.;
  datalines;
111,01jan2017,0001
111,01jan2018,0002
111,01jan2018,0003
111,01jan2023,0004
111,01jan2023,0005
111,01jan2023,0006
222,01jan2023,0007
222,01jan2023,0008
333,01jan2023,9999
;

proc sql;
  select distinct
    l.companyid,

    case
      when l.directorid&amp;lt;r.directorid or missing(r.directorid) then l.directorid
      else r.directorid
      end as directorid_1,
    case
      when l.directorid&amp;lt;r.directorid and not missing(r.directorid) then r.directorid
      when not missing(r.directorid) then l.directorid
      else ' '
      end as directorid_2,

    min(l.awarddate,r.awarddate) as overlapped_awarddate format=date9.,

    not missing(r.directorid) as ed_dummy,

    case
      when l.directorid&amp;lt;r.directorid or missing(r.directorid) then l.awarddate
      else r.awarddate
      end as awarddate_1 format=date9.,
    case
      when l.directorid&amp;lt;r.directorid and not missing(r.directorid) then r.awarddate
      when not missing(r.directorid) then l.awarddate
      else .
      end as awarddate_2 format=date9.

  from have l left join have r
    on 
      l.companyid=r.companyid 
      and l.directorid ne r.directorid
      and l.awarddate between intnx('year',r.awarddate,-1,'s') and r.awarddate

  order by companyid, overlapped_awarddate, directorid_1
  ;
quit;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Patrick_0-1698014921157.png" style="width: 675px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/89006iBA6EAAE650ECF2CC/image-dimensions/675x225?v=v2" width="675" height="225" role="button" title="Patrick_0-1698014921157.png" alt="Patrick_0-1698014921157.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Sun, 22 Oct 2023 23:09:02 GMT</pubDate>
    <dc:creator>Patrick</dc:creator>
    <dc:date>2023-10-22T23:09:02Z</dc:date>
    <item>
      <title>Set condition in same column and then create a new variable</title>
      <link>https://communities.sas.com/t5/SAS-Software-for-Learning/Set-condition-in-same-column-and-then-create-a-new-variable/m-p/899542#M1465</link>
      <description>&lt;P&gt;&lt;SPAN&gt;I want to create ED social ties dummy variable (education, two individuals are socially connected through their education network if they went to the same school and graduated within one year of each other tie now).&amp;nbsp; As the first screenshot shown, Is it possible to set the condition that award date 1 = 2, director id 3 NE 4, and company ID 5 = 6? and then create another table that First column is director id1, second column is director id2, the third column is award date, and the final column is ED dummy which equal 1, which means they have social ties (Just like the second screenshot shown). Many thanks for your help.&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Olynn_0-1697906342030.png" style="width: 400px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/89001i018A0498E230CC69/image-size/medium?v=v2&amp;amp;px=400" role="button" title="Olynn_0-1697906342030.png" alt="Olynn_0-1697906342030.png" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Olynn_1-1697906638012.png" style="width: 400px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/89002iB5691ABD59347671/image-size/medium?v=v2&amp;amp;px=400" role="button" title="Olynn_1-1697906638012.png" alt="Olynn_1-1697906638012.png" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sat, 21 Oct 2023 16:47:43 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Software-for-Learning/Set-condition-in-same-column-and-then-create-a-new-variable/m-p/899542#M1465</guid>
      <dc:creator>Olynn</dc:creator>
      <dc:date>2023-10-21T16:47:43Z</dc:date>
    </item>
    <item>
      <title>Re: Set condition in same column and then create a new variable</title>
      <link>https://communities.sas.com/t5/SAS-Software-for-Learning/Set-condition-in-same-column-and-then-create-a-new-variable/m-p/899553#M1466</link>
      <description>&lt;P&gt;Going forward please help us help you and provide sample HAVE data via a tested SAS datastep as done in below code, show us the desired result using the sample data and explain the logic to get from have to want.&lt;/P&gt;
&lt;P&gt;Screenshots are hard to work with and always require extra effort for the ones answering your questions free of charge which often leads to less people being willing to put in this extra effort.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I'm not sure if I understood your question right. If below code doesn't return what you're after then please amend the sample data and show us the desired result.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
  infile datalines truncover dsd;
  input companyid $ awarddate:date9. directorid $;
  format awarddate date9.;
  datalines;
111,01jan2023,0001
111,01jan2023,0001
111,01jan2023,0002
222,01jan2023,0001
;

proc sort data=have out=inter nodupkey;
  by companyid awarddate directorid;
run;

data want(keep=companyid awarddate director_id_: ed_dummy);
  set inter;
  by companyid awarddate;
  array director_id_ {4} $10;
  retain director_id_ ed_dummy;
  if first.awarddate then
    do;
      call missing(_cnt, of director_id_[*]);
      ed_dummy= not last.awarddate;
    end;
  _cnt+1;
  director_id_[_cnt]=directorid;
  if last.awarddate then output;
run;

proc print data=want;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Patrick_0-1697944287684.png" style="width: 570px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/89003i4385F10B58B88CC1/image-dimensions/570x72?v=v2" width="570" height="72" role="button" title="Patrick_0-1697944287684.png" alt="Patrick_0-1697944287684.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;If there can never be more than two directors in the same group then just change in the code the number of array elements from 4 to 2. ....and should there ever be more directors in the same group than defined for the array then you will get an error "array subscript out of range".&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sun, 22 Oct 2023 03:14:45 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Software-for-Learning/Set-condition-in-same-column-and-then-create-a-new-variable/m-p/899553#M1466</guid>
      <dc:creator>Patrick</dc:creator>
      <dc:date>2023-10-22T03:14:45Z</dc:date>
    </item>
    <item>
      <title>Re: Set condition in same column and then create a new variable</title>
      <link>https://communities.sas.com/t5/SAS-Software-for-Learning/Set-condition-in-same-column-and-then-create-a-new-variable/m-p/899557#M1467</link>
      <description>&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="sas"&gt;data have;
  infile datalines truncover dsd;
  input companyid $ awarddate:date9. directorid $;
  format awarddate date9.;
  datalines;
111,01jan2017,0001
111,01jan2018,0002
111,01jan2018,0003
111,01jan2023,0004
111,01jan2023,0005
111,01jan2023,0006
222,01jan2023,0007
222,01jan2023,0008
;&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Hi Patric,&lt;/P&gt;&lt;P&gt;Sorry for providing without specific data set, and make you misunderstand the question. I will use the data set you provide and try to&amp;nbsp; explain it one more time.&amp;nbsp;&lt;/P&gt;&lt;P&gt;1.&amp;nbsp;Companyid is the school the directors attain.&lt;/P&gt;&lt;P&gt;2. Awarddate is the date they graduated.&lt;/P&gt;&lt;P&gt;3.&amp;nbsp;Every director has a unique &lt;STRONG&gt;directorid&amp;nbsp;&lt;/STRONG&gt;so every ID will be different.&lt;/P&gt;&lt;P&gt;To make a social ties, if two director graduated in same school within one year and share the same companyid, then the&amp;nbsp; ED dummy will be one. For example, director 0001 and 0002 have social ties (They attain same school and Director 0001 graduation period 01Jan2016~01Jan2017 &lt;STRONG&gt;overlap&lt;/STRONG&gt; Director 0002&amp;nbsp; graduation period 01Jan2017~01Jan2018 &lt;STRONG&gt;on&lt;/STRONG&gt; 01Jan2017); Director 0004 and Director 0005 have social ties because their graduate period have overlapped and graduate in same school.&lt;/P&gt;&lt;P&gt;The final form will be like this screenshot.&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Olynn_0-1697952332118.png" style="width: 400px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/89004i28FC988AA180BBF5/image-size/medium?v=v2&amp;amp;px=400" role="button" title="Olynn_0-1697952332118.png" alt="Olynn_0-1697952332118.png" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;The solution you provide before is pretty good!&lt;/P&gt;&lt;P&gt;You are so smart and kind.&lt;/P&gt;&lt;P&gt;Thanks for your help.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sun, 22 Oct 2023 05:35:58 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Software-for-Learning/Set-condition-in-same-column-and-then-create-a-new-variable/m-p/899557#M1467</guid>
      <dc:creator>Olynn</dc:creator>
      <dc:date>2023-10-22T05:35:58Z</dc:date>
    </item>
    <item>
      <title>Re: Set condition in same column and then create a new variable</title>
      <link>https://communities.sas.com/t5/SAS-Software-for-Learning/Set-condition-in-same-column-and-then-create-a-new-variable/m-p/899581#M1468</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/459025"&gt;@Olynn&lt;/a&gt;&amp;nbsp;See if below returns what you're after. I've used this time a SQL because I find it often easier to implement the logic when it comes to joins over date ranges.&lt;/P&gt;
&lt;P&gt;If you don't want the last case with companyid 9999 then change the SQL to an inner join - but then column ed_dummy wouldn't make much sense anymore as it would always be 1.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
  infile datalines truncover dsd;
  input companyid $ awarddate:date9. directorid $;
  format awarddate date9.;
  datalines;
111,01jan2017,0001
111,01jan2018,0002
111,01jan2018,0003
111,01jan2023,0004
111,01jan2023,0005
111,01jan2023,0006
222,01jan2023,0007
222,01jan2023,0008
333,01jan2023,9999
;

proc sql;
  select distinct
    l.companyid,

    case
      when l.directorid&amp;lt;r.directorid or missing(r.directorid) then l.directorid
      else r.directorid
      end as directorid_1,
    case
      when l.directorid&amp;lt;r.directorid and not missing(r.directorid) then r.directorid
      when not missing(r.directorid) then l.directorid
      else ' '
      end as directorid_2,

    min(l.awarddate,r.awarddate) as overlapped_awarddate format=date9.,

    not missing(r.directorid) as ed_dummy,

    case
      when l.directorid&amp;lt;r.directorid or missing(r.directorid) then l.awarddate
      else r.awarddate
      end as awarddate_1 format=date9.,
    case
      when l.directorid&amp;lt;r.directorid and not missing(r.directorid) then r.awarddate
      when not missing(r.directorid) then l.awarddate
      else .
      end as awarddate_2 format=date9.

  from have l left join have r
    on 
      l.companyid=r.companyid 
      and l.directorid ne r.directorid
      and l.awarddate between intnx('year',r.awarddate,-1,'s') and r.awarddate

  order by companyid, overlapped_awarddate, directorid_1
  ;
quit;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Patrick_0-1698014921157.png" style="width: 675px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/89006iBA6EAAE650ECF2CC/image-dimensions/675x225?v=v2" width="675" height="225" role="button" title="Patrick_0-1698014921157.png" alt="Patrick_0-1698014921157.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sun, 22 Oct 2023 23:09:02 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Software-for-Learning/Set-condition-in-same-column-and-then-create-a-new-variable/m-p/899581#M1468</guid>
      <dc:creator>Patrick</dc:creator>
      <dc:date>2023-10-22T23:09:02Z</dc:date>
    </item>
    <item>
      <title>Re: Set condition in same column and then create a new variable</title>
      <link>https://communities.sas.com/t5/SAS-Software-for-Learning/Set-condition-in-same-column-and-then-create-a-new-variable/m-p/899655#M1469</link>
      <description>Many thanks!!!!!!!!!!!!</description>
      <pubDate>Mon, 23 Oct 2023 16:07:05 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Software-for-Learning/Set-condition-in-same-column-and-then-create-a-new-variable/m-p/899655#M1469</guid>
      <dc:creator>Olynn</dc:creator>
      <dc:date>2023-10-23T16:07:05Z</dc:date>
    </item>
  </channel>
</rss>

