<?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: How can i achieve the value based on last YEAR (previous row)? in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/How-can-i-achieve-the-value-based-on-last-YEAR-previous-row/m-p/962182#M375046</link>
    <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data mohx_data;
input MOHX_ID $ MOHX_YEAR MOHX_SEQ MOHX_ENDST NC;
datalines;
A 2023 1 0 60 
A 2023 2 0 30 
A 2024 1 0 60 
A 2025 1 0 60 
B 2023 1 0 25 
B 2023 2 0 35 
B 2024 1 0 45 
B 2025 1 0 60 
;
run;
data want;
 if _n_=1 then do;
   if 0 then set mohx_data(rename=(NC=PREV_YEAR_NC));
   declare hash h(dataset:'mohx_data(rename=(NC=PREV_YEAR_NC))');
   h.definekey('MOHX_ID', 'MOHX_YEAR', 'MOHX_SEQ');
   h.definedata('PREV_YEAR_NC');
   h.definedone();
 end;
set mohx_data;
call missing(PREV_YEAR_NC);
prev_year=MOHX_YEAR-1;
rc=h.find(key:MOHX_ID,key:prev_year,key:MOHX_SEQ);
drop prev_year rc;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
    <pubDate>Wed, 19 Mar 2025 07:10:51 GMT</pubDate>
    <dc:creator>Ksharp</dc:creator>
    <dc:date>2025-03-19T07:10:51Z</dc:date>
    <item>
      <title>How can i achieve the value based on last YEAR (previous row)?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-can-i-achieve-the-value-based-on-last-YEAR-previous-row/m-p/962169#M375039</link>
      <description>&lt;P&gt;How can i achieve the value based on last YEAR (previous row)?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I have a set of data with the Key&lt;/P&gt;&lt;TABLE&gt;&lt;TBODY&gt;&lt;TR&gt;&lt;TD&gt;MOHX_ID&lt;/TD&gt;&lt;TD&gt;MOHX_YEAR&lt;/TD&gt;&lt;TD&gt;MOHX_SEQ&lt;/TD&gt;&lt;TD&gt;MOHX_ENDST&lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;With the key listed above, i wish to get the new field "PREV_YEAR_NC" based on previous year's NC value.&lt;/P&gt;&lt;P&gt;&lt;BR /&gt;Sample as below. Notice red color value is what i want to achieve.&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="StickyRoll_0-1742356552203.png" style="width: 400px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/105497i96396465445485AA/image-size/medium?v=v2&amp;amp;px=400" role="button" title="StickyRoll_0-1742356552203.png" alt="StickyRoll_0-1742356552203.png" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Final table:&lt;BR /&gt;data mohx_data;&lt;BR /&gt;input MOHX_ID $ MOHX_YEAR MOHX_SEQ MOHX_ENDST NC PREV_YEAR_NC;&lt;BR /&gt;datalines;&lt;BR /&gt;A 2023 1 0 60 .&lt;BR /&gt;A 2023 2 0 30 .&lt;BR /&gt;A 2024 1 0 60 60&lt;BR /&gt;A 2025 1 0 60 60&lt;BR /&gt;B 2023 1 0 25 .&lt;BR /&gt;B 2023 2 0 35 .&lt;BR /&gt;B 2024 1 0 45 25&lt;BR /&gt;B 2025 1 0 60 45&lt;BR /&gt;;&lt;BR /&gt;run;&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;/P&gt;</description>
      <pubDate>Wed, 19 Mar 2025 03:59:26 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-can-i-achieve-the-value-based-on-last-YEAR-previous-row/m-p/962169#M375039</guid>
      <dc:creator>StickyRoll</dc:creator>
      <dc:date>2025-03-19T03:59:26Z</dc:date>
    </item>
    <item>
      <title>Re: How can i achieve the value based on last YEAR (previous row)?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-can-i-achieve-the-value-based-on-last-YEAR-previous-row/m-p/962170#M375040</link>
      <description>&lt;P&gt;Hi &lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/376676"&gt;@StickyRoll&lt;/a&gt;, it looks like its not alway the value of the prvious row you are looking for. So what is the complete rule?&lt;/P&gt;
&lt;P&gt;Best, Markus&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 19 Mar 2025 04:10:29 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-can-i-achieve-the-value-based-on-last-YEAR-previous-row/m-p/962170#M375040</guid>
      <dc:creator>MarkusWeick</dc:creator>
      <dc:date>2025-03-19T04:10:29Z</dc:date>
    </item>
    <item>
      <title>Re: How can i achieve the value based on last YEAR (previous row)?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-can-i-achieve-the-value-based-on-last-YEAR-previous-row/m-p/962176#M375043</link>
      <description>&lt;P&gt;Hello. It is always based on last year + key&lt;/P&gt;&lt;TABLE&gt;&lt;TBODY&gt;&lt;TR&gt;&lt;TD&gt;MOHX_ID&lt;/TD&gt;&lt;TD&gt;MOHX_YEAR&lt;/TD&gt;&lt;TD&gt;MOHX_SEQ&lt;/TD&gt;&lt;TD&gt;MOHX_ENDST&lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 19 Mar 2025 05:35:17 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-can-i-achieve-the-value-based-on-last-YEAR-previous-row/m-p/962176#M375043</guid>
      <dc:creator>StickyRoll</dc:creator>
      <dc:date>2025-03-19T05:35:17Z</dc:date>
    </item>
    <item>
      <title>Re: How can i achieve the value based on last YEAR (previous row)?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-can-i-achieve-the-value-based-on-last-YEAR-previous-row/m-p/962177#M375044</link>
      <description>&lt;P&gt;Hi &lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/376676"&gt;@StickyRoll&lt;/a&gt;, and what is the rule by which the key combination, for which the value should be selected, is chosen?&lt;/P&gt;</description>
      <pubDate>Wed, 19 Mar 2025 05:46:47 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-can-i-achieve-the-value-based-on-last-YEAR-previous-row/m-p/962177#M375044</guid>
      <dc:creator>MarkusWeick</dc:creator>
      <dc:date>2025-03-19T05:46:47Z</dc:date>
    </item>
    <item>
      <title>Re: How can i achieve the value based on last YEAR (previous row)?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-can-i-achieve-the-value-based-on-last-YEAR-previous-row/m-p/962182#M375046</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data mohx_data;
input MOHX_ID $ MOHX_YEAR MOHX_SEQ MOHX_ENDST NC;
datalines;
A 2023 1 0 60 
A 2023 2 0 30 
A 2024 1 0 60 
A 2025 1 0 60 
B 2023 1 0 25 
B 2023 2 0 35 
B 2024 1 0 45 
B 2025 1 0 60 
;
run;
data want;
 if _n_=1 then do;
   if 0 then set mohx_data(rename=(NC=PREV_YEAR_NC));
   declare hash h(dataset:'mohx_data(rename=(NC=PREV_YEAR_NC))');
   h.definekey('MOHX_ID', 'MOHX_YEAR', 'MOHX_SEQ');
   h.definedata('PREV_YEAR_NC');
   h.definedone();
 end;
set mohx_data;
call missing(PREV_YEAR_NC);
prev_year=MOHX_YEAR-1;
rc=h.find(key:MOHX_ID,key:prev_year,key:MOHX_SEQ);
drop prev_year rc;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 19 Mar 2025 07:10:51 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-can-i-achieve-the-value-based-on-last-YEAR-previous-row/m-p/962182#M375046</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2025-03-19T07:10:51Z</dc:date>
    </item>
    <item>
      <title>Re: How can i achieve the value based on last YEAR (previous row)?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-can-i-achieve-the-value-based-on-last-YEAR-previous-row/m-p/962262#M375063</link>
      <description>&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;This is a good use case for embedding a LAG function inside an IFN (or IFC) function, as in:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data mohx_data;
input MOHX_ID $ MOHX_YEAR MOHX_SEQ MOHX_ENDST NC;
datalines;
A 2023 1 0 60 
A 2023 2 0 30 
A 2024 1 0 60 
A 2025 1 0 60 
B 2023 1 0 25 
B 2023 2 0 35 
B 2024 1 0 45 
B 2025 1 0 60 
run;

data want;
  set mohx_data;
  by mohx_id mohx_year;
  retain prv_nc .;
  if first.mohx_year then prv_nc=ifn(first.mohx_id,.,lag(nc));
run;

&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;I assume when there are multiple obs for a single mohx_year (and therefore multiple possible NC values), that you want to carry forward the NC value for the first of those multiple obs.&lt;/P&gt;</description>
      <pubDate>Wed, 19 Mar 2025 17:30:03 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-can-i-achieve-the-value-based-on-last-YEAR-previous-row/m-p/962262#M375063</guid>
      <dc:creator>mkeintz</dc:creator>
      <dc:date>2025-03-19T17:30:03Z</dc:date>
    </item>
    <item>
      <title>Re: How can i achieve the value based on last YEAR (previous row)?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-can-i-achieve-the-value-based-on-last-YEAR-previous-row/m-p/962332#M375084</link>
      <description>&lt;P&gt;When you start a new YEAR remember the previous year's value.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;if first.mohx_year then newvar=lag(NC);
retain newvar;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;But when you start a new ID forget it again.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;  if first.mohx_id then call missing(newvar);&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;So data step will look like this:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
  set mohx_data;
  by mohx_id mohx_year;
  if first.mohx_year then newvar=lag(NC);
  if first.mohx_id then call missing(newvar);
  retain newvar;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Results&lt;/P&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Tom_0-1742476966992.png" style="width: 999px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/105558i515A66D38C0A0F22/image-size/large?v=v2&amp;amp;px=999" role="button" title="Tom_0-1742476966992.png" alt="Tom_0-1742476966992.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 20 Mar 2025 13:22:57 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-can-i-achieve-the-value-based-on-last-YEAR-previous-row/m-p/962332#M375084</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2025-03-20T13:22:57Z</dc:date>
    </item>
    <item>
      <title>Re: How can i achieve the value based on last YEAR (previous row)?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-can-i-achieve-the-value-based-on-last-YEAR-previous-row/m-p/962351#M375093</link>
      <description>&lt;P&gt;Thanks everyone. All the solution here works. I wish i could mark all the answers as solution.&lt;/P&gt;</description>
      <pubDate>Fri, 21 Mar 2025 01:54:10 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-can-i-achieve-the-value-based-on-last-YEAR-previous-row/m-p/962351#M375093</guid>
      <dc:creator>StickyRoll</dc:creator>
      <dc:date>2025-03-21T01:54:10Z</dc:date>
    </item>
  </channel>
</rss>

