<?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 variable based on another dataset in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Creating-a-variable-based-on-another-dataset/m-p/609195#M177355</link>
    <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/13162"&gt;@TrueTears&lt;/a&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Ideally you try and understand the code to a level where you can make such a change yourself. But here you go.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;
data want(keep=year firm_id city_id operate action action_adj);
  if _n_=1 then
    do;
      if 0 then set list;
      dcl hash list(dataset:'list(where=(use=1))', multidata:'y');
      list.defineKey('city_id');
      list.defineData('city_id_adj');
      list.defineDone();

      dcl hash Operate0(dataset:'have(keep=year city_id operate where=(operate=0))');
      Operate0.defineKey('year','city_id');
      Operate0.defineDone();

      dcl hash Action1(dataset:'have(keep=year city_id action where=(action=1))');
      Action1.defineKey('year','city_id');
      Action1.defineDone();
    end;
  call missing(of _all_);

  set have;

  /* If operate = 0, then action_adj = 3 */
  if operate = 0 then 
    do;
      action_adj = 3;
      output;
      return;
    end;

  /* For each city_id in have, we check its corresponding city_id_adj in dataset "list" that has use=1 */
  if list.check()=0 then
    do;
      /* If in this set of city_id_adj, there is one that has operate = 0 in the dataset "have", then action_adj = 2 */
      list.reset_dup();
      do while(list.do_over()=0);
        if Operate0.check(key:year, key:city_id_adj)=0 then
          do;
            action_adj = 2;
            output;
            return;
          end;
      end;

      /* action_adj = 1 if there is at least one city_id_adj (with use=1) that has action = 1 in the dataset "have". */
      list.reset_dup();
      do while(list.do_over()=0);
        if Action1.check(key:year, key:city_id_adj)=0 then
          do;
            action_adj = 1;
            output;
            return;
          end;
      end;

    end;

  /* action_adj = 0 for all other cases */
  action_adj = 0;
  output;
  return;

run;
&lt;/CODE&gt;&lt;/PRE&gt;</description>
    <pubDate>Tue, 03 Dec 2019 21:46:42 GMT</pubDate>
    <dc:creator>Patrick</dc:creator>
    <dc:date>2019-12-03T21:46:42Z</dc:date>
    <item>
      <title>Creating a variable based on another dataset</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Creating-a-variable-based-on-another-dataset/m-p/608918#M177250</link>
      <description>&lt;P&gt;I have a dataset similar to the following (but with more years and firm_id's)&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;data have;
input year firm_id city_id operate action;
cards;
2008 28013 1003 1 1
2008 28013 1004 1 1
2008 28013 1007 1 0
2008 28013 1009 1 1
2008 28013 1010 0 1
2008 28013 1013 1 0
2008 28013 1053 1 1
2008 28013 1074 1 0
2008 28013 1075 1 1
;
run;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I also have a list of each city_id and its adjacent city_id (city_id_adj) and a binary variable called "use":&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;data list;
input city_id city_id_adj use;
cards;
1003 1009 1
1003 1010 1
1003 1085 1
1004 1007 0
1004 1009 0
1004 1099 0
1004 1100 0
1007 1004 0
1007 1823 1
1009 1003 1
1009 1004 0
1009 1010 0
1010 1003 1
1010 1009 0
1013 1053 1
1013 1074 1
1053 1013 1
1074 1013 1
1075 1743 1
1075 1744 1
;
run;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I wish to produce the following dataset (I have more years and more firm_id's, so the code should work for multiple years and firm_id's)&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;data want;
input year firm_id city_id operate action action_adj;
cards;
2008 28013 1003 1 1 2
2008 28013 1004 1 1 0
2008 28013 1007 1 0 0
2008 28013 1009 1 1 1
2008 28013 1010 0 1 3
2008 28013 1013 1 0 1
2008 28013 1053 1 1 0
2008 28013 1074 1 0 0
2008 28013 1075 1 1 0
;
run;&lt;/PRE&gt;
&lt;P&gt;To produce the above dataset, the rule for producing the variable action_adj is as follows. Fix the year and firm_id:&lt;/P&gt;
&lt;P&gt;&lt;BR /&gt;If operate = 0, then action_adj = 3. An example is city_id 1010.&lt;/P&gt;
&lt;P&gt;&lt;BR /&gt;For operate = 1:&lt;/P&gt;
&lt;P&gt;&lt;BR /&gt;For each city_id, we check its corresponding city_id_adj in dataset "list" that has use=1. If in this set of city_id_adj, there is one that has operate = 0 in the dataset "have", then action_adj = 2. For example, city_id 1003 corresponds to 1009, 1010, and 1085 (all of these have use=1). Here 1010 has operate = 0, so action_adj = 2 for city_id 1003.&lt;/P&gt;
&lt;P&gt;&lt;BR /&gt;If the list of city_id_adj does not have any that has operate=0, then we do the following:&lt;/P&gt;
&lt;UL&gt;
&lt;LI&gt;action_adj = 1 if there is at least one city_id_adj (with use=1) that has action = 1 in the dataset "have". For example, consider city_id 1009, in the dataset "list", it corresponds to city_id_adj 1003 only (since this is the only one with use = 1). In the dataset "have", 1003 has action=1, so we set action_adj = 1 for 1009. Another example is city_id 1013. In the dataset "list", 1013 corresponds to 1053 and 1074 (both have use=1). Although 1074 has action=0, but 1053 has action=1, so we set action_adj = 1 for 1013.&lt;/LI&gt;
&lt;LI&gt;action_adj = 0 for all other cases.&lt;/LI&gt;
&lt;/UL&gt;
&lt;P&gt;Note all of the above is for a particular year and particular firm_id. This needs to be done for all firm_id and year pairs.&lt;/P&gt;</description>
      <pubDate>Tue, 03 Dec 2019 03:40:14 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Creating-a-variable-based-on-another-dataset/m-p/608918#M177250</guid>
      <dc:creator>TrueTears</dc:creator>
      <dc:date>2019-12-03T03:40:14Z</dc:date>
    </item>
    <item>
      <title>Re: Creating a variable based on another dataset</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Creating-a-variable-based-on-another-dataset/m-p/608936#M177252</link>
      <description>&lt;P&gt;Please try the below code&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
input year firm_id city_id operate action;
cards;
2008 28013 1003 1 1
2008 28013 1004 1 1
2008 28013 1007 1 0
2008 28013 1009 1 1
2008 28013 1010 0 1
2008 28013 1013 1 0
2008 28013 1053 1 1
2008 28013 1074 1 0
2008 28013 1075 1 1
;
run;


data list;
input city_id city_id_adj use;
cards;
1003 1009 1
1003 1010 1
1003 1085 1
1004 1007 0
1004 1009 0
1004 1099 0
1004 1100 0
1007 1004 0
1007 1823 1
1009 1003 1
1009 1004 0
1009 1010 0
1010 1003 1
1010 1009 0
1013 1053 1
1013 1074 1
1053 1013 1
1074 1013 1
1075 1743 1
1075 1744 1
;
run;


proc sort data=have;
by city_id;
run;

proc sort data=list;
by city_id;
run;

proc sort data=have out=operate;
by city_id;
where operate=0;
run;

proc sort data=have out=action;
by city_id;
where action=1;
run;

data want;
merge have(in=a) list(in=b)  ;
by city_id;
if a and b;
if operate=0 then action_adj=3;
run;

proc sort data=want;
by city_id_adj;
run;

data want2;
merge want(in=a) operate(in=v keep=city_id operate rename=(city_id=city_id_adj operate=operate2)) action(keep=city_id action rename=(city_id=city_id_adj action=action2));
by city_id_adj;
if a ;
if use=1 and operate=1 and operate2=0 then action_adj=2;
if use=1 and action2=1 and operate2^=0  then action_adj=1;
if action_adj=. then action_adj=0;
run;

proc sort data=want2;
by city_id action_adj;
run;

data want3;
set want2;
by city_id action_adj;
if last.city_id;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Tue, 03 Dec 2019 06:25:43 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Creating-a-variable-based-on-another-dataset/m-p/608936#M177252</guid>
      <dc:creator>Jagadishkatam</dc:creator>
      <dc:date>2019-12-03T06:25:43Z</dc:date>
    </item>
    <item>
      <title>Re: Creating a variable based on another dataset</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Creating-a-variable-based-on-another-dataset/m-p/608951#M177259</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/13162"&gt;@TrueTears&lt;/a&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Thank you for posting sample data as working data steps and also for taking your time and explain the logic you need with samples and desired result.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Below some code which produces the desired result.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
  input year firm_id city_id operate action;
  cards;
2008 28013 1003 1 1
2008 28013 1004 1 1
2008 28013 1007 1 0
2008 28013 1009 1 1
2008 28013 1010 0 1
2008 28013 1013 1 0
2008 28013 1053 1 1
2008 28013 1074 1 0
2008 28013 1075 1 1
;

data list;
  input city_id city_id_adj use;
  cards;
1003 1009 1
1003 1010 1
1003 1085 1
1004 1007 0
1004 1009 0
1004 1099 0
1004 1100 0
1007 1004 0
1007 1823 1
1009 1003 1
1009 1004 0
1009 1010 0
1010 1003 1
1010 1009 0
1013 1053 1
1013 1074 1
1053 1013 1
1074 1013 1
1075 1743 1
1075 1744 1
;

data want(keep=year firm_id city_id operate action action_adj);
  if _n_=1 then
    do;
      if 0 then set list;
      dcl hash list(dataset:'list(where=(use=1))', multidata:'y');
      list.defineKey('city_id');
      list.defineData('city_id_adj');
      list.defineDone();

      dcl hash Operate0(dataset:'have(keep=city_id operate where=(operate=0))');
      Operate0.defineKey('city_id');
      Operate0.defineDone();

      dcl hash Action1(dataset:'have(keep=city_id action where=(action=1))');
      Action1.defineKey('city_id');
      Action1.defineDone();
    end;
  call missing(of _all_);

  set have;

  /* If operate = 0, then action_adj = 3 */
  if operate = 0 then 
    do;
      action_adj = 3;
      output;
      return;
    end;

  /* For each city_id in have, we check its corresponding city_id_adj in dataset "list" that has use=1 */
  if list.check()=0 then
    do;
      /* If in this set of city_id_adj, there is one that has operate = 0 in the dataset "have", then action_adj = 2 */
      list.reset_dup();
      do while(list.do_over()=0);
        if Operate0.check(key:city_id_adj)=0 then
          do;
            action_adj = 2;
            output;
            return;
          end;
      end;

      /* action_adj = 1 if there is at least one city_id_adj (with use=1) that has action = 1 in the dataset "have". */
      list.reset_dup();
      do while(list.do_over()=0);
        if Action1.check(key:city_id_adj)=0 then
          do;
            action_adj = 1;
            output;
            return;
          end;
      end;

    end;

  /* action_adj = 0 for all other cases */
  action_adj = 0;
  output;
  return;

run;

proc print;
run;

&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Tue, 03 Dec 2019 07:13:38 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Creating-a-variable-based-on-another-dataset/m-p/608951#M177259</guid>
      <dc:creator>Patrick</dc:creator>
      <dc:date>2019-12-03T07:13:38Z</dc:date>
    </item>
    <item>
      <title>Re: Creating a variable based on another dataset</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Creating-a-variable-based-on-another-dataset/m-p/608985#M177281</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/12447"&gt;@Patrick&lt;/a&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Thank you! I tried your code, it seems to work for one particular year and firm_id pair but does not seem to work for multiple. Sorry that I did not add additional year and firm_id in my original post. Consider the following extended "have" dataset:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;data have;
  input year firm_id city_id operate action;
  cards;
2008 28013 1003 1 1
2008 28013 1004 1 1
2008 28013 1007 1 0
2008 28013 1009 1 1
2008 28013 1010 0 1
2008 28013 1013 1 0
2008 28013 1053 1 1
2008 28013 1074 1 0
2008 28013 1075 1 1
2009 28332 1003 1 0
2009 28332 1010 1 0
2009 28332 1013 0 1
2009 28332 1053 1 0
2009 28332 1074 1 1
2009 28332 1075 1 1

;&lt;/PRE&gt;
&lt;P&gt;I added in additional year 2009 and another firm_id 28332. The "list" dataset always remains the same. Now "want" becomes:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;data want;
input year firm_id city_id operate action action_adj;
cards;
2008 28013 1003 1 1 2
2008 28013 1004 1 1 0
2008 28013 1007 1 0 0
2008 28013 1009 1 1 1
2008 28013 1010 0 1 3
2008 28013 1013 1 0 1
2008 28013 1053 1 1 0
2008 28013 1074 1 0 0
2008 28013 1075 1 1 0
2009 28332 1003 1 0 0
2009 28332 1010 1 0 0
2009 28332 1013 0 1 3
2009 28332 1053 1 0 2
2009 28332 1074 1 1 2
2009 28332 1075 1 1 0

;
run;&lt;/PRE&gt;
&lt;P&gt;However, your code gives the following (which is almost correct):&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;data want;
input year firm_id city_id operate action action_adj;
cards;
2008 28013 1003 1 1 2
2008 28013 1004 1 1 0
2008 28013 1007 1 0 0
2008 28013 1009 1 1 1
2008 28013 1010 0 1 3
2008 28013 1013 1 0 1
2008 28013 1053 1 1 0
2008 28013 1074 1 0 0
2008 28013 1075 1 1 0
2009 28332 1003 1 0 2
2009 28332 1010 1 0 1
2009 28332 1013 0 1 3
2009 28332 1053 1 0 2
2009 28332 1074 1 1 2
2009 28332 1075 1 1 0

;
run;&lt;/PRE&gt;
&lt;P&gt;The first difference is for year 2009, firm_id 28332, and city_id 1003 which should have action_adj = 0 rather than action_adj = 2. This is because in the list, city_id 1003 corresponds to 1009, 1010, and 1085, but although 1010 has operate=0 for the year/firm_id pair 2008/28013, it has operate=1 for the year/firm_id pair 2009/28332.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The second difference is for year 2009, firm_id 28332, and city_id 1010 which should have action_adj=0 rather than action_adj=1. This is because city_id 1010 corresponds to 1003 and although 1003 has action=1 for year/firm_id 2008/28013, it has action=0 for year/firm_id 2009/28332.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Could you slightly alter your code so the conditions that I outlined in my original post is applied for every year/firm_id pair and not for the entire dataset?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Thank you so very much.&lt;/P&gt;</description>
      <pubDate>Tue, 03 Dec 2019 10:09:13 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Creating-a-variable-based-on-another-dataset/m-p/608985#M177281</guid>
      <dc:creator>TrueTears</dc:creator>
      <dc:date>2019-12-03T10:09:13Z</dc:date>
    </item>
    <item>
      <title>Re: Creating a variable based on another dataset</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Creating-a-variable-based-on-another-dataset/m-p/608996#M177285</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/12447"&gt;@Patrick&lt;/a&gt;&lt;/P&gt;
&lt;P&gt;&lt;BR /&gt;Thank you! I tried your code, it seems to work for one particular year and firm_id pair but does not seem to work for multiple. Sorry that I did not add additional year and firm_id in my original post. Consider the following extended "have" dataset:&lt;/P&gt;
&lt;P&gt;&lt;BR /&gt;data have;&lt;BR /&gt;input year firm_id city_id operate action;&lt;BR /&gt;cards;&lt;BR /&gt;2008 28013 1003 1 1&lt;BR /&gt;2008 28013 1004 1 1&lt;BR /&gt;2008 28013 1007 1 0&lt;BR /&gt;2008 28013 1009 1 1&lt;BR /&gt;2008 28013 1010 0 1&lt;BR /&gt;2008 28013 1013 1 0&lt;BR /&gt;2008 28013 1053 1 1&lt;BR /&gt;2008 28013 1074 1 0&lt;BR /&gt;2008 28013 1075 1 1&lt;BR /&gt;2009 28332 1003 1 0&lt;BR /&gt;2009 28332 1010 1 0&lt;BR /&gt;2009 28332 1013 0 1&lt;BR /&gt;2009 28332 1053 1 0&lt;BR /&gt;2009 28332 1074 1 1&lt;BR /&gt;2009 28332 1075 1 1&lt;/P&gt;
&lt;P&gt;;&lt;/P&gt;
&lt;P&gt;I added in additional year 2009 and another firm_id 28332. The "list" dataset always remains the same. Now "want" becomes:&lt;/P&gt;
&lt;P&gt;&lt;BR /&gt;data want;&lt;BR /&gt;input year firm_id city_id operate action action_adj;&lt;BR /&gt;cards;&lt;BR /&gt;2008 28013 1003 1 1 2&lt;BR /&gt;2008 28013 1004 1 1 0&lt;BR /&gt;2008 28013 1007 1 0 0&lt;BR /&gt;2008 28013 1009 1 1 1&lt;BR /&gt;2008 28013 1010 0 1 3&lt;BR /&gt;2008 28013 1013 1 0 1&lt;BR /&gt;2008 28013 1053 1 1 0&lt;BR /&gt;2008 28013 1074 1 0 0&lt;BR /&gt;2008 28013 1075 1 1 0&lt;BR /&gt;2009 28332 1003 1 0 0&lt;BR /&gt;2009 28332 1010 1 0 0&lt;BR /&gt;2009 28332 1013 0 1 3&lt;BR /&gt;2009 28332 1053 1 0 2&lt;BR /&gt;2009 28332 1074 1 1 2&lt;BR /&gt;2009 28332 1075 1 1 0&lt;/P&gt;
&lt;P&gt;;&lt;BR /&gt;run;&lt;/P&gt;
&lt;P&gt;However, your code gives the following (which is almost correct):&lt;/P&gt;
&lt;P&gt;&lt;BR /&gt;data want;&lt;BR /&gt;input year firm_id city_id operate action action_adj;&lt;BR /&gt;cards;&lt;BR /&gt;2008 28013 1003 1 1 2&lt;BR /&gt;2008 28013 1004 1 1 0&lt;BR /&gt;2008 28013 1007 1 0 0&lt;BR /&gt;2008 28013 1009 1 1 1&lt;BR /&gt;2008 28013 1010 0 1 3&lt;BR /&gt;2008 28013 1013 1 0 1&lt;BR /&gt;2008 28013 1053 1 1 0&lt;BR /&gt;2008 28013 1074 1 0 0&lt;BR /&gt;2008 28013 1075 1 1 0&lt;BR /&gt;2009 28332 1003 1 0 2&lt;BR /&gt;2009 28332 1010 1 0 1&lt;BR /&gt;2009 28332 1013 0 1 3&lt;BR /&gt;2009 28332 1053 1 0 2&lt;BR /&gt;2009 28332 1074 1 1 2&lt;BR /&gt;2009 28332 1075 1 1 0&lt;/P&gt;
&lt;P&gt;;&lt;BR /&gt;run;&lt;/P&gt;
&lt;P&gt;The first difference is for year 2009, firm_id 28332, and city_id 1003 which should have action_adj = 0 rather than action_adj = 2. This is because in the list, city_id 1003 corresponds to 1009, 1010, and 1085, but although 1010 has operate=0 for the year/firm_id pair 2008/28013, it has operate=1 for the year/firm_id pair 2009/28332.&lt;/P&gt;
&lt;P&gt;&lt;BR /&gt;The second difference is for year 2009, firm_id 28332, and city_id 1010 which should have action_adj=0 rather than action_adj=1. This is because city_id 1010 corresponds to 1003 and although 1003 has action=1 for year/firm_id 2008/28013, it has action=0 for year/firm_id 2009/28332.&lt;/P&gt;
&lt;P&gt;&lt;BR /&gt;Could you slightly alter your code so the conditions that I outlined in my original post is applied for every year/firm_id pair and not for the entire dataset?&lt;/P&gt;
&lt;P&gt;&lt;BR /&gt;Thank you so very much.&lt;/P&gt;</description>
      <pubDate>Tue, 03 Dec 2019 10:20:04 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Creating-a-variable-based-on-another-dataset/m-p/608996#M177285</guid>
      <dc:creator>TrueTears</dc:creator>
      <dc:date>2019-12-03T10:20:04Z</dc:date>
    </item>
    <item>
      <title>Re: Creating a variable based on another dataset</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Creating-a-variable-based-on-another-dataset/m-p/608997#M177286</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/12447"&gt;@Patrick&lt;/a&gt; Thank you! I tried your code, it seems to work for one particular year and firm_id pair but does not seem to work for multiple. Sorry that I did not add additional year and firm_id in my original post. Consider the following extended "have" dataset:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
  input year firm_id city_id operate action;
  cards;
2008 28013 1003 1 1
2008 28013 1004 1 1
2008 28013 1007 1 0
2008 28013 1009 1 1
2008 28013 1010 0 1
2008 28013 1013 1 0
2008 28013 1053 1 1
2008 28013 1074 1 0
2008 28013 1075 1 1
2009 28332 1003 1 0
2009 28332 1010 1 0
2009 28332 1013 0 1
2009 28332 1053 1 0
2009 28332 1074 1 1
2009 28332 1075 1 1

;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;I added in additional year 2009 and another firm_id 28332. The "list" dataset always remains the same. Now "want" becomes:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;
data want;
input year firm_id city_id operate action action_adj;
cards;
2008 28013 1003 1 1 2
2008 28013 1004 1 1 0
2008 28013 1007 1 0 0
2008 28013 1009 1 1 1
2008 28013 1010 0 1 3
2008 28013 1013 1 0 1
2008 28013 1053 1 1 0
2008 28013 1074 1 0 0
2008 28013 1075 1 1 0
2009 28332 1003 1 0 0
2009 28332 1010 1 0 0
2009 28332 1013 0 1 3
2009 28332 1053 1 0 2
2009 28332 1074 1 1 2
2009 28332 1075 1 1 0

;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;However, your code gives the following (which is almost correct):&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;
data want;
input year firm_id city_id operate action action_adj;
cards;
2008 28013 1003 1 1 2
2008 28013 1004 1 1 0
2008 28013 1007 1 0 0
2008 28013 1009 1 1 1
2008 28013 1010 0 1 3
2008 28013 1013 1 0 1
2008 28013 1053 1 1 0
2008 28013 1074 1 0 0
2008 28013 1075 1 1 0
2009 28332 1003 1 0 2
2009 28332 1010 1 0 1
2009 28332 1013 0 1 3
2009 28332 1053 1 0 2
2009 28332 1074 1 1 2
2009 28332 1075 1 1 0

;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;The first difference is for year 2009, firm_id 28332, and city_id 1003 which should have action_adj = 0 rather than action_adj = 2. This is because in the list, city_id 1003 corresponds to 1009, 1010, and 1085, but although 1010 has operate=0 for the year/firm_id pair 2008/28013, it has operate=1 for the year/firm_id pair 2009/28332.&lt;/P&gt;
&lt;P&gt;&lt;BR /&gt;The second difference is for year 2009, firm_id 28332, and city_id 1010 which should have action_adj=0 rather than action_adj=1. This is because city_id 1010 corresponds to 1003 and although 1003 has action=1 for year/firm_id 2008/28013, it has action=0 for year/firm_id 2009/28332.&lt;/P&gt;
&lt;P&gt;&lt;BR /&gt;Could you slightly alter your code so the conditions that I outlined in my original post is applied for every year/firm_id pair and not for the entire dataset?&lt;/P&gt;
&lt;P&gt;&lt;BR /&gt;Thank you so very much.&lt;/P&gt;</description>
      <pubDate>Tue, 03 Dec 2019 10:25:09 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Creating-a-variable-based-on-another-dataset/m-p/608997#M177286</guid>
      <dc:creator>TrueTears</dc:creator>
      <dc:date>2019-12-03T10:25:09Z</dc:date>
    </item>
    <item>
      <title>Re: Creating a variable based on another dataset</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Creating-a-variable-based-on-another-dataset/m-p/609195#M177355</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/13162"&gt;@TrueTears&lt;/a&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Ideally you try and understand the code to a level where you can make such a change yourself. But here you go.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;
data want(keep=year firm_id city_id operate action action_adj);
  if _n_=1 then
    do;
      if 0 then set list;
      dcl hash list(dataset:'list(where=(use=1))', multidata:'y');
      list.defineKey('city_id');
      list.defineData('city_id_adj');
      list.defineDone();

      dcl hash Operate0(dataset:'have(keep=year city_id operate where=(operate=0))');
      Operate0.defineKey('year','city_id');
      Operate0.defineDone();

      dcl hash Action1(dataset:'have(keep=year city_id action where=(action=1))');
      Action1.defineKey('year','city_id');
      Action1.defineDone();
    end;
  call missing(of _all_);

  set have;

  /* If operate = 0, then action_adj = 3 */
  if operate = 0 then 
    do;
      action_adj = 3;
      output;
      return;
    end;

  /* For each city_id in have, we check its corresponding city_id_adj in dataset "list" that has use=1 */
  if list.check()=0 then
    do;
      /* If in this set of city_id_adj, there is one that has operate = 0 in the dataset "have", then action_adj = 2 */
      list.reset_dup();
      do while(list.do_over()=0);
        if Operate0.check(key:year, key:city_id_adj)=0 then
          do;
            action_adj = 2;
            output;
            return;
          end;
      end;

      /* action_adj = 1 if there is at least one city_id_adj (with use=1) that has action = 1 in the dataset "have". */
      list.reset_dup();
      do while(list.do_over()=0);
        if Action1.check(key:year, key:city_id_adj)=0 then
          do;
            action_adj = 1;
            output;
            return;
          end;
      end;

    end;

  /* action_adj = 0 for all other cases */
  action_adj = 0;
  output;
  return;

run;
&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Tue, 03 Dec 2019 21:46:42 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Creating-a-variable-based-on-another-dataset/m-p/609195#M177355</guid>
      <dc:creator>Patrick</dc:creator>
      <dc:date>2019-12-03T21:46:42Z</dc:date>
    </item>
  </channel>
</rss>

