<?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: Looping through triplets of variables for each observation in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Looping-through-triplets-of-variables-for-each-observation/m-p/496141#M131135</link>
    <description>&lt;P&gt;Ok, something like:&lt;/P&gt;
&lt;PRE&gt;data flagdemo;
  input id year_t0 flag_t0;
datalines;
1 2008 0
1 2009 0
1 2010 0
1 2011 0
1 2012 0
1 2013 1
2 2010 0
2 2011 0
2 2012 0
2 2013 0
2 2014 1
3 2009 1
;
run;

proc sql;
  create table want as
  select a.*,
         (select max(flag_t0) from (select * from flagdemo where id=a.id and a.year_t0 &amp;lt;= year_t0 &amp;lt;= a.year_t0 + 2) group by id) as flag_2y
  from   flagdemo a;
quit;&lt;/PRE&gt;
&lt;P&gt;Or you could use the array code I presented earlier.&lt;/P&gt;</description>
    <pubDate>Mon, 17 Sep 2018 07:54:37 GMT</pubDate>
    <dc:creator>RW9</dc:creator>
    <dc:date>2018-09-17T07:54:37Z</dc:date>
    <item>
      <title>Looping through triplets of variables for each observation</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Looping-through-triplets-of-variables-for-each-observation/m-p/495628#M130845</link>
      <description>&lt;P&gt;Hi everybody,&lt;/P&gt;&lt;P&gt;I have the following data:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;DATA FLAGDEMO;
INPUT ID YEAR_T0 FLAG_2008 FLAG_2009 FLAG_2010 FLAG_2011 FLAG_2012 FLAG_2013 FLAG_2014 /*DESIRED FLAGS*/ FLAG_2Y FLAG_3Y;
DATALINES;
1 2008 0 0 0 0 0 1 . 0 0
1 2009 0 0 0 0 0 1 . 0 0
1 2010 0 0 0 0 0 1 . 0 1
1 2011 0 0 0 0 0 1 . 1 1
1 2012 0 0 0 0 0 1 . 1 1
1 2013 0 0 0 0 0 1 . 1 1
2 2010 0 0 0 0 0 . 1 0 0
2 2011 0 0 0 0 0 . 1 0 1
2 2012 0 0 0 0 0 . 1 1 1
2 2013 0 0 0 0 0 . 1 1 1
2 2014 0 0 0 0 0 . 1 1 1
3 2009 0 1 . . . . . 1 1
3 2010 0 1 . . . . . 1 1
;
RUN;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I need to create the /*DESIRED FLAGS*/ FLAG_2Y and FLAG_3Y (I inserted them manually to better explain what I want); these flags are equal to the maximum between FLAG_(n) and FLAG_(n+2) o FLAG_(n+3) for each YEAR_T0=n. So for example, if YEAR_T0=2008 i want FLAG_2Y to be the maximum between flag_2008, flag_2009,flag_2010.&lt;/P&gt;&lt;P&gt;I tried several combination of %do and array but I can't figure out the correct sintax or logic.&lt;/P&gt;&lt;P&gt;Examples of attempt:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;First Attempt&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%macro flag_year_v1(t0,t2);
data output;
set input;
if YEAR_T0=&amp;amp;T0. then flag_2y=max(FLAG_&amp;amp;t0.-FLAG_&amp;amp;t2.);
run;

%mend flag_year_v1;

%flag_year_v1(2008,2010);
%flag_year_v1(2009,2011);
%flag_year_v1(2010,2012);
%flag_year_v1(2011,2013);
%flag_year_v1(2012,2014);
%flag_year_v1(2013,2014);
%flag_year_v1(2014,2014);&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Second Attempt&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%MACRO FLAG_2Y_3Y(START,STOP);
%DO i=(&amp;amp;start.) %TO (&amp;amp;stop.);
%DO j=(&amp;amp;start.+2) %TO (&amp;amp;stop.);
%DO h=(&amp;amp;start.+3) %TO (&amp;amp;stop.);

DATA output;
SET input;
ARRAY FLAG_Y(*) FLAG_&amp;amp;i.-FLAG_&amp;amp;h.;
flag_default_2y=max(FLAG_Y(&amp;amp;i.)-FLAG_Y(&amp;amp;j.));
flag_default_3y=max(FLAG_Y(&amp;amp;i.)-FLAG_Y(&amp;amp;h.));
%END;
%END;
%END;
run;

%MEND FLAG_2Y_3Y;

%FLAG_2Y_3Y(2008,2014);&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;As much as i try, I can't obtain my desired results, and I would like to write a code similar to the second but I am not super proficient with iteratives loop.&lt;/P&gt;&lt;P&gt;Hope somebody can help me&amp;nbsp;&lt;img id="smileyfrustrated" class="emoticon emoticon-smileyfrustrated" src="https://communities.sas.com/i/smilies/16x16_smiley-frustrated.png" alt="Smiley Frustrated" title="Smiley Frustrated" /&gt;&lt;/P&gt;</description>
      <pubDate>Fri, 14 Sep 2018 13:05:23 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Looping-through-triplets-of-variables-for-each-observation/m-p/495628#M130845</guid>
      <dc:creator>HeyLyla90</dc:creator>
      <dc:date>2018-09-14T13:05:23Z</dc:date>
    </item>
    <item>
      <title>Re: Looping through triplets of variables for each observation</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Looping-through-triplets-of-variables-for-each-observation/m-p/495634#M130848</link>
      <description>&lt;P&gt;Hello,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Use the keyword "of" when you reref to a set of variables :&lt;/P&gt;
&lt;P&gt;max(of FLAG_&amp;amp;t0.-FLAG_&amp;amp;t2.)&lt;/P&gt;</description>
      <pubDate>Fri, 14 Sep 2018 11:47:27 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Looping-through-triplets-of-variables-for-each-observation/m-p/495634#M130848</guid>
      <dc:creator>gamotte</dc:creator>
      <dc:date>2018-09-14T11:47:27Z</dc:date>
    </item>
    <item>
      <title>Re: Looping through triplets of variables for each observation</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Looping-through-triplets-of-variables-for-each-observation/m-p/495641#M130854</link>
      <description>&lt;P&gt;You can use arrays for this, no need macro which just complicates things.&lt;/P&gt;
&lt;PRE&gt;data want;
  set have;
  array f flag_:;
  do i=1 to dim(f);
    if vname(f{i})=cats("FLAG_",year_to) then flag_default_2y=max(f{i},f{i+1},f{i+2});
  end;
run;&lt;/PRE&gt;
&lt;P&gt;This is not tested, as you have not provided any test data in a datastep, and looking at it again, then you would also need to check if there are elements available for the max, i.e. if the last element then you do not have +1 and +2, so what would you do in those instances?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;This would of course - as always - be far simpler if you modelled you data in a usable way and avoid Excel thinking.&amp;nbsp;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;ID&amp;nbsp; YEAR_TO&amp;nbsp; YEAR&amp;nbsp; FLAG&lt;/P&gt;
&lt;P&gt;...&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;With this, you can simply compare if year_to &amp;lt;= year &amp;lt;= year_to+2.&amp;nbsp; Much simpler coding and logic.&lt;/P&gt;</description>
      <pubDate>Fri, 14 Sep 2018 12:04:30 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Looping-through-triplets-of-variables-for-each-observation/m-p/495641#M130854</guid>
      <dc:creator>RW9</dc:creator>
      <dc:date>2018-09-14T12:04:30Z</dc:date>
    </item>
    <item>
      <title>Re: Looping through triplets of variables for each observation</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Looping-through-triplets-of-variables-for-each-observation/m-p/495682#M130870</link>
      <description>&lt;P&gt;Hi RW9,&lt;/P&gt;&lt;P&gt;thank you for your answer.&lt;/P&gt;&lt;P&gt;I run your code with my&amp;nbsp;data (i edited my answer providing the test data) and it partially work.&lt;/P&gt;&lt;P&gt;The problem is that&amp;nbsp;my real data do not have the two final FLAG_2Y and FLAG_3Y so when SAS compile for i=2013, it doesn't find the i+2 variable because&amp;nbsp;these variables stops at FLAG_2014 and provide the error "ARRAY subscript out of range".&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I solved the issue by writing:&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want_2y;
  set FLAGDEMO;
  array f flag_:;
  do i=1 TO 5;
    if vname(f{i})=cats("FLAG_",year_t0) then flag_default_2y=max(f{i},f{i+1},f{i+2});
  end;
    if vname(f{6})=cats("FLAG_",year_t0) then flag_default_2y=max(f{6},f{7});
	if vname(f{7})=cats("FLAG_",year_t0) then flag_default_2y=f{7};
run;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;However, I'm sure there is a more efficient way to write this, so that if the number of years should increase, it would be not necessary to&amp;nbsp;manually insert the numbers.&amp;nbsp;Of course writing it as a macro, but since I am not that&amp;nbsp;expert about dos and arrays I wonder if there is a more straightforward solution.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Otherwise, my original data looked like this:&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;DATA FLAGDEMO;
INPUT ID YEAR_T0 FLAG_t0;
DATALINES;
1 2008 0
1 2009 0
1 2010 0
1 2011 0
1 2012 0
1 2013 1
2 2010 0
2 2011 0
2 2012 0
2 2013 0
2 2014 1
3 2009 1
;
RUN;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;What would you have done in this case to calculate the same flags? I transposed this table in what I posted because I thought it would have been easier, but any better suggestion would approciated &lt;span class="lia-unicode-emoji" title=":slightly_smiling_face:"&gt;🙂&lt;/span&gt;&lt;/P&gt;</description>
      <pubDate>Fri, 14 Sep 2018 13:55:53 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Looping-through-triplets-of-variables-for-each-observation/m-p/495682#M130870</guid>
      <dc:creator>HeyLyla90</dc:creator>
      <dc:date>2018-09-14T13:55:53Z</dc:date>
    </item>
    <item>
      <title>Re: Looping through triplets of variables for each observation</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Looping-through-triplets-of-variables-for-each-observation/m-p/495720#M130898</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/126219"&gt;@HeyLyla90&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;Hi RW9,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I solved the issue by writing:&lt;/P&gt;
&lt;P&gt;However, I'm sure there is a more efficient way to write this, so that if the number of years should increase, it would be not necessary to&amp;nbsp;manually insert the numbers.&amp;nbsp;Of course writing it as a macro, but since I am not that&amp;nbsp;expert about dos and arrays I wonder if there is a more straightforward solution.&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;Yes there is. It is called data normalization. Instead of having actual&amp;nbsp;data in your variable names (the year) create a data set that has a variable to hold that information and one record per value.&lt;/P&gt;
&lt;P&gt;Is that data example the way you read an external file or did you create flag_2008 etc in a previous step?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 14 Sep 2018 15:05:48 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Looping-through-triplets-of-variables-for-each-observation/m-p/495720#M130898</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2018-09-14T15:05:48Z</dc:date>
    </item>
    <item>
      <title>Re: Looping through triplets of variables for each observation</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Looping-through-triplets-of-variables-for-each-observation/m-p/495722#M130900</link>
      <description>&lt;P&gt;From the data I provided in the reply, I did:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc transpose data =FLAGDEMO out =FLAGDEMO2 prefix = FLAG_;
	by ID YEAR_T0;
	id YEAR_T0;
	var FLAG_T0;
run;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;Then joined FLAGDEMO2 with FLAGDEMO to have the matrix I initially posted, but the original data is the second one!&lt;/P&gt;</description>
      <pubDate>Fri, 14 Sep 2018 15:10:29 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Looping-through-triplets-of-variables-for-each-observation/m-p/495722#M130900</guid>
      <dc:creator>HeyLyla90</dc:creator>
      <dc:date>2018-09-14T15:10:29Z</dc:date>
    </item>
    <item>
      <title>Re: Looping through triplets of variables for each observation</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Looping-through-triplets-of-variables-for-each-observation/m-p/495752#M130912</link>
      <description>&lt;P&gt;From your original data (note I corrected it to avoid shouting):&lt;/P&gt;
&lt;PRE&gt;data flagdemo;
  input id year_t0 flag_t0;
datalines;
1 2008 0
1 2009 0
1 2010 0
1 2011 0
1 2012 0
1 2013 1
2 2010 0
2 2011 0
2 2012 0
2 2013 0
2 2014 1
3 2009 1
;
run;&lt;/PRE&gt;
&lt;P&gt;You haven't posted what you want as output, so I will just show an output routine which will for each row of the above, take the next two rows max:&lt;/P&gt;
&lt;PRE&gt;data want;
  set flagdemo;
  if lag(id)=id and lag2(id)=id then flag_2y=max(lag(flag_t0),lag2(flag_t0));
run;&lt;/PRE&gt;
&lt;P&gt;You will note how flag_2y is only created where there are two previous results.&amp;nbsp; There are many ways of doing this part, its merely to show you that working with normalised data is far simpler.&amp;nbsp; If you can provide what you want as output (as your first required output doesn't make sense, only one 2yr flag for many years) then I can look on Monday.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 14 Sep 2018 16:14:15 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Looping-through-triplets-of-variables-for-each-observation/m-p/495752#M130912</guid>
      <dc:creator>RW9</dc:creator>
      <dc:date>2018-09-14T16:14:15Z</dc:date>
    </item>
    <item>
      <title>Re: Looping through triplets of variables for each observation</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Looping-through-triplets-of-variables-for-each-observation/m-p/496075#M131098</link>
      <description>&lt;P&gt;My desired output is always the one I posted from the beginning, that would be:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data flagdemo;
input id year_t0 flag_t0 wanted_flag_2y;
datalines;
1 2008 0 0
1 2009 0 0
1 2010 0 0
1 2011 0 1
1 2012 0 1
1 2013 1 1
2 2010 0 0
2 2011 0 0
2 2012 0 1
2 2013 0 1
2 2014 1 1
3 2009 1 1
;
run;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Your code gets different result&lt;FONT size="1 2 3 4 5 6 7"&gt;(*)&lt;/FONT&gt;, what I need to do is always to obtain a flag that for each id indicates the maximum of flag_t0 in the two and three years following the t0 so for 2008 the maximum among 2008, 2009 and 2010 and so on.&lt;/P&gt;&lt;P&gt;I realize I have still an excel mindset, but I really try to do my best as a beginner and&amp;nbsp;I know there are a lot of functions out there for me to discover&amp;nbsp;&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;P&gt;&lt;FONT size="1 2 3 4 5 6 7"&gt;(*)&lt;/FONT&gt;Results with your last code:&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
input id year_t0 flag_t0 wanted_flag_2y;
datalines
1 2008 0 .
1 2009 0 .
1 2010 0 .
1 2011 0 0
1 2012 0 0
1 2013 1 0
2 2010 0 .
2 2011 0 .
2 2012 0 1
2 2013 0 1
2 2014 1 0
3 2009 1 .
;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Sun, 16 Sep 2018 20:06:17 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Looping-through-triplets-of-variables-for-each-observation/m-p/496075#M131098</guid>
      <dc:creator>HeyLyla90</dc:creator>
      <dc:date>2018-09-16T20:06:17Z</dc:date>
    </item>
    <item>
      <title>Re: Looping through triplets of variables for each observation</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Looping-through-triplets-of-variables-for-each-observation/m-p/496084#M131101</link>
      <description>&lt;P&gt;Looking ahead is harder than looking backwards since you need to examine observations that you have not yet read from the disk.&lt;/P&gt;
&lt;P&gt;But you can do it by adding extra SET statements.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want ;
  set flagdemo end=eof1 ;
  if eof1 then call missing(id1);
  else set flagdemo (firstobs=2 keep=id flag_t0 rename=(id=id1 flag_t0=flag_t1)) end=eof2;
  if eof2 then call missing(id2);
  else set flagdemo (firstobs=3 keep=id flag_t0 rename=(id=id2 flag_t0=flag_t2));
  if id ne id1 then call missing(flag_t1);
  if id ne id2 then call missing(flag_t2);

  flag_2y = flag_t0 or flag_t1 or flag_t2 ;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;You could also use PROC SQL to join the table with itself.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql ;
create table want as
  select a.*,max(b.flag_t0) as flag_2y
  from flagdemo a
  left join flagdemo b
  on a.id = b.id and b.year_t0 between a.year_t0 and a.year_t0+2
  group by 1,2,3
  order by 1,2
;
quit;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 17 Sep 2018 07:03:34 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Looping-through-triplets-of-variables-for-each-observation/m-p/496084#M131101</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2018-09-17T07:03:34Z</dc:date>
    </item>
    <item>
      <title>Re: Looping through triplets of variables for each observation</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Looping-through-triplets-of-variables-for-each-observation/m-p/496113#M131116</link>
      <description>&lt;P&gt;Assign the flag variables to an array indexed by year.&amp;nbsp; Append 3 extra dummy variables to that array (thereby extending the upper bound of the array index to 3 plus the highest expected value of YEAR_T0.&amp;nbsp; Then it's simple:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;DATA FLAGDEMO;
INPUT ID YEAR_T0 FLAG_2008 FLAG_2009 FLAG_2010 FLAG_2011 FLAG_2012 FLAG_2013 FLAG_2014 /*DESIRED FLAGS*/ FLAG_2Y FLAG_3Y;
DATALINES;
1 2008 0 0 0 0 0 1 . 0 0
1 2009 0 0 0 0 0 1 . 0 0
1 2010 0 0 0 0 0 1 . 0 1
1 2011 0 0 0 0 0 1 . 1 1
1 2012 0 0 0 0 0 1 . 1 1
1 2013 0 0 0 0 0 1 . 1 1
2 2010 0 0 0 0 0 . 1 0 0
2 2011 0 0 0 0 0 . 1 0 1
2 2012 0 0 0 0 0 . 1 1 1
2 2013 0 0 0 0 0 . 1 1 1
2 2014 0 0 0 0 0 . 1 1 1
3 2009 0 1 . . . . . 1 1
3 2010 0 1 . . . . . 1 1
;
RUN;
data want (drop=dummy);
  set flagdemo;
  array flg{2008:2017} flag_2008-flag_2014 dummy dummy dummy;
  f2y=max(flg{year_t0},flg{year_t0+1},flg{year_t0+2});
  f3y=max(f2y,flg{year_t0+3});
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;The "trick" here is to set the lower and upper bounds of the index for array flg (2008 and 2017 in this case).&amp;nbsp; And since the extra dummy variables are all missing, they don't modify the result of the max functions.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;By the way, this program does not reproduce the last record of your sample output, which I suspect is in error.&lt;/P&gt;</description>
      <pubDate>Mon, 17 Sep 2018 04:23:17 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Looping-through-triplets-of-variables-for-each-observation/m-p/496113#M131116</guid>
      <dc:creator>mkeintz</dc:creator>
      <dc:date>2018-09-17T04:23:17Z</dc:date>
    </item>
    <item>
      <title>Re: Looping through triplets of variables for each observation</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Looping-through-triplets-of-variables-for-each-observation/m-p/496141#M131135</link>
      <description>&lt;P&gt;Ok, something like:&lt;/P&gt;
&lt;PRE&gt;data flagdemo;
  input id year_t0 flag_t0;
datalines;
1 2008 0
1 2009 0
1 2010 0
1 2011 0
1 2012 0
1 2013 1
2 2010 0
2 2011 0
2 2012 0
2 2013 0
2 2014 1
3 2009 1
;
run;

proc sql;
  create table want as
  select a.*,
         (select max(flag_t0) from (select * from flagdemo where id=a.id and a.year_t0 &amp;lt;= year_t0 &amp;lt;= a.year_t0 + 2) group by id) as flag_2y
  from   flagdemo a;
quit;&lt;/PRE&gt;
&lt;P&gt;Or you could use the array code I presented earlier.&lt;/P&gt;</description>
      <pubDate>Mon, 17 Sep 2018 07:54:37 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Looping-through-triplets-of-variables-for-each-observation/m-p/496141#M131135</guid>
      <dc:creator>RW9</dc:creator>
      <dc:date>2018-09-17T07:54:37Z</dc:date>
    </item>
    <item>
      <title>Re: Looping through triplets of variables for each observation</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Looping-through-triplets-of-variables-for-each-observation/m-p/496149#M131142</link>
      <description>Yes it is, I also though of this solution, thank you!</description>
      <pubDate>Mon, 17 Sep 2018 08:30:57 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Looping-through-triplets-of-variables-for-each-observation/m-p/496149#M131142</guid>
      <dc:creator>HeyLyla90</dc:creator>
      <dc:date>2018-09-17T08:30:57Z</dc:date>
    </item>
  </channel>
</rss>

