<?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: Rolling StdDev of recent 5 obs in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Rolling-StdDev-of-recent-5-obs/m-p/645956#M193160</link>
    <description>&lt;P&gt;Just eliminate the first 4 records from the output data set. In that case, you would be much better off giving the output data set a different name than the input data set.&lt;/P&gt;</description>
    <pubDate>Thu, 07 May 2020 17:26:29 GMT</pubDate>
    <dc:creator>PaigeMiller</dc:creator>
    <dc:date>2020-05-07T17:26:29Z</dc:date>
    <item>
      <title>Rolling StdDev of recent 5 obs</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Rolling-StdDev-of-recent-5-obs/m-p/645953#M193158</link>
      <description>&lt;P&gt;I would like to calculate standard deviation of a variable for the recent five years. Proc Expand works pretty well, but not in a perfect way I expect it would.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;data prdsale;
	set sashelp.PrdSale;
	run;

proc sort data= prdsale nodupkey;
	by region division year quarter;
	run;

proc expand data= prdsale
	out= prdsale;
	by region division;
	convert actual= StdDev_actual/transformout= (MovStd 5); 
	run;
	
proc print ;
	var region division actual StdDev_Actual;
	run;&lt;/PRE&gt;
&lt;P&gt;The above code calculates the standard deviation of the variable, actual, using the most recent five data point. But, it also calculates Std.Dev. when there are less than 5 data point. For example, the third observation in the data has the data point in time t, t-1, and t-2 (but not in time t-3 and t-4). It still calculates the Std.Dev. of three data points. Can I just calculate it only when there are strictly five data point?&lt;/P&gt;</description>
      <pubDate>Thu, 07 May 2020 17:21:40 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Rolling-StdDev-of-recent-5-obs/m-p/645953#M193158</guid>
      <dc:creator>braam</dc:creator>
      <dc:date>2020-05-07T17:21:40Z</dc:date>
    </item>
    <item>
      <title>Re: Rolling StdDev of recent 5 obs</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Rolling-StdDev-of-recent-5-obs/m-p/645956#M193160</link>
      <description>&lt;P&gt;Just eliminate the first 4 records from the output data set. In that case, you would be much better off giving the output data set a different name than the input data set.&lt;/P&gt;</description>
      <pubDate>Thu, 07 May 2020 17:26:29 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Rolling-StdDev-of-recent-5-obs/m-p/645956#M193160</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2020-05-07T17:26:29Z</dc:date>
    </item>
    <item>
      <title>Re: Rolling StdDev of recent 5 obs</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Rolling-StdDev-of-recent-5-obs/m-p/645960#M193164</link>
      <description>Thanks for your suggestion. But my data includes some missing observations here and there, which makes this issue severer. So I was wondering if there is a general solution (or option) for this.</description>
      <pubDate>Thu, 07 May 2020 17:35:16 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Rolling-StdDev-of-recent-5-obs/m-p/645960#M193164</guid>
      <dc:creator>braam</dc:creator>
      <dc:date>2020-05-07T17:35:16Z</dc:date>
    </item>
    <item>
      <title>Re: Rolling StdDev of recent 5 obs</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Rolling-StdDev-of-recent-5-obs/m-p/645962#M193166</link>
      <description>&lt;P&gt;So what result do you want when there is not 5 data point (&lt;EM&gt;i.e.&lt;/EM&gt; at least one of the previous 5 are missing)?&lt;/P&gt;</description>
      <pubDate>Thu, 07 May 2020 17:39:04 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Rolling-StdDev-of-recent-5-obs/m-p/645962#M193166</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2020-05-07T17:39:04Z</dc:date>
    </item>
    <item>
      <title>Re: Rolling StdDev of recent 5 obs</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Rolling-StdDev-of-recent-5-obs/m-p/645967#M193169</link>
      <description>&lt;P&gt;Add the TRIM and NOMISS options to your CONVERT statement.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;See the explanation in Trick 4 in this paper or check the documentation.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;A href="https://support.sas.com/resources/papers/proceedings10/093-2010.pdf" target="_blank" rel="noopener"&gt;https://support.sas.com/resources/papers/proceedings10/093-2010.pdf&lt;/A&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;A href="https://documentation.sas.com/?docsetId=etsug&amp;amp;docsetTarget=etsug_expand_details19.htm&amp;amp;docsetVersion=15.1&amp;amp;locale=en"&gt;https://documentation.sas.com/?docsetId=etsug&amp;amp;docsetTarget=etsug_expand_details19.htm&amp;amp;docsetVersion=15.1&amp;amp;locale=en&lt;/A&gt;&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="delete_summary.JPG" style="width: 657px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/39175iF816EE97AD80CD27/image-size/large?v=v2&amp;amp;px=999" role="button" title="delete_summary.JPG" alt="delete_summary.JPG" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;NOMISS options, from documentation link above&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;BLOCKQUOTE&gt;
&lt;P&gt;The NOMISS operator does not perform any calculations, but serves to modify the operation of the moving time window operator that follows it. The NOMISS operator has no effect unless it is followed by a moving time window operator.&lt;/P&gt;
&lt;P&gt;For example, the following statement computes a five-period moving average of the variable&lt;SPAN&gt;&amp;nbsp;&lt;/SPAN&gt;&lt;CODE class="aa-varname"&gt;X&lt;/CODE&gt;&lt;SPAN&gt;&amp;nbsp;&lt;/SPAN&gt;but produces a missing value when any of the five values are missing:&lt;/P&gt;
&lt;PRE class="xisDoc-code"&gt;convert x=y / transformout=( nomiss movave 5 );
&lt;/PRE&gt;
&lt;P&gt;The following statement computes the cumulative sum of the variable&lt;SPAN&gt;&amp;nbsp;&lt;/SPAN&gt;&lt;CODE class="aa-varname"&gt;X&lt;/CODE&gt;&lt;SPAN&gt;&amp;nbsp;&lt;/SPAN&gt;but produces a missing value for all periods after the first missing&lt;SPAN&gt;&amp;nbsp;&lt;/SPAN&gt;&lt;CODE class="aa-varname"&gt;X&lt;/CODE&gt;&lt;SPAN&gt;&amp;nbsp;&lt;/SPAN&gt;value:&lt;/P&gt;
&lt;PRE class="xisDoc-code"&gt;convert x=y / transformout=( nomiss cusum );&lt;/PRE&gt;
&lt;/BLOCKQUOTE&gt;
&lt;P&gt;&amp;nbsp;EDITed to add NOMISS option.&lt;/P&gt;</description>
      <pubDate>Thu, 07 May 2020 17:53:59 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Rolling-StdDev-of-recent-5-obs/m-p/645967#M193169</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2020-05-07T17:53:59Z</dc:date>
    </item>
    <item>
      <title>Re: Rolling StdDev of recent 5 obs</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Rolling-StdDev-of-recent-5-obs/m-p/645969#M193171</link>
      <description>Then I would like to have a missing because data is not sufficient to calculate StdDev. &lt;BR /&gt;&lt;BR /&gt;The way I did (bypassed) is to generate another variable, which is equal to one if none of the five data point is missing. When this variable is equal to 1, I keep StdDev. But I believe that this is not straightforward, so I wanted to have an easier solution.</description>
      <pubDate>Thu, 07 May 2020 17:47:03 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Rolling-StdDev-of-recent-5-obs/m-p/645969#M193171</guid>
      <dc:creator>braam</dc:creator>
      <dc:date>2020-05-07T17:47:03Z</dc:date>
    </item>
    <item>
      <title>Re: Rolling StdDev of recent 5 obs</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Rolling-StdDev-of-recent-5-obs/m-p/645979#M193176</link>
      <description>Thanks a lot for your help. TRIMLEFT worked for me well!</description>
      <pubDate>Thu, 07 May 2020 18:05:05 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Rolling-StdDev-of-recent-5-obs/m-p/645979#M193176</guid>
      <dc:creator>braam</dc:creator>
      <dc:date>2020-05-07T18:05:05Z</dc:date>
    </item>
    <item>
      <title>Re: Rolling StdDev of recent 5 obs</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Rolling-StdDev-of-recent-5-obs/m-p/646025#M193199</link>
      <description>&lt;P&gt;For those who will see this post:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;1. trimleft doesn' handle missing values well.&lt;/P&gt;
&lt;P&gt;2. nomiss should be used for handling missing values.&lt;/P&gt;</description>
      <pubDate>Thu, 07 May 2020 20:12:43 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Rolling-StdDev-of-recent-5-obs/m-p/646025#M193199</guid>
      <dc:creator>braam</dc:creator>
      <dc:date>2020-05-07T20:12:43Z</dc:date>
    </item>
    <item>
      <title>Re: Rolling StdDev of recent 5 obs</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Rolling-StdDev-of-recent-5-obs/m-p/646178#M193257</link>
      <description>&lt;P&gt;SQL can solve it easily .&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data air;
 set sashelp.air;
t+1;
run;
proc sql;
create table want as
select *,case
when (select count(*) from air where t between a.t-4 and a.t)&amp;lt;5 then .
else (select std(air) from air where t between a.t-4 and a.t) 
end as rolling_std
 from air as a;
quit;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Fri, 08 May 2020 12:04:21 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Rolling-StdDev-of-recent-5-obs/m-p/646178#M193257</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2020-05-08T12:04:21Z</dc:date>
    </item>
    <item>
      <title>Re: Rolling StdDev of recent 5 obs</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Rolling-StdDev-of-recent-5-obs/m-p/646179#M193258</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;
data prdsale;
	set sashelp.PrdSale;
	run;

proc sort data= prdsale nodupkey;
	by region division year quarter;
	run;
data air;
 set prdsale;
 by region division;
 if first.division then t=0;
 t+1;
run;

proc sql;
create table want as
select *,case
when (select count(*) from air
where region=a.region and division=a.division and t between a.t-4 and a.t)&amp;lt;5 then .
else (select std(actual) from air where
region=a.region and division=a.division and t between a.t-4 and a.t) 
end as rolling_std
 from air as a;
quit;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Fri, 08 May 2020 12:10:24 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Rolling-StdDev-of-recent-5-obs/m-p/646179#M193258</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2020-05-08T12:10:24Z</dc:date>
    </item>
  </channel>
</rss>

