<?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 do I Change the previous values based upon the next values with condition in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/How-do-I-Change-the-previous-values-based-upon-the-next-values/m-p/503031#M134359</link>
    <description>&lt;P&gt;See this:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data test;
old_date = 201701;
new_date = input(put(old_date,6.) !! '01',yymmdd8.);
format new_date yymmn6.;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
    <pubDate>Wed, 10 Oct 2018 11:05:15 GMT</pubDate>
    <dc:creator>Kurt_Bremser</dc:creator>
    <dc:date>2018-10-10T11:05:15Z</dc:date>
    <item>
      <title>How do I Change the previous values based upon the next values with condition</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-do-I-Change-the-previous-values-based-upon-the-next-values/m-p/503003#M134343</link>
      <description>&lt;P&gt;Dear All,&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I had spent sometime on trying to figure out with the help of the SAS community to do the following;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I have the dataset as below; ('month' is numeric)&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
    input month id bonus;
    cards;
201701 101 0
201701 102 0
201702 101 0
201702 102 1
201703 101 0
201703 102 0
201704 101 0
201704 102 0
201705 101 0
201705 102 0
201706 101 0
201706 102 0
201707 101 1
201707 102 0
;
run;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;I wish to check if the value of the bonus is 1 from the top order. If it is 1, I wish to make the value for bonus as 1 for the previous 6 months as well. Otherwise in which case, I wish to keep it as 0.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Ideally, I wanted to have the following results;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;201701 101 1
201701 102 1
201702 101 1
201702 102 1
201703 101 1
201703 102 0
201704 101 1
201704 102 0
201705 101 1
201705 102 0
201706 101 1
201706 102 0
201707 101 1
201707 102 0&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;I tried to do the above with the following code with the help of community forums, however, I see it does not and looks very much not convincing. It leads to a long steps likely.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql noprint;
    CREATE TABLE want AS
    SELECT a.*, b.bonus AS bonus1,c.bonus as bonus2,d.bonus as bonus3,e.bonus as bonus4,f.bonus as bonus5, g.bonus as bonus6
    FROM have a
    LEFT JOIN have b
    ON b.ID=a.ID AND b.Month=a.Month+1+88*(mod(a.MONTH,100)&amp;gt;11)
	LEFT JOIN have c
    ON c.ID=a.ID AND c.Month=a.Month+2+88*(mod(a.MONTH,100)&amp;gt;10)
	LEFT JOIN have d
    ON d.ID=a.ID AND d.Month=a.Month+3+88*(mod(a.MONTH,100)&amp;gt;9)
	LEFT JOIN have e
    ON e.ID=a.ID AND e.Month=a.Month+4+88*(mod(a.MONTH,100)&amp;gt;8)
	LEFT JOIN have f
    ON f.ID=a.ID AND f.Month=a.Month+5+88*(mod(a.MONTH,100)&amp;gt;7)
	LEFT JOIN have g
    ON g.ID=a.ID AND g.Month=a.Month+6+88*(mod(a.MONTH,100)&amp;gt;6)
    ORDER BY Month, ID
    ;
quit;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;Can anyone please help me doing this efficiently?I use SAS EG&amp;nbsp;7.13 HF2&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thank you,&lt;/P&gt;</description>
      <pubDate>Wed, 10 Oct 2018 08:47:54 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-do-I-Change-the-previous-values-based-upon-the-next-values/m-p/503003#M134343</guid>
      <dc:creator>ggfggrr</dc:creator>
      <dc:date>2018-10-10T08:47:54Z</dc:date>
    </item>
    <item>
      <title>Re: How do I Change the previous values based upon the next values with condition</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-do-I-Change-the-previous-values-based-upon-the-next-values/m-p/503011#M134346</link>
      <description>&lt;P&gt;Sort in reverse time order, and carry over with retained variables.&lt;/P&gt;
&lt;P&gt;Note that I made your "month" into a SAS date, so that SAS date functions and formats can be used. See Maxim 33.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
input month :yymmn6. id bonus;
format month yymmn6.;
cards;
201701 101 0
201701 102 0
201702 101 0
201702 102 1
201703 101 0
201703 102 0
201704 101 0
201704 102 0
201705 101 0
201705 102 0
201706 101 0
201706 102 0
201707 101 1
201707 102 0
;
run;

proc sort data=have;
by id descending month;
run;

data want;
set have;
by id;
retain _bonus _month;
format _month yymmn6.;
if first.id
then do;
  _bonus = 0;
  _month = .;
end;
if bonus = 1
then do;
  _bonus = 1;
  _month = month;
end;
else do; /* bonus = 0 */
  if _bonus = 1
  then do;
    if intck('month',month,_month) &amp;gt; 6
    then do;
      _bonus = 0;
      _month = .;
    end;
    else bonus = 1;
  end;
end;
drop _bonus _month;
run;

proc sort data=want;
by month id;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 10 Oct 2018 09:14:33 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-do-I-Change-the-previous-values-based-upon-the-next-values/m-p/503011#M134346</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2018-10-10T09:14:33Z</dc:date>
    </item>
    <item>
      <title>Re: How do I Change the previous values based upon the next values with condition</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-do-I-Change-the-previous-values-based-upon-the-next-values/m-p/503027#M134355</link>
      <description>&lt;P&gt;Thank you so much for your quick help in solving this.&lt;/P&gt;&lt;P&gt;Also, I am referring to one of your other post &lt;A title="Convert Character variable / string YYYYMM in date" href="https://communities.sas.com/t5/SAS-Communities-Library/How-to-create-a-data-step-version-of-your-data-AKA-generate/ta-p/258712" target="_blank"&gt;Convert Character variable / string YYYYMM in date&lt;/A&gt;&amp;nbsp;using which I tried to convert the numeric '201701" for example to date SAS date format 201701 (for YYYYMM). However I receive the empty values (.) in the column HAVE. Anything I do it incorrectly?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Kind regards,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 10 Oct 2018 10:40:00 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-do-I-Change-the-previous-values-based-upon-the-next-values/m-p/503027#M134355</guid>
      <dc:creator>ggfggrr</dc:creator>
      <dc:date>2018-10-10T10:40:00Z</dc:date>
    </item>
    <item>
      <title>Re: How do I Change the previous values based upon the next values with condition</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-do-I-Change-the-previous-values-based-upon-the-next-values/m-p/503031#M134359</link>
      <description>&lt;P&gt;See this:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data test;
old_date = 201701;
new_date = input(put(old_date,6.) !! '01',yymmdd8.);
format new_date yymmn6.;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 10 Oct 2018 11:05:15 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-do-I-Change-the-previous-values-based-upon-the-next-values/m-p/503031#M134359</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2018-10-10T11:05:15Z</dc:date>
    </item>
  </channel>
</rss>

