<?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: Flag the next value of a variable in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Flag-the-next-value-of-a-variable/m-p/870634#M343870</link>
    <description>&lt;P&gt;Thanks. I was trying to avoid having to define the type and/or length of LAST by having the first place it appears be the assignment statement and missed that potential erasure.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Wed, 19 Apr 2023 19:56:41 GMT</pubDate>
    <dc:creator>Tom</dc:creator>
    <dc:date>2023-04-19T19:56:41Z</dc:date>
    <item>
      <title>Flag the next value of a variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Flag-the-next-value-of-a-variable/m-p/870575#M343838</link>
      <description>&lt;P&gt;Hello SAS community,&lt;/P&gt;
&lt;P&gt;I have a dataset that looks like the below, basically an id variable to flag different entities the then another variable that takes different values (can be one of four values: missing, Open,Char or Clos). For each id variable that data are sorted by time (i have a "proper" date variable, but here i include it as a number for simplicity).&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;BR /&gt;data have;&lt;BR /&gt;input id $ x $ time ;&lt;BR /&gt;cards;&lt;BR /&gt;1 Open 1&lt;BR /&gt;1 Open 2 &lt;BR /&gt;1 Clos 3&lt;BR /&gt;1 Clos 4&lt;BR /&gt;1 Clos 5&lt;BR /&gt;1 Open 6&lt;BR /&gt;1 Open 7&lt;BR /&gt;2 Open 1&lt;BR /&gt;2 Clos 2 &lt;BR /&gt;2 Char 3&lt;BR /&gt;2 Char 4&lt;BR /&gt;;;;;&lt;BR /&gt;run;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I would like to create another variable y that tells me (within each id) what is the next value of x when x is equal to "Clos", that is not "Clos", like below. So this variable takes a value when x is clos, telling you what is the next non-clos value within a given id.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Thank you all very much.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;data want;&lt;BR /&gt;input id $ x $ time y $ ;&lt;BR /&gt;cards;&lt;BR /&gt;1 Open 1 .&lt;BR /&gt;1 Open 2 . &lt;BR /&gt;1 Clos 3 open&lt;BR /&gt;1 Clos 4 open&lt;BR /&gt;1 Clos 5 open&lt;BR /&gt;1 Open 6 .&lt;BR /&gt;1 Open 7 .&lt;BR /&gt;2 Open 1 .&lt;BR /&gt;2 Clos 2 char&lt;BR /&gt;2 Char 3 .&lt;BR /&gt;2 Char 4 .&lt;BR /&gt;;;;;&lt;BR /&gt;run;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 19 Apr 2023 15:04:37 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Flag-the-next-value-of-a-variable/m-p/870575#M343838</guid>
      <dc:creator>costasRO</dc:creator>
      <dc:date>2023-04-19T15:04:37Z</dc:date>
    </item>
    <item>
      <title>Re: Flag the next value of a variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Flag-the-next-value-of-a-variable/m-p/870582#M343841</link>
      <description>&lt;P&gt;I cannot figure out what real world problem would want something like this.&amp;nbsp; Can you explain what you are trying to do?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;In general it is much easier to REMEMBER something than it is to predict the FUTURE.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Sort the data in descending order and use a RETAINed variable to remember the latest non CLOS value.&amp;nbsp; You will want to a second non-retained variable to output.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
  input id $ x $ time ;
  row+1;
cards4;
1 Open 1
1 Open 2
1 Clos 3
1 Clos 4
1 Clos 5
1 Open 6
1 Open 7
2 Open 1
2 Clos 2
2 Char 3
2 Char 4
;;;;

proc sort;
  by id descending row ;
run;

data want;
  set have;
  by id;
  if x ne 'Clos' then last=X;
  retain last;
  if first.id then call missing(last);
  if x = 'Clos' then want=last;
  drop last;
run;

proc sort;
  by id row;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Result&lt;/P&gt;
&lt;PRE&gt;Obs    id     x      time    row    want

  1    1     Open      1       1
  2    1     Open      2       2
  3    1     Clos      3       3    Open
  4    1     Clos      4       4    Open
  5    1     Clos      5       5    Open
  6    1     Open      6       6
  7    1     Open      7       7
  8    2     Open      1       8
  9    2     Clos      2       9    Char
 10    2     Char      3      10
 11    2     Char      4      11
&lt;/PRE&gt;</description>
      <pubDate>Wed, 19 Apr 2023 15:33:26 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Flag-the-next-value-of-a-variable/m-p/870582#M343841</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2023-04-19T15:33:26Z</dc:date>
    </item>
    <item>
      <title>Re: Flag the next value of a variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Flag-the-next-value-of-a-variable/m-p/870612#M343854</link>
      <description>&lt;P&gt;Great idea,&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/159"&gt;@Tom&lt;/a&gt;, and so much easier than other look-ahead techniques.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Small correction: I think &lt;FONT face="courier new,courier"&gt;call missing(last)&lt;/FONT&gt;&amp;nbsp;in observations where&amp;nbsp;&lt;FONT face="courier new,courier"&gt;first.id&lt;/FONT&gt; should be restricted to cases with&amp;nbsp;&lt;FONT face="courier new,courier"&gt;x='Clos'&lt;/FONT&gt;&amp;nbsp;to avoid losing a few values for variable &lt;FONT face="courier new,courier"&gt;want&lt;/FONT&gt;&amp;nbsp;(example: the sample dataset HAVE with the last observation removed).&lt;/P&gt;
&lt;PRE&gt;  if x ne 'Clos' then last=x;
  &lt;STRONG&gt;else do;&lt;/STRONG&gt; 
    if first.id then call missing(last);
    want=last;
  &lt;STRONG&gt;end;&lt;/STRONG&gt;&lt;/PRE&gt;
&lt;P&gt;Or, alternatively, set &lt;FONT face="courier new,courier"&gt;last&lt;/FONT&gt; to missing in the &lt;EM&gt;last&lt;/EM&gt; observation of each BY group:&lt;/P&gt;
&lt;PRE&gt;  if x ne 'Clos' then last=x;
  else want=last;
  if &lt;STRONG&gt;last&lt;/STRONG&gt;.id then call missing(last);&lt;/PRE&gt;</description>
      <pubDate>Wed, 19 Apr 2023 18:00:28 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Flag-the-next-value-of-a-variable/m-p/870612#M343854</guid>
      <dc:creator>FreelanceReinh</dc:creator>
      <dc:date>2023-04-19T18:00:28Z</dc:date>
    </item>
    <item>
      <title>Re: Flag the next value of a variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Flag-the-next-value-of-a-variable/m-p/870634#M343870</link>
      <description>&lt;P&gt;Thanks. I was trying to avoid having to define the type and/or length of LAST by having the first place it appears be the assignment statement and missed that potential erasure.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 19 Apr 2023 19:56:41 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Flag-the-next-value-of-a-variable/m-p/870634#M343870</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2023-04-19T19:56:41Z</dc:date>
    </item>
    <item>
      <title>Re: Flag the next value of a variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Flag-the-next-value-of-a-variable/m-p/870750#M343939</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;/*
I think Tom's code is the best way to do.
But for having some fun.
*/

data have;
  input id $ x $ time ;
cards4;
1 Open 1
1 Open 2
1 Clos 3
1 Clos 4
1 Clos 5
1 Open 6
1 Open 7
2 Open 1
2 Clos 2
2 Char 3
2 Char 4
;;;;

data want ;
   if 0 then set have;
   if 0 then set have(rename=(x=y));
   if 0 then set have(rename=(x=want));
   declare hash h(ordered:'y');
   declare hiter hi('h');
   h.definekey('time');
   h.definedata('y');
   h.definedone();

do until(last.id);
 set have;
 by id;
 h.add(key:time,data:x);
end;
do until(last.id);
 set have;
 by id;
 call missing(want);
 if x='Clos' then do;
 rc=hi.setcur(key:time);
 do while(rc=0);
   if y ne 'Clos' then do;want=y;leave;end;
   rc=hi.next();
 end;
 end;
 output;
end;
h.delete();hi.delete();&lt;BR /&gt;drop y rc;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Fri, 21 Apr 2023 11:49:43 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Flag-the-next-value-of-a-variable/m-p/870750#M343939</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2023-04-21T11:49:43Z</dc:date>
    </item>
    <item>
      <title>Re: Flag the next value of a variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Flag-the-next-value-of-a-variable/m-p/870764#M343950</link>
      <description>&lt;P&gt;Thanks&amp;nbsp;everyone&amp;nbsp;for the codes, they all work perfectly. Regards&lt;/P&gt;</description>
      <pubDate>Thu, 20 Apr 2023 12:56:43 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Flag-the-next-value-of-a-variable/m-p/870764#M343950</guid>
      <dc:creator>costasRO</dc:creator>
      <dc:date>2023-04-20T12:56:43Z</dc:date>
    </item>
  </channel>
</rss>

