<?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: SAS - calculate average values of previous n quarters in New SAS User</title>
    <link>https://communities.sas.com/t5/New-SAS-User/SAS-calculate-average-values-of-previous-n-quarters/m-p/606095#M17340</link>
    <description>&lt;P&gt;Thank you Paige! How can I merge it with my original dataset? And also how can I get it for the previous quarters&amp;nbsp; as well?&lt;/P&gt;</description>
    <pubDate>Thu, 21 Nov 2019 12:38:08 GMT</pubDate>
    <dc:creator>adrfinance</dc:creator>
    <dc:date>2019-11-21T12:38:08Z</dc:date>
    <item>
      <title>SAS - calculate average values of previous n quarters</title>
      <link>https://communities.sas.com/t5/New-SAS-User/SAS-calculate-average-values-of-previous-n-quarters/m-p/606085#M17334</link>
      <description>&lt;P&gt;I have a SAS dataset that looks like:&lt;/P&gt;&lt;PRE&gt;&lt;CODE&gt;             Obs    qy         Time       beginning   end                   

               1    2008 Q1    2008Q1      1           1                    
               2    2008 Q1    2008Q1      1           2                    
               3    2008 Q2    2008Q2      1           1                    
               4    2008 Q2    2008Q2      1           2                    
               5    2008 Q2    2008Q2      1           1                    
               6    2008 Q3    2008Q3      1           1                    
               7    2008 Q4    2008Q4      1           1                    
               8    2008 Q4    2008Q4      1           2                    
               9    2008 Q4    2008Q4      1           1                    
              10    2009 Q1    2009Q1      1           1   &lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;and I would like to have some new variables containing the accounts that had beginning=1 and end=2 per quarter, for the n previous quarters (n could be 1,2,3,4 etc). How can I do that?&lt;/P&gt;&lt;P&gt;Desired output:&lt;/P&gt;&lt;PRE&gt;&lt;CODE&gt;             Obs    qy         Time       beginning   end      one_quarter_back             

               1    2008 Q1    2008Q1      1           1   0.25                  
               2    2008 Q1    2008Q1      1           2   0.25                 
               3    2008 Q2    2008Q2      1           1   0.5                 
               4    2008 Q2    2008Q2      1           2   0.5              
               5    2008 Q2    2008Q2      1           1   0.5              
               6    2008 Q3    2008Q3      1           1   0.33              
               7    2008 Q4    2008Q4      1           1   0.4             
               8    2008 Q4    2008Q4      1           2   0.4              
               9    2008 Q4    2008Q4      1           1   0.4                 
              10    2009 Q1    2009Q1      1           1   0.15&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;(The above assuming that the respective calculations led to these numbers, and we would have similar for 2,3,4 etc quarters back)&lt;/P&gt;</description>
      <pubDate>Thu, 21 Nov 2019 12:05:35 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/SAS-calculate-average-values-of-previous-n-quarters/m-p/606085#M17334</guid>
      <dc:creator>adrfinance</dc:creator>
      <dc:date>2019-11-21T12:05:35Z</dc:date>
    </item>
    <item>
      <title>Re: SAS - calculate average values of previous n quarters</title>
      <link>https://communities.sas.com/t5/New-SAS-User/SAS-calculate-average-values-of-previous-n-quarters/m-p/606092#M17337</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/300616"&gt;@adrfinance&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;I have a SAS dataset that looks like:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE&gt;             Obs    qy         Time       beginning   end                   

               1    2008 Q1    2008Q1      1           1                    
               2    2008 Q1    2008Q1      1           2                    
               3    2008 Q2    2008Q2      1           1                    
               4    2008 Q2    2008Q2      1           2                    
               5    2008 Q2    2008Q2      1           1                    
               6    2008 Q3    2008Q3      1           1                    
               7    2008 Q4    2008Q4      1           1                    
               8    2008 Q4    2008Q4      1           2                    
               9    2008 Q4    2008Q4      1           1                    
              10    2009 Q1    2009Q1      1           1   &lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;and I would like to have some new variables containing the accounts that had beginning=1 and end=2 per quarter, for the n previous quarters (n could be 1,2,3,4 etc). How can I do that?&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;"Accounts"? I'm not sure what that word is referring to. There is no obvious "account" variable.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Can you further explain the part about "had beginning=1 and end=2 per quarter"?&amp;nbsp; Because for 2008Q3, this condition is not met, yet 2008Q3 winds up on the output data set.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;BLOCKQUOTE&gt;
&lt;P&gt;Desired output:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE&gt;             Obs    qy         Time       beginning   end      one_quarter_back             

               1    2008 Q1    2008Q1      1           1   0.25                  
               2    2008 Q1    2008Q1      1           2   0.25                 
               3    2008 Q2    2008Q2      1           1   0.5                 
               4    2008 Q2    2008Q2      1           2   0.5              
               5    2008 Q2    2008Q2      1           1   0.5              
               6    2008 Q3    2008Q3      1           1   0.33              
               7    2008 Q4    2008Q4      1           1   0.4             
               8    2008 Q4    2008Q4      1           2   0.4              
               9    2008 Q4    2008Q4      1           1   0.4                 
              10    2009 Q1    2009Q1      1           1   0.15&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;(The above assuming that the respective calculations led to these numbers, and we would have similar for 2,3,4 etc quarters back)&lt;/P&gt;
&lt;/BLOCKQUOTE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Is beginning always equal to 1? Can you provide a more "realistic" example, where beginning is not always equal to 1? Or at least provide data that matches the explanation so that "accounts" are clear? Also, please explain the method of calculation that results in a value of 0.25 in the first row under ONE_QUARTER_BACK.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Thanks.&lt;/P&gt;</description>
      <pubDate>Thu, 21 Nov 2019 12:23:22 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/SAS-calculate-average-values-of-previous-n-quarters/m-p/606092#M17337</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2019-11-21T12:23:22Z</dc:date>
    </item>
    <item>
      <title>Re: SAS - calculate average values of previous n quarters</title>
      <link>https://communities.sas.com/t5/New-SAS-User/SAS-calculate-average-values-of-previous-n-quarters/m-p/606093#M17338</link>
      <description>&lt;P&gt;Account = one row of data in this case...&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;the beginning is always equal to 1.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I want to count the percentage of accounts that have beginning equal to 1 and end equal to 2, per quarter.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE&gt;             Obs    qy         Time       beginning   end      one_quarter_back             

               1    2008 Q1    2008Q1      1           1   0.5                  
               2    2008 Q1    2008Q1      1           2   0.5                 
               3    2008 Q2    2008Q2      1           1   0.33                 
               4    2008 Q2    2008Q2      1           2   0.33             
               5    2008 Q2    2008Q2      1           1   0.33              
               6    2008 Q3    2008Q3      1           1   0              
               7    2008 Q4    2008Q4      1           1   0.33             
               8    2008 Q4    2008Q4      1           2   0.33              
               9    2008 Q4    2008Q4      1           1   0                 
              10    2009 Q1    2009Q1      1           1   0&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;This would be the correct table if these were my only data&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 21 Nov 2019 12:25:06 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/SAS-calculate-average-values-of-previous-n-quarters/m-p/606093#M17338</guid>
      <dc:creator>adrfinance</dc:creator>
      <dc:date>2019-11-21T12:25:06Z</dc:date>
    </item>
    <item>
      <title>Re: SAS - calculate average values of previous n quarters</title>
      <link>https://communities.sas.com/t5/New-SAS-User/SAS-calculate-average-values-of-previous-n-quarters/m-p/606094#M17339</link>
      <description>&lt;P&gt;Thank you, that's much clearer now.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have2;
    set have;
    /* Since beginning is always equal to 1, I don't use it here in my code */
    if /* beginning=1 and */ end=2 then flag=1;
    else flag=0;
run;
proc summary nway data=have2;
    class qy;
    var flag;
    output out=want mean=one_quarter_back;
run;
    &lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;If you want, you can merge output data set WANT back in with the original data set HAVE.&lt;/P&gt;</description>
      <pubDate>Thu, 21 Nov 2019 12:30:26 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/SAS-calculate-average-values-of-previous-n-quarters/m-p/606094#M17339</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2019-11-21T12:30:26Z</dc:date>
    </item>
    <item>
      <title>Re: SAS - calculate average values of previous n quarters</title>
      <link>https://communities.sas.com/t5/New-SAS-User/SAS-calculate-average-values-of-previous-n-quarters/m-p/606095#M17340</link>
      <description>&lt;P&gt;Thank you Paige! How can I merge it with my original dataset? And also how can I get it for the previous quarters&amp;nbsp; as well?&lt;/P&gt;</description>
      <pubDate>Thu, 21 Nov 2019 12:38:08 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/SAS-calculate-average-values-of-previous-n-quarters/m-p/606095#M17340</guid>
      <dc:creator>adrfinance</dc:creator>
      <dc:date>2019-11-21T12:38:08Z</dc:date>
    </item>
    <item>
      <title>Re: SAS - calculate average values of previous n quarters</title>
      <link>https://communities.sas.com/t5/New-SAS-User/SAS-calculate-average-values-of-previous-n-quarters/m-p/606098#M17342</link>
      <description>&lt;P&gt;You do a DATA step MERGE&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I'm still not sure what you mean by "how can I get it for the previous quarters as well?" Can you give an example?&lt;/P&gt;</description>
      <pubDate>Thu, 21 Nov 2019 12:48:52 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/SAS-calculate-average-values-of-previous-n-quarters/m-p/606098#M17342</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2019-11-21T12:48:52Z</dc:date>
    </item>
    <item>
      <title>Re: SAS - calculate average values of previous n quarters</title>
      <link>https://communities.sas.com/t5/New-SAS-User/SAS-calculate-average-values-of-previous-n-quarters/m-p/606100#M17343</link>
      <description>&lt;PRE&gt;&lt;CODE&gt;             Obs    qy         Time       beginning   end      one_quarter_back    two_quarters_back         

               1    2008 Q1    2008Q1      1           1   0.5                     .
               2    2008 Q1    2008Q1      1           2   0.5                     .
               3    2008 Q2    2008Q2      1           1   0.33                    0.5
               4    2008 Q2    2008Q2      1           2   0.33                    0.5
               5    2008 Q2    2008Q2      1           1   0.33                    0.5
               6    2008 Q3    2008Q3      1           1   0                       0.33
               7    2008 Q4    2008Q4      1           1   0.33                    0
               8    2008 Q4    2008Q4      1           2   0.33                    0
               9    2008 Q4    2008Q4      1           1   0                       0.33
              10    2009 Q1    2009Q1      1           1   0&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;0.33&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;this is what two_quarters_back would look like. How do I do a DATA step MERGE?&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 21 Nov 2019 12:52:01 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/SAS-calculate-average-values-of-previous-n-quarters/m-p/606100#M17343</guid>
      <dc:creator>adrfinance</dc:creator>
      <dc:date>2019-11-21T12:52:01Z</dc:date>
    </item>
    <item>
      <title>Re: SAS - calculate average values of previous n quarters</title>
      <link>https://communities.sas.com/t5/New-SAS-User/SAS-calculate-average-values-of-previous-n-quarters/m-p/606103#M17344</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
input Obs    qy   :yyq.     Time    :yyq.     beginning   end   ;  
format qy yyq.; 
drop time;
cards;
               1    2008Q1    2008Q1      1           1                    
               2    2008Q1    2008Q1      1           2                    
               3    2008Q2    2008Q2      1           1                    
               4    2008Q2    2008Q2      1           2                    
               5    2008Q2    2008Q2      1           1                    
               6    2008Q3    2008Q3      1           1                    
               7    2008Q4    2008Q4      1           1                    
               8    2008Q4    2008Q4      1           2                    
               9    2008Q4    2008Q4      1           1                    
              10    2009Q1    2009Q1      1           1   
;
proc sql;
create table want as
select *,

(select sum(beginning=1 and end=2) from have 
where qy between intnx('qtr',a.qy,-1) and a.qy)/
(select count(*) from have 
where qy between intnx('qtr',a.qy,-1) and a.qy) as one_quarter_back  ,


(select sum(beginning=1 and end=2) from have 
where qy between intnx('qtr',a.qy,-2) and a.qy)/
(select count(*) from have 
where qy between intnx('qtr',a.qy,-2) and a.qy) as two_quarter_back  ,


(select sum(beginning=1 and end=2) from have 
where qy between intnx('qtr',a.qy,-3) and a.qy)/
(select count(*) from have 
where qy between intnx('qtr',a.qy,-3) and a.qy) as three_quarter_back  

 from have as a;
quit;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Thu, 21 Nov 2019 12:53:22 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/SAS-calculate-average-values-of-previous-n-quarters/m-p/606103#M17344</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2019-11-21T12:53:22Z</dc:date>
    </item>
    <item>
      <title>Re: SAS - calculate average values of previous n quarters</title>
      <link>https://communities.sas.com/t5/New-SAS-User/SAS-calculate-average-values-of-previous-n-quarters/m-p/606106#M17345</link>
      <description>&lt;P&gt;I get the following error:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;ERROR: Function INTNX requires a numeric expression as argument 2.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;probably qy is not numeric?&lt;/P&gt;</description>
      <pubDate>Thu, 21 Nov 2019 13:00:41 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/SAS-calculate-average-values-of-previous-n-quarters/m-p/606106#M17345</guid>
      <dc:creator>adrfinance</dc:creator>
      <dc:date>2019-11-21T13:00:41Z</dc:date>
    </item>
    <item>
      <title>Re: SAS - calculate average values of previous n quarters</title>
      <link>https://communities.sas.com/t5/New-SAS-User/SAS-calculate-average-values-of-previous-n-quarters/m-p/606108#M17346</link>
      <description>&lt;P&gt;DATA step merge&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data merged;
     merge have want;
     by qy;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;But don't do this if you want the additional quarters.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Instead, to get the additional quarters into the results, do this:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have2;
    set have;
    /* Since beginning is always equal to 1, I don't use it here in my code */
    if /* beginning=1 and */ end=2 then flag=1;
    else flag=0;
run;
proc summary nway data=have2;
    class qy;
    var flag;
    output out=want mean=one_quarter_back;
run;
data want2;
     set want;
     two_quarter_back=lag(one_quarter_back);
     three_quarter_back=lag2(one_quarter_back);
     four_quarter_back=lag3(one_quarter_back);
run;
data merged;
    merge have want2;
    by qy;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 21 Nov 2019 13:06:56 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/SAS-calculate-average-values-of-previous-n-quarters/m-p/606108#M17346</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2019-11-21T13:06:56Z</dc:date>
    </item>
    <item>
      <title>Re: SAS - calculate average values of previous n quarters</title>
      <link>https://communities.sas.com/t5/New-SAS-User/SAS-calculate-average-values-of-previous-n-quarters/m-p/606110#M17348</link>
      <description>&lt;P&gt;Paige, In the last step you use want2 and want but you haven't defined these, right?&lt;/P&gt;</description>
      <pubDate>Thu, 21 Nov 2019 13:13:40 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/SAS-calculate-average-values-of-previous-n-quarters/m-p/606110#M17348</guid>
      <dc:creator>adrfinance</dc:creator>
      <dc:date>2019-11-21T13:13:40Z</dc:date>
    </item>
  </channel>
</rss>

