<?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: Identify whether observation was present in different period in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Identify-whether-observation-was-present-in-different-period/m-p/621087#M182541</link>
    <description>&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/31304"&gt;@PeterClemmensen&lt;/a&gt; Works perfectly. Thank you very much!</description>
    <pubDate>Thu, 30 Jan 2020 09:32:38 GMT</pubDate>
    <dc:creator>as_methodology</dc:creator>
    <dc:date>2020-01-30T09:32:38Z</dc:date>
    <item>
      <title>Identify whether observation was present in different period</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Identify-whether-observation-was-present-in-different-period/m-p/620114#M182169</link>
      <description>&lt;P&gt;Hello,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I have a dataset that includes an ID and year that looks somewhat like this:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;data have;
input year id;
datalines;
2000 1
2000 2
2000 3
2000 4
2000 5
2001 1
2001 2
2001 3
2001 6
2001 7
2001 8
2002 1
2002 2
2002 6
2002 7
2002 9
2002 10
;
run;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Now, I want a way to check whether an obervation was present in the previous period returning "1", if it did, and "0" otherwise. So for example in year 2002, it shoud return the value "1" for ID "1", "2", "6" and "7" and "0" for the ID "9" and "10".&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I have been tinkering around with it, but have not yet reached a conclusion.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thank you very much for your help.&lt;/P&gt;</description>
      <pubDate>Mon, 27 Jan 2020 06:42:34 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Identify-whether-observation-was-present-in-different-period/m-p/620114#M182169</guid>
      <dc:creator>as_methodology</dc:creator>
      <dc:date>2020-01-27T06:42:34Z</dc:date>
    </item>
    <item>
      <title>Re: Identify whether observation was present in different period</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Identify-whether-observation-was-present-in-different-period/m-p/620117#M182171</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/233979"&gt;@as_methodology&lt;/a&gt;&amp;nbsp;should we only consider the previous period? If we consider ID=1 in 2002, should we only look for ID=1 in 2001 or all preceding periods (years)?&lt;/P&gt;</description>
      <pubDate>Mon, 27 Jan 2020 07:20:32 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Identify-whether-observation-was-present-in-different-period/m-p/620117#M182171</guid>
      <dc:creator>PeterClemmensen</dc:creator>
      <dc:date>2020-01-27T07:20:32Z</dc:date>
    </item>
    <item>
      <title>Re: Identify whether observation was present in different period</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Identify-whether-observation-was-present-in-different-period/m-p/620118#M182172</link>
      <description>&lt;P&gt;Hi draycut&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thanks for the reply. I will only need to check whether it was present in the previous period.&lt;/P&gt;</description>
      <pubDate>Mon, 27 Jan 2020 07:25:09 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Identify-whether-observation-was-present-in-different-period/m-p/620118#M182172</guid>
      <dc:creator>as_methodology</dc:creator>
      <dc:date>2020-01-27T07:25:09Z</dc:date>
    </item>
    <item>
      <title>Re: Identify whether observation was present in different period</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Identify-whether-observation-was-present-in-different-period/m-p/620121#M182174</link>
      <description>&lt;P&gt;The hash object is your friend&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
input year id;
datalines;
2000 1
2000 2
2000 3
2000 4
2000 5
2001 1
2001 2
2001 3
2001 6
2001 7
2001 8
2002 1
2002 2
2002 6
2002 7
2002 9
2002 10
;
run;

data want;
    if _N_=1 then do;
        declare hash h (dataset : "have");
        h.definekey ("year", "id");
        h.definedone();
    end;

    set have;

    present = ifn(h.check (key : year-1, key : id), 0, 1);
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Result:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;year  id  present 
2000  1   0 
2000  2   0 
2000  3   0 
2000  4   0 
2000  5   0 
2001  1   1 
2001  2   1 
2001  3   1 
2001  6   0 
2001  7   0 
2001  8   0 
2002  1   1 
2002  2   1 
2002  6   1 
2002  7   1 
2002  9   0 
2002  10  0 &lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 27 Jan 2020 08:04:25 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Identify-whether-observation-was-present-in-different-period/m-p/620121#M182174</guid>
      <dc:creator>PeterClemmensen</dc:creator>
      <dc:date>2020-01-27T08:04:25Z</dc:date>
    </item>
    <item>
      <title>Re: Identify whether observation was present in different period</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Identify-whether-observation-was-present-in-different-period/m-p/620122#M182175</link>
      <description>&lt;P&gt;Please let us know if this worked for you.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
input year id;
datalines;
2000 1
2000 2
2000 3
2000 4
2000 5
2001 1
2001 2
2001 3
2001 6
2001 7
2001 8
2002 1
2002 2
2002 6
2002 7
2002 9
2002 10
;
run;

proc sort data=have;
by id year;
run;

data want;
set have;
by id year;
flag=1;
if first.id = last.id and first.id and first.id then flag=0;
/*else if first.id then flag=0;*/		/*un-comment if necessary*/
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Mon, 27 Jan 2020 08:05:54 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Identify-whether-observation-was-present-in-different-period/m-p/620122#M182175</guid>
      <dc:creator>Satish_Parida</dc:creator>
      <dc:date>2020-01-27T08:05:54Z</dc:date>
    </item>
    <item>
      <title>Re: Identify whether observation was present in different period</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Identify-whether-observation-was-present-in-different-period/m-p/620158#M182189</link>
      <description>&lt;BLOCKQUOTE&gt;
&lt;P&gt;The hash object is your friend&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;LI-CODE lang="sas"&gt;&lt;SPAN class="token procnames"&gt;data&lt;/SPAN&gt; have&lt;SPAN class="token punctuation"&gt;;&lt;/SPAN&gt;
&lt;SPAN class="token keyword"&gt;input&lt;/SPAN&gt; &lt;SPAN class="token function"&gt;year&lt;/SPAN&gt; &lt;SPAN class="token keyword"&gt;id&lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;;&lt;/SPAN&gt;
&lt;SPAN class="token datalines"&gt;&lt;SPAN class="token keyword"&gt;datalines&lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;;&lt;/SPAN&gt;&lt;SPAN class="token data string"&gt;
2000 1
2000 2
2000 3
2000 4
2000 5
2001 1
2001 2
2001 3
2001 6
2001 7
2001 8
2002 1
2002 2
2002 6
2002 7
2002 9
2002 10
&lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;;&lt;/SPAN&gt;&lt;/SPAN&gt;
&lt;SPAN class="token procnames"&gt;run&lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;;&lt;/SPAN&gt;

&lt;SPAN class="token procnames"&gt;data&lt;/SPAN&gt; want&lt;SPAN class="token punctuation"&gt;;&lt;/SPAN&gt;
    &lt;SPAN class="token keyword"&gt;if&lt;/SPAN&gt; _N_&lt;SPAN class="token operator"&gt;=&lt;/SPAN&gt;&lt;SPAN class="token number"&gt;1&lt;/SPAN&gt; &lt;SPAN class="token keyword"&gt;then&lt;/SPAN&gt; do&lt;SPAN class="token punctuation"&gt;;&lt;/SPAN&gt;
        &lt;SPAN class="token keyword"&gt;declare&lt;/SPAN&gt; hash h &lt;SPAN class="token punctuation"&gt;(&lt;/SPAN&gt;dataset : &lt;SPAN class="token string"&gt;"have"&lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;)&lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;;&lt;/SPAN&gt;
        h&lt;SPAN class="token punctuation"&gt;.&lt;/SPAN&gt;definekey &lt;SPAN class="token punctuation"&gt;(&lt;/SPAN&gt;&lt;SPAN class="token string"&gt;"year"&lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;,&lt;/SPAN&gt; &lt;SPAN class="token string"&gt;"id"&lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;)&lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;;&lt;/SPAN&gt;
        h&lt;SPAN class="token punctuation"&gt;.&lt;/SPAN&gt;definedone&lt;SPAN class="token punctuation"&gt;(&lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;)&lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;;&lt;/SPAN&gt;
    end&lt;SPAN class="token punctuation"&gt;;&lt;/SPAN&gt;

    &lt;SPAN class="token keyword"&gt;set&lt;/SPAN&gt; have&lt;SPAN class="token punctuation"&gt;;&lt;/SPAN&gt;

    present &lt;SPAN class="token operator"&gt;=&lt;/SPAN&gt; &lt;SPAN class="token function"&gt;ifn&lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;(&lt;/SPAN&gt;h&lt;SPAN class="token punctuation"&gt;.&lt;/SPAN&gt;check &lt;SPAN class="token punctuation"&gt;(&lt;/SPAN&gt;key : &lt;SPAN class="token function"&gt;year&lt;/SPAN&gt;&lt;SPAN class="token number"&gt;-1&lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;,&lt;/SPAN&gt; key : &lt;SPAN class="token keyword"&gt;id&lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;)&lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;,&lt;/SPAN&gt; &lt;SPAN class="token number"&gt;0&lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;,&lt;/SPAN&gt; &lt;SPAN class="token number"&gt;1&lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;)&lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;;&lt;/SPAN&gt;
&lt;SPAN class="token procnames"&gt;run&lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;;&lt;/SPAN&gt;&lt;/LI-CODE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Result:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;year  id  present 
2000  1   0 
2000  2   0 
2000  3   0 
2000  4   0 
2000  5   0 
2001  1   1 
2001  2   1 
2001  3   1 
2001  6   0 
2001  7   0 
2001  8   0 
2002  1   1 
2002  2   1 
2002  6   1 
2002  7   1 
2002  9   0 
2002  10  0 &lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;/BLOCKQUOTE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Hi &lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/31304"&gt;@PeterClemmensen&lt;/a&gt;&amp;nbsp;, I liked the solution you posted, but when I try running your two data steps, my "present" column is always set to 1. It looks to me like it should work, as you have shown with your output.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;For me, moving the &lt;FONT face="courier new,courier"&gt;check&lt;/FONT&gt; method to a previous line and testing the result in the &lt;FONT face="courier new,courier"&gt;ifn()&lt;/FONT&gt; function produces the same result as your solution, i.e.:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
    if _N_=1 then do;
        declare hash h (dataset : "have");
        h.definekey ("year", "id");
        h.definedone();
    end;

    set have;

    rc      = h.check (key : year-1, key : id);
    present = ifn(rc, 0, 1);
run;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;The logs are clean when running either version of the code, so the only thing I can think of is SAS version; I'm running on 9.1.3, which version are you using?&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;Thanks &amp;amp; kind regards,&lt;/P&gt;
&lt;P&gt;Amir.&lt;/P&gt;</description>
      <pubDate>Mon, 27 Jan 2020 13:57:24 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Identify-whether-observation-was-present-in-different-period/m-p/620158#M182189</guid>
      <dc:creator>Amir</dc:creator>
      <dc:date>2020-01-27T13:57:24Z</dc:date>
    </item>
    <item>
      <title>Re: Identify whether observation was present in different period</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Identify-whether-observation-was-present-in-different-period/m-p/620163#M182192</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/22588"&gt;@Amir&lt;/a&gt;&amp;nbsp;thank you.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Just to be a little self-critical looking at my own code, I like this better&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
    if _N_=1 then do;
        declare hash h (dataset : "have");
        h.definekey ("year", "id");
        h.definedone();
    end;

    set have;

    present = ifn(h.check (key : year-1, key : id) = 0, 1, 0);
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;I think it makes it clearer that a successful search should return a 1 to present and a non-succesful search should return zero.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;As to your question, I am not sure. I run SAS 9.4Mx, but I do not have SAS available right now. I think you are right though, that this is a version thing. Quite a few differences exist between SAS 9.1 and later versions in the hash field.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/21262"&gt;@hashman&lt;/a&gt;&amp;nbsp;can probably guide you in the right direction &lt;span class="lia-unicode-emoji" title=":slightly_smiling_face:"&gt;🙂&lt;/span&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 27 Jan 2020 14:18:26 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Identify-whether-observation-was-present-in-different-period/m-p/620163#M182192</guid>
      <dc:creator>PeterClemmensen</dc:creator>
      <dc:date>2020-01-27T14:18:26Z</dc:date>
    </item>
    <item>
      <title>Re: Identify whether observation was present in different period</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Identify-whether-observation-was-present-in-different-period/m-p/620182#M182199</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/31304"&gt;@PeterClemmensen&lt;/a&gt;, thanks for the response. I was also thinking of an alternative for that same line of code you amended, which is how I discovered the anomaly.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I was going to try / suggest the following, but kept on getting "1" for "present":&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;    present = h.check (key : year-1, key : id) eq 0;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Kind regards,&lt;/P&gt;
&lt;P&gt;Amir.&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 27 Jan 2020 14:42:31 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Identify-whether-observation-was-present-in-different-period/m-p/620182#M182199</guid>
      <dc:creator>Amir</dc:creator>
      <dc:date>2020-01-27T14:42:31Z</dc:date>
    </item>
    <item>
      <title>Re: Identify whether observation was present in different period</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Identify-whether-observation-was-present-in-different-period/m-p/621087#M182541</link>
      <description>&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/31304"&gt;@PeterClemmensen&lt;/a&gt; Works perfectly. Thank you very much!</description>
      <pubDate>Thu, 30 Jan 2020 09:32:38 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Identify-whether-observation-was-present-in-different-period/m-p/621087#M182541</guid>
      <dc:creator>as_methodology</dc:creator>
      <dc:date>2020-01-30T09:32:38Z</dc:date>
    </item>
    <item>
      <title>Re: Identify whether observation was present in different period</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Identify-whether-observation-was-present-in-different-period/m-p/621088#M182542</link>
      <description>&lt;P&gt;Anytime &lt;span class="lia-unicode-emoji" title=":slightly_smiling_face:"&gt;🙂&lt;/span&gt;&lt;/P&gt;</description>
      <pubDate>Thu, 30 Jan 2020 09:36:42 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Identify-whether-observation-was-present-in-different-period/m-p/621088#M182542</guid>
      <dc:creator>PeterClemmensen</dc:creator>
      <dc:date>2020-01-30T09:36:42Z</dc:date>
    </item>
  </channel>
</rss>

