<?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: Copy value from next observation in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Copy-value-from-next-observation/m-p/349353#M81014</link>
    <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data hlp1;
   set have;
   retain hlp_group;
   if mod(_N_,2) then hlp_group+1;
run;
proc sort data=hlp1 out=hlp2; by hlp_group id d; run;

data hlp3;
   set hlp2;
   by hlp_group id d;
   hlp_lag1_value=lag1(value);
   if not first.hlp_group then value=hlp_lag1_value;
run;

PROC SORT DATA=hlp3 out=want(keep=id d value); by hlp_group id descending d;run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
    <pubDate>Wed, 12 Apr 2017 09:49:03 GMT</pubDate>
    <dc:creator>Oligolas</dc:creator>
    <dc:date>2017-04-12T09:49:03Z</dc:date>
    <item>
      <title>Copy value from next observation</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Copy-value-from-next-observation/m-p/349347#M81011</link>
      <description />
      <pubDate>Wed, 12 Apr 2017 09:58:36 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Copy-value-from-next-observation/m-p/349347#M81011</guid>
      <dc:creator>ari</dc:creator>
      <dc:date>2017-04-12T09:58:36Z</dc:date>
    </item>
    <item>
      <title>Re: Copy value from next observation</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Copy-value-from-next-observation/m-p/349349#M81012</link>
      <description>&lt;P&gt;Hi,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Well, you can hold the data over with retain, and then control the output using output statement as given below. &amp;nbsp;However I would advise you to get more ata there to help. &amp;nbsp;As it stands with what you have provided there is not enough information to do this properly, i.e. a change in sort alters that data completely. &amp;nbsp;Normally on these type of questions you would reverse sort the data, then retain last value, much simpler and robust. &amp;nbsp;So get something in the data which indicates the order and make your life easier.&lt;/P&gt;
&lt;PRE&gt;data have;
  input id d value;
datalines;
1 1 80
1 0 100
1 1 100
1 0 120
1 1 30
1 0 50
;
run;

data want;
  set have end=last;
  retain out_id out_d out_value;
  if _n_=1 then do;
    out_id=id;
    out_d=d;
    out_value=value;
  end;
  else do;
    if d=0 and out_d=1 then do;
      out_value=value;
      output;
    end;
    else output;
  end;
  if last then output;
run;
&lt;/PRE&gt;</description>
      <pubDate>Wed, 12 Apr 2017 09:42:06 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Copy-value-from-next-observation/m-p/349349#M81012</guid>
      <dc:creator>RW9</dc:creator>
      <dc:date>2017-04-12T09:42:06Z</dc:date>
    </item>
    <item>
      <title>Re: Copy value from next observation</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Copy-value-from-next-observation/m-p/349353#M81014</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data hlp1;
   set have;
   retain hlp_group;
   if mod(_N_,2) then hlp_group+1;
run;
proc sort data=hlp1 out=hlp2; by hlp_group id d; run;

data hlp3;
   set hlp2;
   by hlp_group id d;
   hlp_lag1_value=lag1(value);
   if not first.hlp_group then value=hlp_lag1_value;
run;

PROC SORT DATA=hlp3 out=want(keep=id d value); by hlp_group id descending d;run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 12 Apr 2017 09:49:03 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Copy-value-from-next-observation/m-p/349353#M81014</guid>
      <dc:creator>Oligolas</dc:creator>
      <dc:date>2017-04-12T09:49:03Z</dc:date>
    </item>
    <item>
      <title>Re: Copy value from next observation</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Copy-value-from-next-observation/m-p/349356#M81016</link>
      <description>Thanks RW. Yes reverse sorting and retaining is the best solution.</description>
      <pubDate>Wed, 12 Apr 2017 09:59:35 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Copy-value-from-next-observation/m-p/349356#M81016</guid>
      <dc:creator>ari</dc:creator>
      <dc:date>2017-04-12T09:59:35Z</dc:date>
    </item>
  </channel>
</rss>

