<?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: Find next variable by group in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Find-next-variable-by-group/m-p/948085#M371032</link>
    <description>&lt;P&gt;Yes,&amp;nbsp;&lt;SPAN&gt;the first record have to be RECEPTION. I didn't specify it and it's true that it's important. So your solution will work&amp;nbsp;&lt;span class="lia-unicode-emoji" title=":slightly_smiling_face:"&gt;🙂&lt;/span&gt; Thanks !&lt;/SPAN&gt;&lt;/P&gt;</description>
    <pubDate>Fri, 18 Oct 2024 16:35:07 GMT</pubDate>
    <dc:creator>sasuser_8</dc:creator>
    <dc:date>2024-10-18T16:35:07Z</dc:date>
    <item>
      <title>Find next variable by group</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Find-next-variable-by-group/m-p/947891#M370988</link>
      <description>&lt;P&gt;Hi,&lt;/P&gt;
&lt;P&gt;I have this code:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;DATA SALE;
LENGTH FILE $3 DEPARTMENT $10 ;
INPUT FILE DATE:YYMMDD10. DEPARTMENT $;
FORMAT DATE YYMMDD10.;
DATALINES;

102 2024-01-15 RECEPTION
102 2024-01-30 RECEPTION
102 2024-02-10 SHOES
102 2024-02-29 RECEPTION 
102 2024-02-29 SHOES
103 2024-01-22 RECEPTION
103 2024-02-15 RECEPTION
103 2024-02-20 RECEPTION
103 2024-03-02 PANTS
104 2024-04-12 RECEPTION
104 2024-05-02 RECEPTION
104 2024-05-20 RECEPTION
104 2024-05-30 SHIRTS
104 2024-06-14 SHIRTS
104 2024-06-30 SHIRTS 
;
RUN;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;I would like to find, for each file, the first department where the item was transferred after &lt;EM&gt;Reception.&lt;/EM&gt; For that example, the result would be as follows:&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="result.jpg" style="width: 340px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/101391i2E6BEED6D918924D/image-size/large?v=v2&amp;amp;px=999" role="button" title="result.jpg" alt="result.jpg" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;Does anyone know how to proceed ?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Thanks&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 17 Oct 2024 15:24:38 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Find-next-variable-by-group/m-p/947891#M370988</guid>
      <dc:creator>sasuser_8</dc:creator>
      <dc:date>2024-10-17T15:24:38Z</dc:date>
    </item>
    <item>
      <title>Re: Find next variable by group</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Find-next-variable-by-group/m-p/947911#M370992</link>
      <description>&lt;P&gt;This might work but I doubt you example data covers all scenarios.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;DATA SALE;
LENGTH FILE $3 DEPARTMENT $10 ;
INPUT FILE DATE:YYMMDD10. DEPARTMENT $;
FORMAT DATE YYMMDD10.;
DATALINES;
102 2024-01-15 RECEPTION
102 2024-01-30 RECEPTION
102 2024-02-10 SHOES
102 2024-02-29 RECEPTION 
102 2024-02-29 SHOES
103 2024-01-22 RECEPTION
103 2024-02-15 RECEPTION
103 2024-02-20 RECEPTION
103 2024-03-02 PANTS
104 2024-04-12 RECEPTION
104 2024-05-02 RECEPTION
104 2024-05-20 RECEPTION
104 2024-05-30 SHIRTS
104 2024-06-14 SHIRTS
104 2024-06-30 SHIRTS 
;
RUN;

data sale;
   set sale;
   by file;
   ddif = dif(date);
   if first.file then ddif=.;
   run;
proc print;
   run;
proc summary data=sale nway;
   by file;
   where department ne: 'REC' and ddif ne 0;
   class department;
   output out=test(drop=_:) idgroup(min(ddif) out[1](date ddif)=);
   run;
proc print;
   run;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Capture.PNG" style="width: 271px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/101398iFB6CB70DB20E2107/image-size/large?v=v2&amp;amp;px=999" role="button" title="Capture.PNG" alt="Capture.PNG" /&gt;&lt;/span&gt;&lt;/P&gt;</description>
      <pubDate>Thu, 17 Oct 2024 16:58:44 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Find-next-variable-by-group/m-p/947911#M370992</guid>
      <dc:creator>data_null__</dc:creator>
      <dc:date>2024-10-17T16:58:44Z</dc:date>
    </item>
    <item>
      <title>Re: Find next variable by group</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Find-next-variable-by-group/m-p/947970#M371003</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
set sales,,
by file;
length next_department $10 date_transfer 8;
format date_transfer yymmdd10.;
retain next_department date_transfer;
if first.file
then do;
  next_department = "";
  date_transfer = .;
end;
if department ne "RECEPTION" and next_department = ""
then do;
  next_department = department;
  date_transfer = date;
end;
if last.file;
keep file next_department date_transfer;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Untested, posted from my tablet.&lt;/P&gt;</description>
      <pubDate>Thu, 17 Oct 2024 21:06:56 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Find-next-variable-by-group/m-p/947970#M371003</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2024-10-17T21:06:56Z</dc:date>
    </item>
    <item>
      <title>Re: Find next variable by group</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Find-next-variable-by-group/m-p/947972#M371004</link>
      <description>&lt;P&gt;Do you mean you want the first observation (that is not "RECEPTION') for each FILE?&amp;nbsp; That does not sound like it needs to do anything with NEXT observation.&amp;nbsp; Which makes it easier since it is easier to remember something than predict the future.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
  set sale;
  by file date ;
  if first.file then any=0;
  retain any;
  if department ne 'RECEPTION' then do;
    if not any then output;
    any=1;
  end;
  drop any;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Result&lt;/P&gt;
&lt;PRE&gt;Obs    FILE    DEPARTMENT          DATE

 1     102       SHOES       2024-02-10
 2     103       PANTS       2024-03-02
 3     104       SHIRTS      2024-05-30

&lt;/PRE&gt;</description>
      <pubDate>Thu, 17 Oct 2024 21:54:31 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Find-next-variable-by-group/m-p/947972#M371004</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2024-10-17T21:54:31Z</dc:date>
    </item>
    <item>
      <title>Re: Find next variable by group</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Find-next-variable-by-group/m-p/947976#M371005</link>
      <description>&lt;P&gt;Given that your observations are in date order for each FILE, this should be easy:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
   set SALE;
   by file;
   where department ne "RECEPTION";
   if first.file;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;The WHERE statement performs subsetting before first.file gets created.&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 17 Oct 2024 23:06:20 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Find-next-variable-by-group/m-p/947976#M371005</guid>
      <dc:creator>Astounding</dc:creator>
      <dc:date>2024-10-17T23:06:20Z</dc:date>
    </item>
    <item>
      <title>Re: Find next variable by group</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Find-next-variable-by-group/m-p/947977#M371006</link>
      <description>&lt;P&gt;I think you want one record per FILE, namely the first non-reception record to follow a reception:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;DATA SALE;
LENGTH FILE $3 DEPARTMENT $10 ;
INPUT FILE DATE:YYMMDD10. DEPARTMENT $;
FORMAT DATE YYMMDD10.;
DATALINES;
102 2024-01-15 RECEPTION
102 2024-01-30 RECEPTION
102 2024-02-10 SHOES
102 2024-02-29 RECEPTION 
102 2024-02-29 SHOES
103 2024-01-22 RECEPTION
103 2024-02-15 RECEPTION
103 2024-02-20 RECEPTION
103 2024-03-02 PANTS
104 2024-04-12 RECEPTION
104 2024-05-02 RECEPTION
104 2024-05-20 RECEPTION
104 2024-05-30 SHIRTS
104 2024-06-14 SHIRTS
104 2024-06-30 SHIRTS 
RUN;

data want;
  set sale;
  by file; 
  if first.file^=1 and department^='RECEPTION' and lag(department)='RECEPTION';
  if file^=lag(file);
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;The first subsetting IF gets all non-receptions that follow a reception.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;From that subset, the second subsetting IF takes only the first such record for each FILE.&lt;/P&gt;</description>
      <pubDate>Fri, 18 Oct 2024 01:22:21 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Find-next-variable-by-group/m-p/947977#M371006</guid>
      <dc:creator>mkeintz</dc:creator>
      <dc:date>2024-10-18T01:22:21Z</dc:date>
    </item>
    <item>
      <title>Re: Find next variable by group</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Find-next-variable-by-group/m-p/947987#M371008</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;DATA SALE;
LENGTH FILE $3 DEPARTMENT $10 ;
INPUT FILE DATE:YYMMDD10. DEPARTMENT $;
FORMAT DATE YYMMDD10.;
DATALINES;
102 2024-01-15 RECEPTION
102 2024-01-30 RECEPTION
102 2024-02-10 SHOES
102 2024-02-29 RECEPTION 
102 2024-02-29 SHOES
103 2024-01-22 RECEPTION
103 2024-02-15 RECEPTION
103 2024-02-20 RECEPTION
103 2024-03-02 PANTS
104 2024-04-12 RECEPTION
104 2024-05-02 RECEPTION
104 2024-05-20 RECEPTION
104 2024-05-30 SHIRTS
104 2024-06-14 SHIRTS
104 2024-06-30 SHIRTS 
;
data want;
 set sale;
 by FILE;
 retain found 0;
 if first.FILE then found=0;
 if lag(DEPARTMENT) eq 'RECEPTION' and DEPARTMENT ne 'RECEPTION' and lag(FILE)=FILE and not found then do;
  output; found=1;
 end;
 drop found;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Fri, 18 Oct 2024 02:41:14 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Find-next-variable-by-group/m-p/947987#M371008</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2024-10-18T02:41:14Z</dc:date>
    </item>
    <item>
      <title>Re: Find next variable by group</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Find-next-variable-by-group/m-p/948005#M371011</link>
      <description>&lt;P&gt;Elegant.&lt;/P&gt;</description>
      <pubDate>Fri, 18 Oct 2024 08:20:44 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Find-next-variable-by-group/m-p/948005#M371011</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2024-10-18T08:20:44Z</dc:date>
    </item>
    <item>
      <title>Re: Find next variable by group</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Find-next-variable-by-group/m-p/948009#M371013</link>
      <description>Kurt,&lt;BR /&gt;Nope. If there were not any "RECEPTION" within a FILE group, that could get wrong result.</description>
      <pubDate>Fri, 18 Oct 2024 08:55:02 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Find-next-variable-by-group/m-p/948009#M371013</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2024-10-18T08:55:02Z</dc:date>
    </item>
    <item>
      <title>Re: Find next variable by group</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Find-next-variable-by-group/m-p/948043#M371024</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/18408"&gt;@Ksharp&lt;/a&gt;&amp;nbsp;,&lt;/P&gt;
&lt;P&gt;Of course you're right about that.&amp;nbsp; And other cases as well:&amp;nbsp; if the first RECEPTION record comes after a non-RECEPTION record.&lt;/P&gt;
&lt;P&gt;My real point:&amp;nbsp; since the poster doesn't say what's really in the data, and it makes such a difference in the complexity of the solution, shouldn't somebody have asked?&amp;nbsp; The original poster probably doesn't see that the question is important, so let me ask:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;For a FILE, does the first record have to be RECEPTION or could it be something else?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If it is something else, should that record be ignored?&lt;/P&gt;</description>
      <pubDate>Fri, 18 Oct 2024 13:39:06 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Find-next-variable-by-group/m-p/948043#M371024</guid>
      <dc:creator>Astounding</dc:creator>
      <dc:date>2024-10-18T13:39:06Z</dc:date>
    </item>
    <item>
      <title>Re: Find next variable by group</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Find-next-variable-by-group/m-p/948085#M371032</link>
      <description>&lt;P&gt;Yes,&amp;nbsp;&lt;SPAN&gt;the first record have to be RECEPTION. I didn't specify it and it's true that it's important. So your solution will work&amp;nbsp;&lt;span class="lia-unicode-emoji" title=":slightly_smiling_face:"&gt;🙂&lt;/span&gt; Thanks !&lt;/SPAN&gt;&lt;/P&gt;</description>
      <pubDate>Fri, 18 Oct 2024 16:35:07 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Find-next-variable-by-group/m-p/948085#M371032</guid>
      <dc:creator>sasuser_8</dc:creator>
      <dc:date>2024-10-18T16:35:07Z</dc:date>
    </item>
    <item>
      <title>Re: Find next variable by group</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Find-next-variable-by-group/m-p/948225#M371045</link>
      <description>&lt;P&gt;Astounding,&lt;BR /&gt;Don't be offended. I truely understand what you mean. &lt;BR /&gt;We all provide some suggestion based on the data posted by OP . We don't know how it looks like or how it was in reality.&lt;BR /&gt;So base on the data posted by OP and write code to get what OP want, That is OK.&lt;BR /&gt;My real point: make my own code to be as strong/robust as it could be to take into acount of many accident scenarios .&lt;BR /&gt;&lt;BR /&gt;All the best.&lt;/P&gt;</description>
      <pubDate>Sat, 19 Oct 2024 06:39:14 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Find-next-variable-by-group/m-p/948225#M371045</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2024-10-19T06:39:14Z</dc:date>
    </item>
  </channel>
</rss>

