<?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: Retrieve value from observation of previous period in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Retrieve-value-from-observation-of-previous-period/m-p/631819#M187227</link>
    <description>&lt;P&gt;Thank you very much for your answer. This works fine, if one has to check only the previous period (which I stated in my question). However, my actual problem is a little bit more complicated and I have to check different time periods (t-1, t-2, t-3, but also t+1 and t+2). I didn't mention this before, since I wanted to state the question as easily as possible. And in this situation, I cannot figure out how to generalize your solution in such a situation.&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Fri, 13 Mar 2020 11:29:50 GMT</pubDate>
    <dc:creator>as_methodology</dc:creator>
    <dc:date>2020-03-13T11:29:50Z</dc:date>
    <item>
      <title>Retrieve value from observation of previous period</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Retrieve-value-from-observation-of-previous-period/m-p/631805#M187220</link>
      <description>&lt;P&gt;Hello everyone,&lt;/P&gt;&lt;P&gt;&lt;BR /&gt;A couple of weeks ago I asked a question on how to identify whether an observation was present in different period.&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/31304"&gt;@PeterClemmensen&lt;/a&gt;&amp;nbsp; 's answer was very useful and I have been successfully been working with it:&lt;/P&gt;&lt;P&gt;&lt;BR /&gt;&lt;A href="http://%20https://communities.sas.com/t5/SAS-Programming/Identify-whether-observation-was-present-in-different-period/m-p/621088#" target="_self"&gt;https://communities.sas.com/t5/SAS-Programming/Identify-whether-observation-was-present-in-different-period/m-p/621088#&lt;/A&gt;&lt;/P&gt;&lt;P&gt;&lt;BR /&gt;Now, the issue arose that I do not only need to know whether the observation was present in the previous period, but also to retrieve a value from that observation.&lt;/P&gt;&lt;P&gt;&lt;BR /&gt;The data and the solution from my previous post looks somwhat like this:&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
input year id value;
datalines;
2000 1 123
2000 2 456
2000 3 789
2000 4 012
2000 5 345
2001 1 678
2001 2 901
2001 3 234
2001 6 567
2001 7 890
2001 8 987
2002 1 654
2002 2 321
2002 6 098
2002 7 765
2002 9 432
2002 10 109
;
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, 0);
run;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;Now I want to retrieve the value form "value". So for example in year 2002, it shoud return the value "678" for ID "1", "901" for "2" and so on.&lt;/P&gt;&lt;P&gt;&lt;BR /&gt;I am aware that I have to add&lt;/P&gt;&lt;P&gt;&lt;BR /&gt;h.definedata ("value");&lt;/P&gt;&lt;P&gt;&lt;BR /&gt;to the statement. But I cannot find out, how to call the value afterwards.&lt;/P&gt;&lt;P&gt;&lt;BR /&gt;Thank you very much already for your help!&lt;/P&gt;</description>
      <pubDate>Fri, 13 Mar 2020 09:51:14 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Retrieve-value-from-observation-of-previous-period/m-p/631805#M187220</guid>
      <dc:creator>as_methodology</dc:creator>
      <dc:date>2020-03-13T09:51:14Z</dc:date>
    </item>
    <item>
      <title>Re: Retrieve value from observation of previous period</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Retrieve-value-from-observation-of-previous-period/m-p/631808#M187222</link>
      <description>&lt;P&gt;Where do you want the 'lookup' value to go? Replacing the original value? If so, then simply do&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.definedata   ("value");
        h.definedone   ();
    end;

    set have;

    present = h.find (key : year-1, key : id) = 0;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Fri, 13 Mar 2020 10:15:21 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Retrieve-value-from-observation-of-previous-period/m-p/631808#M187222</guid>
      <dc:creator>PeterClemmensen</dc:creator>
      <dc:date>2020-03-13T10:15:21Z</dc:date>
    </item>
    <item>
      <title>Re: Retrieve value from observation of previous period</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Retrieve-value-from-observation-of-previous-period/m-p/631810#M187223</link>
      <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/233979"&gt;@as_methodology&lt;/a&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Here is a way to achieve this using PROC SQL:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql;
	select a.*,
		   case when b.value &amp;gt; 0 then 1 else 0 end as present,
		   b.value as prev_value
	from have as a left join have as b
	on a.id = b.id and a.year = (b.year+1)
	order by year, id;
quit;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Hope this helps!&lt;/P&gt;
&lt;P&gt;Best,&lt;/P&gt;</description>
      <pubDate>Fri, 13 Mar 2020 10:16:38 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Retrieve-value-from-observation-of-previous-period/m-p/631810#M187223</guid>
      <dc:creator>ed_sas_member</dc:creator>
      <dc:date>2020-03-13T10:16:38Z</dc:date>
    </item>
    <item>
      <title>Re: Retrieve value from observation of previous period</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Retrieve-value-from-observation-of-previous-period/m-p/631811#M187224</link>
      <description>&lt;P&gt;Hi &lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/31304"&gt;@PeterClemmensen&lt;/a&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thanks for your answer. It should create a new variable, for example in the column "present". So instead for "1" it should return the value from the previous period.&lt;/P&gt;</description>
      <pubDate>Fri, 13 Mar 2020 10:17:36 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Retrieve-value-from-observation-of-previous-period/m-p/631811#M187224</guid>
      <dc:creator>as_methodology</dc:creator>
      <dc:date>2020-03-13T10:17:36Z</dc:date>
    </item>
    <item>
      <title>Re: Retrieve value from observation of previous period</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Retrieve-value-from-observation-of-previous-period/m-p/631813#M187226</link>
      <description>&lt;P&gt;Ok. No problem&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(rename=value=lagvalue");
        h.definekey    ("year", "id");
        h.definedata   ("lagvalue");
        h.definedone   ();
    end;

    set have;
    lagvalue = .;

    present = h.find (key : year-1, key : id) = 0;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Fri, 13 Mar 2020 10:25:00 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Retrieve-value-from-observation-of-previous-period/m-p/631813#M187226</guid>
      <dc:creator>PeterClemmensen</dc:creator>
      <dc:date>2020-03-13T10:25:00Z</dc:date>
    </item>
    <item>
      <title>Re: Retrieve value from observation of previous period</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Retrieve-value-from-observation-of-previous-period/m-p/631819#M187227</link>
      <description>&lt;P&gt;Thank you very much for your answer. This works fine, if one has to check only the previous period (which I stated in my question). However, my actual problem is a little bit more complicated and I have to check different time periods (t-1, t-2, t-3, but also t+1 and t+2). I didn't mention this before, since I wanted to state the question as easily as possible. And in this situation, I cannot figure out how to generalize your solution in such a situation.&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 13 Mar 2020 11:29:50 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Retrieve-value-from-observation-of-previous-period/m-p/631819#M187227</guid>
      <dc:creator>as_methodology</dc:creator>
      <dc:date>2020-03-13T11:29:50Z</dc:date>
    </item>
    <item>
      <title>Re: Retrieve value from observation of previous period</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Retrieve-value-from-observation-of-previous-period/m-p/631823#M187229</link>
      <description>&lt;P&gt;Here is a general solution, that looks up different time periods and assigns the values to different variables.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Feel free to ask &lt;span class="lia-unicode-emoji" title=":slightly_smiling_face:"&gt;🙂&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want (drop=rc v vt t);
    if _N_=1 then do;
        declare hash h (dataset : "have(rename=value=v");
        h.definekey    ("year", "id");
        h.definedata   ("v");
        h.definedone   ();
    end;

    set have;
    v = .;
    array vv {-3 : 2} vtminus3 vtminus2 vtminus1 vt vtplus1 vtplus2;

    do t = -3 to 2;
        rc = h.find (key : year + t, key : id);
        vv [t] = v;
    end;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 13 Mar 2020 11:48:35 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Retrieve-value-from-observation-of-previous-period/m-p/631823#M187229</guid>
      <dc:creator>PeterClemmensen</dc:creator>
      <dc:date>2020-03-13T11:48:35Z</dc:date>
    </item>
    <item>
      <title>Re: Retrieve value from observation of previous period</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Retrieve-value-from-observation-of-previous-period/m-p/631830#M187230</link>
      <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/233979"&gt;@as_methodology&lt;/a&gt;&amp;nbsp; Good morning, Can you please post the expected output/input sample that you mentioned seems complicated. For some reason, I am unable to comprehend the thread well. You may write a brief description along with the samples. Thank you!&lt;/P&gt;</description>
      <pubDate>Fri, 13 Mar 2020 12:16:34 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Retrieve-value-from-observation-of-previous-period/m-p/631830#M187230</guid>
      <dc:creator>novinosrin</dc:creator>
      <dc:date>2020-03-13T12:16:34Z</dc:date>
    </item>
    <item>
      <title>Re: Retrieve value from observation of previous period</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Retrieve-value-from-observation-of-previous-period/m-p/634966#M188490</link>
      <description>&lt;P&gt;So, after the office changed into home-office mode, I finally get to thank &lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/31304"&gt;@PeterClemmensen&lt;/a&gt; for your help! This has made my life so much simpler! And I already included it in my process.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;As a quick feedback to you: When including it, I noticed that the code looks for a value in the future, say t+2, and it cannot find one because it does not exist, it will return the last value it was able to find, in this case t+1. Surprinsingly, this works fine for values in the past.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I used the solution you gave to my previous post to work around it, and it works perfectly. So, thank you very much again!&lt;/P&gt;</description>
      <pubDate>Thu, 26 Mar 2020 07:13:25 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Retrieve-value-from-observation-of-previous-period/m-p/634966#M188490</guid>
      <dc:creator>as_methodology</dc:creator>
      <dc:date>2020-03-26T07:13:25Z</dc:date>
    </item>
    <item>
      <title>Re: Retrieve value from observation of previous period</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Retrieve-value-from-observation-of-previous-period/m-p/634967#M188491</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/292097"&gt;@ed_sas_member&lt;/a&gt;&amp;nbsp;Thank you very much! This also works very nice. And I was even able to generalize it...&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;PROC SQL;
   CREATE TABLE WORK.WANT AS 
   SELECT t1.year, 
          t1.id, 
          t1.value, 
          t2.id AS idTM1, 
          t2.value AS valueTM1, 
          t3.id AS idTM2, 
          t3.value AS valueTM2,
		  t4.id AS idTP1, 
          t4.value AS valueTP1,
		  t5.id AS idTP2, 
          t5.value AS valueTP2
      FROM WORK.HAVE t1
           LEFT JOIN WORK.HAVE t2 ON (t1.year-1 = t2.year) AND (t1.id = t2.id)
           LEFT JOIN WORK.HAVE t3 ON (t1.year-2 = t3.year) AND (t1.id = t3.id)
		   LEFT JOIN WORK.HAVE t4 ON (t1.year+1 = t4.year) AND (t1.id = t4.id)
		   LEFT JOIN WORK.HAVE t5 ON (t1.year+2 = t5.year) AND (t1.id = t5.id)
      ORDER BY t1.year,
               t1.id;
QUIT;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Thu, 26 Mar 2020 07:57:18 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Retrieve-value-from-observation-of-previous-period/m-p/634967#M188491</guid>
      <dc:creator>as_methodology</dc:creator>
      <dc:date>2020-03-26T07:57:18Z</dc:date>
    </item>
    <item>
      <title>Re: Retrieve value from observation of previous period</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Retrieve-value-from-observation-of-previous-period/m-p/634969#M188492</link>
      <description>&lt;P&gt;hi &lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/138205"&gt;@novinosrin&lt;/a&gt; Thank you very much for your answer. The basic question was to find out what the value of the observation was in a different time period (e.g. turnover of an enterprise in the previous period) and get that information, which is currently stored in a different line, in the same line. As the answers from the others show, it should look like this&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="as_methodology_0-1585209614080.png" style="width: 400px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/37385iFA2F9D1FF500D7E4/image-size/medium?v=v2&amp;amp;px=400" role="button" title="as_methodology_0-1585209614080.png" alt="as_methodology_0-1585209614080.png" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 26 Mar 2020 08:01:19 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Retrieve-value-from-observation-of-previous-period/m-p/634969#M188492</guid>
      <dc:creator>as_methodology</dc:creator>
      <dc:date>2020-03-26T08:01:19Z</dc:date>
    </item>
    <item>
      <title>Re: Retrieve value from observation of previous period</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Retrieve-value-from-observation-of-previous-period/m-p/634975#M188493</link>
      <description>&lt;P&gt;Thank you for your feedback &lt;span class="lia-unicode-emoji" title=":slightly_smiling_face:"&gt;🙂&lt;/span&gt; And glad the code work for you. Please remember to close the thread. This helps future users navigate the community.&lt;/P&gt;</description>
      <pubDate>Thu, 26 Mar 2020 08:36:03 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Retrieve-value-from-observation-of-previous-period/m-p/634975#M188493</guid>
      <dc:creator>PeterClemmensen</dc:creator>
      <dc:date>2020-03-26T08:36:03Z</dc:date>
    </item>
  </channel>
</rss>

