<?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 Creating a new variable based on corresponding IDs from another dataset in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Creating-a-new-variable-based-on-corresponding-IDs-from-another/m-p/607199#M176436</link>
    <description>&lt;P&gt;I have the following dataset (there are more years, but I am just showing the first two years):&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;data have;
     input year  ID  shock;
     cards;
     2000  1001  1
     2000  1002  0
     2000  1004  0
     2000  1006  1
     2000  1008  0
     2000  1010  0
     2000  1011  0
     2001  1001  0
     2001  1002  0
     2001  1004  1
     2001  1006  1
     2001  1008  1
     2001  1010  0
     2001  1011  0
;
run;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I have another dataset that lists "adjacent" IDs as follows:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;data list;
     input ID  ID_adj;
     cards;
         1001  1002
         1001  1006
         1002  1001
	 1004  1008
	 1004  1011
	 1006  1010
	 1008  1004
	 1008  1011
	 1010  1006
	 1010  1011
	 1011  1004
	 1011  1008
	 1011  1010
;
run;&lt;/PRE&gt;
&lt;P&gt;What I wish to do is create the following dataset:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;data want;
     input year  ID  shock treat $;
     cards;
         2000  1001  1  T
         2000  1002  0  A  
         2000  1004  0  N
	 2000  1006  1  T
	 2000  1008  0  N
	 2000  1010  0  A
	 2000  1011  0  N
	 2001  1001  0  A
         2001  1002  0  N
         2001  1004  1  T
	 2001  1006  1  T
	 2001  1008  1  T
	 2001  1010  0  A
	 2001  1011  0  A
;
run;&lt;/PRE&gt;
&lt;P&gt;The variable treat is defined as follows:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;1) If shock takes a value of 1, then treat = T.&lt;/P&gt;
&lt;P&gt;2) If shock takes a value of 0, then we look across all of the ID_adj in the list dataset that corresponds to the ID. If the value of shock is 1 for at least one ID_adj in the same year, then treat = A. It's best to illustrate this with an example.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;In the original dataset 'have', look at ID 1002 in year 2000. In the dataset 'list', ID 1002 corresponds to 1001 and in year 2000, ID 1001 has shock = 1. Thus, we set treat = A.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Similarly, in the original dataset 'have', look at ID 1010 in year 2000. In the dataset 'list', ID 1010 corresponds to 1006 and 1011. Although ID 1011 has shock = 0 in year 2000, ID 1006 has shock = 1 in year 2000, so treat for ID 1010 = A.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;3) If shock takes a value of 0, then we look across all of the ID_adj in the list dataset that corresponds to the ID. If the value of shock is 0 for all ID_adj in the same year, then treat = N. Again, it's best to illustrate with an example:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;In the original dataset 'have', look at ID 1008 in year 2000. In the dataset 'list', ID 1008 corresponds to 1004 and 1011. Both ID 1004 and 1011 have shock = 0 in year 2000, so treat = N for ID 1008 in year 2000.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Since the value of shock changes across different years, we need to do the above across all years, thereby producing the dataset 'want'.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Tue, 26 Nov 2019 05:13:35 GMT</pubDate>
    <dc:creator>TrueTears</dc:creator>
    <dc:date>2019-11-26T05:13:35Z</dc:date>
    <item>
      <title>Creating a new variable based on corresponding IDs from another dataset</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Creating-a-new-variable-based-on-corresponding-IDs-from-another/m-p/607199#M176436</link>
      <description>&lt;P&gt;I have the following dataset (there are more years, but I am just showing the first two years):&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;data have;
     input year  ID  shock;
     cards;
     2000  1001  1
     2000  1002  0
     2000  1004  0
     2000  1006  1
     2000  1008  0
     2000  1010  0
     2000  1011  0
     2001  1001  0
     2001  1002  0
     2001  1004  1
     2001  1006  1
     2001  1008  1
     2001  1010  0
     2001  1011  0
;
run;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I have another dataset that lists "adjacent" IDs as follows:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;data list;
     input ID  ID_adj;
     cards;
         1001  1002
         1001  1006
         1002  1001
	 1004  1008
	 1004  1011
	 1006  1010
	 1008  1004
	 1008  1011
	 1010  1006
	 1010  1011
	 1011  1004
	 1011  1008
	 1011  1010
;
run;&lt;/PRE&gt;
&lt;P&gt;What I wish to do is create the following dataset:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;data want;
     input year  ID  shock treat $;
     cards;
         2000  1001  1  T
         2000  1002  0  A  
         2000  1004  0  N
	 2000  1006  1  T
	 2000  1008  0  N
	 2000  1010  0  A
	 2000  1011  0  N
	 2001  1001  0  A
         2001  1002  0  N
         2001  1004  1  T
	 2001  1006  1  T
	 2001  1008  1  T
	 2001  1010  0  A
	 2001  1011  0  A
;
run;&lt;/PRE&gt;
&lt;P&gt;The variable treat is defined as follows:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;1) If shock takes a value of 1, then treat = T.&lt;/P&gt;
&lt;P&gt;2) If shock takes a value of 0, then we look across all of the ID_adj in the list dataset that corresponds to the ID. If the value of shock is 1 for at least one ID_adj in the same year, then treat = A. It's best to illustrate this with an example.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;In the original dataset 'have', look at ID 1002 in year 2000. In the dataset 'list', ID 1002 corresponds to 1001 and in year 2000, ID 1001 has shock = 1. Thus, we set treat = A.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Similarly, in the original dataset 'have', look at ID 1010 in year 2000. In the dataset 'list', ID 1010 corresponds to 1006 and 1011. Although ID 1011 has shock = 0 in year 2000, ID 1006 has shock = 1 in year 2000, so treat for ID 1010 = A.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;3) If shock takes a value of 0, then we look across all of the ID_adj in the list dataset that corresponds to the ID. If the value of shock is 0 for all ID_adj in the same year, then treat = N. Again, it's best to illustrate with an example:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;In the original dataset 'have', look at ID 1008 in year 2000. In the dataset 'list', ID 1008 corresponds to 1004 and 1011. Both ID 1004 and 1011 have shock = 0 in year 2000, so treat = N for ID 1008 in year 2000.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Since the value of shock changes across different years, we need to do the above across all years, thereby producing the dataset 'want'.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 26 Nov 2019 05:13:35 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Creating-a-new-variable-based-on-corresponding-IDs-from-another/m-p/607199#M176436</guid>
      <dc:creator>TrueTears</dc:creator>
      <dc:date>2019-11-26T05:13:35Z</dc:date>
    </item>
    <item>
      <title>Re: Creating a new variable based on corresponding IDs from another dataset</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Creating-a-new-variable-based-on-corresponding-IDs-from-another/m-p/607205#M176441</link>
      <description>&lt;P&gt;Proc SQL is well suited for this operation:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql;
create table want as
select
	a.*,
	case when a.shock then "T" when max(c.shock) then "A" else "N" end as treat
from
	have as a left join
	list as b on a.id=b.id left join
	have as c on b.id_adj=c.id and a.year=c.year
group by a.year, a.id, a.shock;
quit;
&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Tue, 26 Nov 2019 05:48:50 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Creating-a-new-variable-based-on-corresponding-IDs-from-another/m-p/607205#M176441</guid>
      <dc:creator>PGStats</dc:creator>
      <dc:date>2019-11-26T05:48:50Z</dc:date>
    </item>
  </channel>
</rss>

