<?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: Differences between quarters in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Differences-between-quarters/m-p/492027#M129233</link>
    <description>&lt;P&gt;I shamelessly stole the example data from &lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/18408"&gt;@Ksharp&lt;/a&gt; and expanded the basic idea (as you need a "reverse" difference):&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
input Date :$20.      ID           ID_A    $    Quarter;
cards;
03JAN2017                      1                  A                   1
04JAN2017                      1                  A                   1
04APR2017                      1                  A                   2
10MAY2017                      1                  A                   2
09AUG2017                      1                  A                   3
16FEB2017                      1                  B                   1
18SEP2018                      1                  B                   7
13JAN2017                      2                  A                   1
15FEB2017                      2                  A                   1
21JUN2017                      2                  A                   2
14MAR2017                      2                  B                   1
21NOV2017                      2                  B                   4
;
run;

proc sort
  data=have(keep=id id_a quarter)
  out=temp
  nodupkey
;
by id id_a descending quarter;
run;

data diff;
set temp;
by id id_a;
qbt = lag(quarter) - quarter;
if first.id_a then qbt = -50000;
run;

proc sort data=diff;
by id id_a quarter;
run;

data want;
merge
  have
  diff
;
by id id_a quarter;
run;

proc print data=want noobs;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Result:&lt;/P&gt;
&lt;PRE&gt;  Date       ID    ID_A    Quarter       qbt

03JAN2017     1     A         1            1
04JAN2017     1     A         1            1
04APR2017     1     A         2            1
10MAY2017     1     A         2            1
09AUG2017     1     A         3       -50000
16FEB2017     1     B         1            6
18SEP2018     1     B         7       -50000
13JAN2017     2     A         1            1
15FEB2017     2     A         1            1
21JUN2017     2     A         2       -50000
14MAR2017     2     B         1            3
21NOV2017     2     B         4       -50000
&lt;/PRE&gt;
&lt;P&gt;which matches your originally wanted result:&lt;/P&gt;
&lt;PRE&gt;Date                          ID               ID_A            Quarter          QBT
03JAN2017                      1                 A             1                 1
04JAN2017                      1                 A             1                 1
04APR2017                      1                 A             2                 1
10MAY2017                      1                 A             2                 1
09AUG2017                      1                 A             3               -50000 /* Just a flag */
16FEB2017                      1                 B             1                 6
18SEP2018                      1                 B             7                -50000
13JAN2017                      2                 A             1                 1
15FEB2017                      2                 A             1                 1
21JUN2017                      2                 A             2                -50000
14MAR2017                      2                 B             1                 3
21NOV2017                      2                 B             4               -50000&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Mon, 03 Sep 2018 13:22:49 GMT</pubDate>
    <dc:creator>Kurt_Bremser</dc:creator>
    <dc:date>2018-09-03T13:22:49Z</dc:date>
    <item>
      <title>Differences between quarters</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Differences-between-quarters/m-p/491862#M129119</link>
      <description>&lt;PRE&gt;Dear All:&lt;BR /&gt;My Data is as follows
Date                               ID           ID_A            Quarter
03JAN2017                      1                  A                   1
04JAN2017                      1                  A                   1
04APR2017                      1                  A                   2
10MAY2017                      1                  A                   2
09AUG2017                      1                  A                   3
16FEB2017                      1                  B                   1
18SEP2018                      1                  B                   7
13JAN2017                      2                  A                   1
15FEB2017                      2                  A                   1
21JUN2017                      2                  A                   2
14MAR2017                      2                  B                   1
21NOV2017                      2                  B                   4

I want to add another variable QBT indicating the quarters between transactions

Date                          ID               ID_A            Quarter          QBT
03JAN2017                      1                 A             1                 1
04JAN2017                      1                 A             1                 1
04APR2017                      1                 A             2                 1
10MAY2017                      1                 A             2                 1
09AUG2017                      1                 A             3               -50000 /* Just a flag */
16FEB2017                      1                 B             1                 6
18SEP2018                      1                 B             7                -50000
13JAN2017                      2                 A             1                 1
15FEB2017                      2                 A             1                 1
21JUN2017                      2                 A             2                -50000
14MAR2017                      2                 B             1                 3
21NOV2017                      2                 B             4               -50000

The rules are as follows:&lt;BR /&gt;&lt;BR /&gt;&lt;/PRE&gt;&lt;PRE&gt;For ID = 1 and ID_A = A  there are transactions in 3 Quarters -- 1, 2 and 3

so QBT is the difference between Quarter  = 2 and Quarter  = 1

So for the first two lines QBT = 1; then for the next two lines QBT = 1 also (3 -2).  The last row of ID and ID_A is = -50000

The same logic applies to ID = 1 and ID_A = B&lt;/PRE&gt;&lt;P&gt;Thanx in Advance&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;R&lt;/P&gt;</description>
      <pubDate>Sun, 02 Sep 2018 02:54:23 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Differences-between-quarters/m-p/491862#M129119</guid>
      <dc:creator>RandyStan</dc:creator>
      <dc:date>2018-09-02T02:54:23Z</dc:date>
    </item>
    <item>
      <title>Re: Differences between quarters</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Differences-between-quarters/m-p/491881#M129134</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
input Date :$20.      ID           ID_A    $    Quarter;
cards;
03JAN2017                      1                  A                   1
04JAN2017                      1                  A                   1
04APR2017                      1                  A                   2
10MAY2017                      1                  A                   2
09AUG2017                      1                  A                   3
16FEB2017                      1                  B                   1
18SEP2018                      1                  B                   7
13JAN2017                      2                  A                   1
15FEB2017                      2                  A                   1
21JUN2017                      2                  A                   2
14MAR2017                      2                  B                   1
21NOV2017                      2                  B                   4
;
run;
proc sort data=have(keep=id id_a quarter) out=temp nodupkey;
by id id_a quarter;
run;
data diff;
 merge temp temp(firstobs=2 rename=(id=_id id_a=_id_a quarter=_quarter));
 if id=_id and id_a=_id_a then diff=_quarter-quarter;
  else diff=-50000;
 drop _:;
run;
data want;
 merge have diff;
 by id id_a quarter;
run;
&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Sun, 02 Sep 2018 10:58:28 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Differences-between-quarters/m-p/491881#M129134</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2018-09-02T10:58:28Z</dc:date>
    </item>
    <item>
      <title>Re: Differences between quarters</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Differences-between-quarters/m-p/491927#M129174</link>
      <description>&lt;P&gt;Dear Mr. KSharp:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Your solution does not result in the output that I need. Your solution of finding the difference gives the following&lt;/P&gt;&lt;PRE class=" language-sas"&gt;&lt;CODE class="  language-sas"&gt;&lt;SPAN class="token datalines"&gt;&lt;SPAN class="token data string"&gt;DATE                          ID                 ID_A               QUARTER         QBT&lt;BR /&gt;03JAN2017                      1                  A                   1              0     
04JAN2017                      1                  A                   1              1
04APR2017                      1                  A                   2              0
10MAY2017                      1                  A                   2              1
09AUG2017                      1                  A                   3             -50000&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;What I need is&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&lt;/P&gt;&lt;PRE class=" language-sas"&gt;&lt;CODE class="  language-sas"&gt;&lt;SPAN class="token datalines"&gt;&lt;SPAN class="token data string"&gt;DATE                          ID                ID_A              QUARTER     QBT&lt;BR /&gt;03JAN2017                      1                  A                   1        1
04JAN2017                      1                  A                   1        1
04APR2017                      1                  A                   2        1
10MAY2017                      1                  A                   2        1
09AUG2017                      1                  A                   3       -50000&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;Please help&lt;/P&gt;&lt;P&gt;&amp;nbsp;Thanx&lt;/P&gt;&lt;P&gt;R&lt;/P&gt;</description>
      <pubDate>Sun, 02 Sep 2018 21:15:54 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Differences-between-quarters/m-p/491927#M129174</guid>
      <dc:creator>RandyStan</dc:creator>
      <dc:date>2018-09-02T21:15:54Z</dc:date>
    </item>
    <item>
      <title>Re: Differences between quarters</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Differences-between-quarters/m-p/492027#M129233</link>
      <description>&lt;P&gt;I shamelessly stole the example data from &lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/18408"&gt;@Ksharp&lt;/a&gt; and expanded the basic idea (as you need a "reverse" difference):&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
input Date :$20.      ID           ID_A    $    Quarter;
cards;
03JAN2017                      1                  A                   1
04JAN2017                      1                  A                   1
04APR2017                      1                  A                   2
10MAY2017                      1                  A                   2
09AUG2017                      1                  A                   3
16FEB2017                      1                  B                   1
18SEP2018                      1                  B                   7
13JAN2017                      2                  A                   1
15FEB2017                      2                  A                   1
21JUN2017                      2                  A                   2
14MAR2017                      2                  B                   1
21NOV2017                      2                  B                   4
;
run;

proc sort
  data=have(keep=id id_a quarter)
  out=temp
  nodupkey
;
by id id_a descending quarter;
run;

data diff;
set temp;
by id id_a;
qbt = lag(quarter) - quarter;
if first.id_a then qbt = -50000;
run;

proc sort data=diff;
by id id_a quarter;
run;

data want;
merge
  have
  diff
;
by id id_a quarter;
run;

proc print data=want noobs;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Result:&lt;/P&gt;
&lt;PRE&gt;  Date       ID    ID_A    Quarter       qbt

03JAN2017     1     A         1            1
04JAN2017     1     A         1            1
04APR2017     1     A         2            1
10MAY2017     1     A         2            1
09AUG2017     1     A         3       -50000
16FEB2017     1     B         1            6
18SEP2018     1     B         7       -50000
13JAN2017     2     A         1            1
15FEB2017     2     A         1            1
21JUN2017     2     A         2       -50000
14MAR2017     2     B         1            3
21NOV2017     2     B         4       -50000
&lt;/PRE&gt;
&lt;P&gt;which matches your originally wanted result:&lt;/P&gt;
&lt;PRE&gt;Date                          ID               ID_A            Quarter          QBT
03JAN2017                      1                 A             1                 1
04JAN2017                      1                 A             1                 1
04APR2017                      1                 A             2                 1
10MAY2017                      1                 A             2                 1
09AUG2017                      1                 A             3               -50000 /* Just a flag */
16FEB2017                      1                 B             1                 6
18SEP2018                      1                 B             7                -50000
13JAN2017                      2                 A             1                 1
15FEB2017                      2                 A             1                 1
21JUN2017                      2                 A             2                -50000
14MAR2017                      2                 B             1                 3
21NOV2017                      2                 B             4               -50000&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 03 Sep 2018 13:22:49 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Differences-between-quarters/m-p/492027#M129233</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2018-09-03T13:22:49Z</dc:date>
    </item>
    <item>
      <title>Re: Differences between quarters</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Differences-between-quarters/m-p/492049#M129250</link>
      <description>&lt;P&gt;That is really weird. Here is my output .&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&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="x.png" style="width: 600px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/22953i160A0DCDE2439F05/image-size/large?v=v2&amp;amp;px=999" role="button" title="x.png" alt="x.png" /&gt;&lt;/span&gt;&lt;/P&gt;</description>
      <pubDate>Mon, 03 Sep 2018 13:05:19 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Differences-between-quarters/m-p/492049#M129250</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2018-09-03T13:05:19Z</dc:date>
    </item>
    <item>
      <title>Re: Differences between quarters</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Differences-between-quarters/m-p/492053#M129254</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/133090"&gt;@RandyStan&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;Dear Mr. KSharp:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Your solution does not result in the output that I need. Your solution of finding the difference gives the following&lt;/P&gt;
&lt;LI-CODE lang="sas"&gt;&lt;SPAN class="token datalines"&gt;&lt;SPAN class="token data string"&gt;DATE ID ID_A QUARTER QBT&lt;BR /&gt;03JAN2017 1 A 1 0 04JAN2017 1 A 1 1 04APR2017 1 A 2 0 10MAY2017 1 A 2 1 09AUG2017 1 A 3 -50000&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;/LI-CODE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;What I need is&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;&lt;/P&gt;
&lt;LI-CODE lang="sas"&gt;&lt;SPAN class="token datalines"&gt;&lt;SPAN class="token data string"&gt;DATE ID ID_A QUARTER QBT&lt;BR /&gt;03JAN2017 1 A 1 1 04JAN2017 1 A 1 1 04APR2017 1 A 2 1 10MAY2017 1 A 2 1 09AUG2017 1 A 3 -50000&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;/LI-CODE&gt;
&lt;P&gt;&amp;nbsp;Please help&lt;/P&gt;
&lt;P&gt;&amp;nbsp;Thanx&lt;/P&gt;
&lt;P&gt;R&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;You must have run something different, as &lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/18408"&gt;@Ksharp&lt;/a&gt;'s code&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;

data have;
input Date :$20.      ID           ID_A    $    Quarter;
cards;
03JAN2017                      1                  A                   1
04JAN2017                      1                  A                   1
04APR2017                      1                  A                   2
10MAY2017                      1                  A                   2
09AUG2017                      1                  A                   3
16FEB2017                      1                  B                   1
18SEP2018                      1                  B                   7
13JAN2017                      2                  A                   1
15FEB2017                      2                  A                   1
21JUN2017                      2                  A                   2
14MAR2017                      2                  B                   1
21NOV2017                      2                  B                   4
;
run;
proc sort data=have(keep=id id_a quarter) out=temp nodupkey;
by id id_a quarter;
run;
data diff;
 merge temp temp(firstobs=2 rename=(id=_id id_a=_id_a quarter=_quarter));
 if id=_id and id_a=_id_a then diff=_quarter-quarter;
  else diff=-50000;
 drop _:;
run;
data want;
 merge have diff;
 by id id_a quarter;
run;

proc print data=want noobs;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;gives me the same result as mine:&lt;/P&gt;
&lt;PRE&gt;  Date       ID    ID_A    Quarter      diff

03JAN2017     1     A         1            1
04JAN2017     1     A         1            1
04APR2017     1     A         2            1
10MAY2017     1     A         2            1
09AUG2017     1     A         3       -50000
16FEB2017     1     B         1            6
18SEP2018     1     B         7       -50000
13JAN2017     2     A         1            1
15FEB2017     2     A         1            1
21JUN2017     2     A         2       -50000
14MAR2017     2     B         1            3
21NOV2017     2     B         4       -50000
&lt;/PRE&gt;
&lt;P&gt;which also matches your originally wanted result.&lt;/P&gt;</description>
      <pubDate>Mon, 03 Sep 2018 13:21:46 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Differences-between-quarters/m-p/492053#M129254</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2018-09-03T13:21:46Z</dc:date>
    </item>
  </channel>
</rss>

