<?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: Calculating a rolling median with varying numbers of observations in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Calculating-a-rolling-median-with-varying-numbers-of/m-p/632005#M187324</link>
    <description>&lt;P&gt;Sorry, the data for each month is quite extensive so I just posted the first few lines.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;An input (very&amp;nbsp;condensed)&amp;nbsp;would look something like this:&lt;/P&gt;&lt;P&gt;AUG2015&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 26&lt;BR /&gt;AUG2015&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 27&lt;BR /&gt;SEP2015&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;16&lt;BR /&gt;SEP2015&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 20&lt;BR /&gt;SEP2015&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;7&lt;BR /&gt;OCT2015&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;22&lt;BR /&gt;NOV2015&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 28&lt;BR /&gt;NOV2015&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 28&lt;BR /&gt;NOV2015&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 35&lt;BR /&gt;NOV2015&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 15&lt;BR /&gt;DEC2015&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 28&lt;BR /&gt;DEC2015&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 8&lt;BR /&gt;DEC2015&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 14&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;The idea is to obtain something like this:&lt;/P&gt;&lt;P&gt;AUG2015&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; ..&lt;/P&gt;&lt;P&gt;SEP2015&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; ..&lt;/P&gt;&lt;P&gt;OCT2015&amp;nbsp;&amp;nbsp;&amp;nbsp; 21&lt;/P&gt;&lt;P&gt;NOV2015&amp;nbsp;&amp;nbsp;&amp;nbsp; 21&lt;/P&gt;&lt;P&gt;DEC2015&amp;nbsp;&amp;nbsp;&amp;nbsp; 25&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;As I mentioned in my original comment I think what I need to do is assign a group to each month, something along the lines of:&lt;/P&gt;&lt;P&gt;data outfile_test3;&lt;BR /&gt;set outfile_test2;&lt;BR /&gt;by END_DATE;&lt;BR /&gt;retain count 0;&lt;BR /&gt;if first.END_DATE then count=count+1;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;This begins to count, but it counts the day as a new date (so it adds a count for the 2nd Aug 2015 etc). I can make a new variable using either MONTH or YEAR functions and it seems to be working correctly, but I'm not sure exactly how to combine the two.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I'm also not sure exactly how to calculate the 'window' of medians. I think the code I'm after is using a lag function but I don't really know how to write it.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Sorry for how basic this work through is, but I am very new SAS!&lt;/P&gt;</description>
    <pubDate>Fri, 13 Mar 2020 18:03:46 GMT</pubDate>
    <dc:creator>J_Brown</dc:creator>
    <dc:date>2020-03-13T18:03:46Z</dc:date>
    <item>
      <title>Calculating a rolling median with varying numbers of observations</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Calculating-a-rolling-median-with-varying-numbers-of/m-p/631976#M187303</link>
      <description>&lt;P&gt;I have a problem that is ostensibly simple, but I'm really struggling with it.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;In my data I have two columns that are important: END_DATE and DURATION. END_DATE is a date variable formatted as monyy7 (but does contain the day of the month as well).&amp;nbsp;&lt;/P&gt;&lt;P&gt;The data looks like this:&lt;/P&gt;&lt;P&gt;AUG2015&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 26&lt;BR /&gt;AUG2015&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 27&lt;BR /&gt;AUG2015&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;16&lt;BR /&gt;AUG2015&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 20&lt;BR /&gt;AUG2015&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;7&lt;BR /&gt;AUG2015&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;22&lt;BR /&gt;AUG2015&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 28&lt;BR /&gt;AUG2015&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 28&lt;BR /&gt;AUG2015&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 35&lt;BR /&gt;AUG2015&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 15&lt;BR /&gt;AUG2015&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 28&lt;BR /&gt;AUG2015&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 8&lt;BR /&gt;AUG2015&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 14&lt;/P&gt;&lt;P&gt;With these I want to calculate a 3 month moving median. It's easy to calculate the median for the months, but I'm not sure how to do a rolling median.&lt;/P&gt;&lt;P&gt;From googling I found PROC EXPAND, but this requires non-duplicate variables. This is a problem as for each month we have lots of durations (as you can see from the example). I can't calculate the median for each month and then use PROC EXPAND as a median of a median won't be an&amp;nbsp;accurate measure.&lt;/P&gt;&lt;P&gt;I think I need create a new variable where each month is assigned a group but I'm unsure how to do this.&lt;/P&gt;&lt;P&gt;ie. AUG2015 =0, SEP2015 =1,&amp;nbsp;OCT2015=2&amp;nbsp;etc&lt;/P&gt;&lt;P&gt;Once I have that I assume that I could calculate the rolling median using some sort of lag function?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I'm new to SAS, so apologies if this question is very trivial!&lt;/P&gt;</description>
      <pubDate>Fri, 13 Mar 2020 16:57:19 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Calculating-a-rolling-median-with-varying-numbers-of/m-p/631976#M187303</guid>
      <dc:creator>J_Brown</dc:creator>
      <dc:date>2020-03-13T16:57:19Z</dc:date>
    </item>
    <item>
      <title>Re: Calculating a rolling median with varying numbers of observations</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Calculating-a-rolling-median-with-varying-numbers-of/m-p/631984#M187307</link>
      <description>Can you provide a fully worked example please?&lt;BR /&gt;Show your input data and expected output. The data you've posted for a single month so it isn't really reflective of your problem that I can see.</description>
      <pubDate>Fri, 13 Mar 2020 17:14:05 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Calculating-a-rolling-median-with-varying-numbers-of/m-p/631984#M187307</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2020-03-13T17:14:05Z</dc:date>
    </item>
    <item>
      <title>Re: Calculating a rolling median with varying numbers of observations</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Calculating-a-rolling-median-with-varying-numbers-of/m-p/632005#M187324</link>
      <description>&lt;P&gt;Sorry, the data for each month is quite extensive so I just posted the first few lines.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;An input (very&amp;nbsp;condensed)&amp;nbsp;would look something like this:&lt;/P&gt;&lt;P&gt;AUG2015&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 26&lt;BR /&gt;AUG2015&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 27&lt;BR /&gt;SEP2015&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;16&lt;BR /&gt;SEP2015&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 20&lt;BR /&gt;SEP2015&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;7&lt;BR /&gt;OCT2015&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;22&lt;BR /&gt;NOV2015&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 28&lt;BR /&gt;NOV2015&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 28&lt;BR /&gt;NOV2015&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 35&lt;BR /&gt;NOV2015&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 15&lt;BR /&gt;DEC2015&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 28&lt;BR /&gt;DEC2015&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 8&lt;BR /&gt;DEC2015&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 14&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;The idea is to obtain something like this:&lt;/P&gt;&lt;P&gt;AUG2015&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; ..&lt;/P&gt;&lt;P&gt;SEP2015&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; ..&lt;/P&gt;&lt;P&gt;OCT2015&amp;nbsp;&amp;nbsp;&amp;nbsp; 21&lt;/P&gt;&lt;P&gt;NOV2015&amp;nbsp;&amp;nbsp;&amp;nbsp; 21&lt;/P&gt;&lt;P&gt;DEC2015&amp;nbsp;&amp;nbsp;&amp;nbsp; 25&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;As I mentioned in my original comment I think what I need to do is assign a group to each month, something along the lines of:&lt;/P&gt;&lt;P&gt;data outfile_test3;&lt;BR /&gt;set outfile_test2;&lt;BR /&gt;by END_DATE;&lt;BR /&gt;retain count 0;&lt;BR /&gt;if first.END_DATE then count=count+1;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;This begins to count, but it counts the day as a new date (so it adds a count for the 2nd Aug 2015 etc). I can make a new variable using either MONTH or YEAR functions and it seems to be working correctly, but I'm not sure exactly how to combine the two.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I'm also not sure exactly how to calculate the 'window' of medians. I think the code I'm after is using a lag function but I don't really know how to write it.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Sorry for how basic this work through is, but I am very new SAS!&lt;/P&gt;</description>
      <pubDate>Fri, 13 Mar 2020 18:03:46 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Calculating-a-rolling-median-with-varying-numbers-of/m-p/632005#M187324</guid>
      <dc:creator>J_Brown</dc:creator>
      <dc:date>2020-03-13T18:03:46Z</dc:date>
    </item>
    <item>
      <title>Re: Calculating a rolling median with varying numbers of observations</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Calculating-a-rolling-median-with-varying-numbers-of/m-p/632014#M187330</link>
      <description>&lt;P&gt;PROC SUMMARY to get the median by each month in a data set.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Then PROC EXPAND to get the moving median of the last N months. An example of a moving average of 3 months is shown here: &lt;A href="https://documentation.sas.com/?docsetId=etsug&amp;amp;docsetVersion=15.1&amp;amp;docsetTarget=etsug_expand_examples04.htm&amp;amp;locale=en" target="_blank"&gt;https://documentation.sas.com/?docsetId=etsug&amp;amp;docsetVersion=15.1&amp;amp;docsetTarget=etsug_expand_examples04.htm&amp;amp;locale=en&lt;/A&gt;, you want a moving median which is MOVMED instaed of MOVAVE.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 13 Mar 2020 18:27:21 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Calculating-a-rolling-median-with-varying-numbers-of/m-p/632014#M187330</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2020-03-13T18:27:21Z</dc:date>
    </item>
    <item>
      <title>Re: Calculating a rolling median with varying numbers of observations</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Calculating-a-rolling-median-with-varying-numbers-of/m-p/632034#M187338</link>
      <description>So do you want each month to be calculated individually first and then combined to a three month median or should your three month median include the repeat measures as well? The numbers shown in your example are EXACTLY what you expect to have returned?</description>
      <pubDate>Fri, 13 Mar 2020 19:06:55 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Calculating-a-rolling-median-with-varying-numbers-of/m-p/632034#M187338</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2020-03-13T19:06:55Z</dc:date>
    </item>
    <item>
      <title>Re: Calculating a rolling median with varying numbers of observations</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Calculating-a-rolling-median-with-varying-numbers-of/m-p/632041#M187339</link>
      <description>&lt;P&gt;Convert your months to periods as you indicated, follow the instructions here for that:&lt;/P&gt;
&lt;P&gt;&lt;A href="https://stats.idre.ucla.edu/sas/faq/how-can-i-create-an-enumeration-variable-by-groups/" target="_blank"&gt;https://stats.idre.ucla.edu/sas/faq/how-can-i-create-an-enumeration-variable-by-groups/&lt;/A&gt;.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Then you can use this macro here with a window of 3. There are probably more optimal ways than this but this will work for your example.It assumes your variable name is X. Replace that as needed.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql noprint;
select min(period) into :min_period TRIMMED
from have;
select max(period) into :max_period TRIMMED
from have;
quit;

%put &amp;amp;min_period;
%put &amp;amp;max_period;

options nomprint nosymbolgen;

%macro moving_summary(datain=, window=, dataout=);

*remove previous summary table;
proc sql;
drop table &amp;amp;dataout;
quit;

%do i=&amp;amp;min_period. %to %eval(&amp;amp;max_period.-&amp;amp;window.);
proc means data=&amp;amp;datain noprint;
var x;
where period between &amp;amp;i and %eval(&amp;amp;i. + &amp;amp;window. - 1);
output out=_temp n=n_x mean=mean_x std=std_x stderr=stderr_x median = median_x;
run;

data _temp;
set _temp;
start = &amp;amp;i;
end = &amp;amp;i + &amp;amp;window - 1;
run;

proc append base=&amp;amp;dataout data=_temp;
run;

proc sql;
drop table _temp;
quit;

%end;
%mend;

%moving_summary(datain=have, window=6, dataout=summary_stats);

proc means data=have;
where period between 1 and 6;
var x;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Fri, 13 Mar 2020 19:19:12 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Calculating-a-rolling-median-with-varying-numbers-of/m-p/632041#M187339</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2020-03-13T19:19:12Z</dc:date>
    </item>
    <item>
      <title>Re: Calculating a rolling median with varying numbers of observations</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Calculating-a-rolling-median-with-varying-numbers-of/m-p/632108#M187368</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/13879"&gt;@Reeza&lt;/a&gt; 's suggestion made me recall a technique I have sometimes used for rolling windows - namely to output a copy of each data record once for each window in which it belongs (and assigning a consistent ending date for each window).&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Then run a proc summary or proc means using the window end date as a class var:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
  input  date :monyy7. x;
  format date date9.;
datalines;
AUG2015       26
AUG2015       27
SEP2015       16
SEP2015       20
SEP2015        7
OCT2015       22
NOV2015       28
NOV2015       28
NOV2015       35
NOV2015       15
DEC2015       28
DEC2015        8
DEC2015       14
;
data vneed / view=vneed;
  set have nobs=nrecs;
  if _n_=1 then do;
     /*Generate the window end date needed for the 1st 3-month window*/
     set have (keep=date rename=(date=first_date));
     first_date=intnx('month',first_date,2,'end');   

     /*Generate the window end date needed for the last 3-month window */
     set have (keep=date rename=(date=last_date)) point=nrecs;
     last_date=intnx('month',last_date,0,'end');
  end;

  /* Write a record for each window it belongs to */
  do m=0 to 2;  
    window_enddate=intnx('month',date,m,'end');
    if first_date&amp;lt;=window_enddate&amp;lt;=last_date then output;
  end;
  format window_enddate date9.;
run;
proc means data=vneed median ;
  var x;
  class window_enddate;
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;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sat, 14 Mar 2020 05:35:11 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Calculating-a-rolling-median-with-varying-numbers-of/m-p/632108#M187368</guid>
      <dc:creator>mkeintz</dc:creator>
      <dc:date>2020-03-14T05:35:11Z</dc:date>
    </item>
  </channel>
</rss>

