<?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 Create a variable based on another dataset in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Create-a-variable-based-on-another-dataset/m-p/363655#M86147</link>
    <description>&lt;P&gt;I have 2 datasets, call them data1 and data2. They both have the same set of variables, one of them being ID.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;data2 is a subset of data1. I want to create a variable in data1 that takes on the value of 1 when the ID variable is present in data2, and 0 if not in data2.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;What is the most efficient way to do this?&lt;/P&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>Fri, 02 Jun 2017 00:18:02 GMT</pubDate>
    <dc:creator>Melk</dc:creator>
    <dc:date>2017-06-02T00:18:02Z</dc:date>
    <item>
      <title>Create a variable based on another dataset</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Create-a-variable-based-on-another-dataset/m-p/363655#M86147</link>
      <description>&lt;P&gt;I have 2 datasets, call them data1 and data2. They both have the same set of variables, one of them being ID.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;data2 is a subset of data1. I want to create a variable in data1 that takes on the value of 1 when the ID variable is present in data2, and 0 if not in data2.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;What is the most efficient way to do this?&lt;/P&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>Fri, 02 Jun 2017 00:18:02 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Create-a-variable-based-on-another-dataset/m-p/363655#M86147</guid>
      <dc:creator>Melk</dc:creator>
      <dc:date>2017-06-02T00:18:02Z</dc:date>
    </item>
    <item>
      <title>Re: Create a variable based on another dataset</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Create-a-variable-based-on-another-dataset/m-p/363659#M86151</link>
      <description>&lt;P&gt;If needed sort datasets by ID then use merge:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
merge data1(in=in1)
      data2(in=in2 keep=ID)
 ;  by ID;
       if in1 and in2 then flag=1;
       else flag=2;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Fri, 02 Jun 2017 00:32:18 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Create-a-variable-based-on-another-dataset/m-p/363659#M86151</guid>
      <dc:creator>Shmuel</dc:creator>
      <dc:date>2017-06-02T00:32:18Z</dc:date>
    </item>
    <item>
      <title>Re: Create a variable based on another dataset</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Create-a-variable-based-on-another-dataset/m-p/363684#M86168</link>
      <description>&lt;P&gt;Efficient means no sorting.&lt;/P&gt;
&lt;P&gt;Like this ?&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data WANT; 
  if _N_ = 1 then do;  
    declare hash VET(dataset: "DATA2"); 
    VET.definekey("KEYVAR"); 
    VET.definedone();
  end; 
  set DATA1; 
  FLAG=VAT.check();
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;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 02 Jun 2017 03:08:01 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Create-a-variable-based-on-another-dataset/m-p/363684#M86168</guid>
      <dc:creator>ChrisNZ</dc:creator>
      <dc:date>2017-06-02T03:08:01Z</dc:date>
    </item>
    <item>
      <title>Re: Create a variable based on another dataset</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Create-a-variable-based-on-another-dataset/m-p/363706#M86171</link>
      <description>&lt;P&gt;The function check returns 0 if the key was found in the hash object.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;FLAG = (VAT.check() = 0);&lt;/P&gt;</description>
      <pubDate>Fri, 02 Jun 2017 07:43:03 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Create-a-variable-based-on-another-dataset/m-p/363706#M86171</guid>
      <dc:creator>andreas_lds</dc:creator>
      <dc:date>2017-06-02T07:43:03Z</dc:date>
    </item>
    <item>
      <title>Re: Create a variable based on another dataset</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Create-a-variable-based-on-another-dataset/m-p/363998#M86264</link>
      <description>True &lt;span class="lia-unicode-emoji" title=":slightly_smiling_face:"&gt;🙂&lt;/span&gt;</description>
      <pubDate>Sat, 03 Jun 2017 10:09:35 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Create-a-variable-based-on-another-dataset/m-p/363998#M86264</guid>
      <dc:creator>ChrisNZ</dc:creator>
      <dc:date>2017-06-03T10:09:35Z</dc:date>
    </item>
    <item>
      <title>Re: Create a variable based on another dataset</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Create-a-variable-based-on-another-dataset/m-p/364794#M86554</link>
      <description>&lt;P&gt;Hello - thanks for the response. I tried this and the merged dataset has duplicate observations. Is there a way to just add a variable, flag, to data1, with value 1 if the ID variable is in data2, an value 0 if not? (without doing a merge)?&lt;/P&gt;</description>
      <pubDate>Tue, 06 Jun 2017 23:17:55 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Create-a-variable-based-on-another-dataset/m-p/364794#M86554</guid>
      <dc:creator>Melk</dc:creator>
      <dc:date>2017-06-06T23:17:55Z</dc:date>
    </item>
    <item>
      <title>Re: Create a variable based on another dataset</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Create-a-variable-based-on-another-dataset/m-p/364797#M86557</link>
      <description>&lt;P&gt;Thank you both! worked perfectly with the edit from andreas.&lt;/P&gt;</description>
      <pubDate>Tue, 06 Jun 2017 23:22:34 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Create-a-variable-based-on-another-dataset/m-p/364797#M86557</guid>
      <dc:creator>Melk</dc:creator>
      <dc:date>2017-06-06T23:22:34Z</dc:date>
    </item>
  </channel>
</rss>

