<?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 standard deviation set the starting point in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/rolling-standard-deviation-set-the-starting-point/m-p/320798#M70706</link>
    <description>&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/120563"&gt;@Jennifer925&lt;/a&gt; wrote:&lt;BR /&gt;
&lt;P&gt;proc sql;&lt;BR /&gt;select *,case when (select count(*) from have where year lt a.year) le &lt;FONT color="#FF0000"&gt;&lt;STRONG&gt;5&lt;/STRONG&gt; &lt;/FONT&gt;then .&lt;BR /&gt;else (select var(x) from have where year between a.year-2 and a.year) end as var&lt;BR /&gt;from have as a;&lt;BR /&gt;quit;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I just used this code get the result I wanted. But I am trying to use a function to describe the number &lt;STRONG&gt;&lt;FONT color="#FF0000"&gt;5&lt;/FONT&gt;&lt;/STRONG&gt;( count (X=0)before starting point plus 1),&amp;nbsp; I have a lot of different categories and their starting points are different. Could you help me out of these problem? Thanks.&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;Do you have a SAS ETS licence?&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;Run the following to see:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;proc setinit;&lt;/P&gt;
&lt;P&gt;run;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If you do, proc Expand is your best bet, see the third example in the documentation. &amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Thu, 22 Dec 2016 18:45:18 GMT</pubDate>
    <dc:creator>Reeza</dc:creator>
    <dc:date>2016-12-22T18:45:18Z</dc:date>
    <item>
      <title>rolling standard deviation set the starting point</title>
      <link>https://communities.sas.com/t5/SAS-Programming/rolling-standard-deviation-set-the-starting-point/m-p/320647#M70645</link>
      <description>&lt;P&gt;How to use the proc SQL to get the rolling 3 years standard deviation and set the starting year is 1984(exclude the first three zero value of X but keep the zero value of year 1986) ? Thanks a lot.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;data have;
infile cards dlm=',' truncover;
input  year x;
cards;
1980,0
1981,0
1982,0
1983,0
1984,5
1985,7
1986,0
1987,6
1988,2
1989,1

;
run;
&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 21 Dec 2016 23:16:02 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/rolling-standard-deviation-set-the-starting-point/m-p/320647#M70645</guid>
      <dc:creator>Jennifer925</dc:creator>
      <dc:date>2016-12-21T23:16:02Z</dc:date>
    </item>
    <item>
      <title>Re: rolling standard deviation set the starting point</title>
      <link>https://communities.sas.com/t5/SAS-Programming/rolling-standard-deviation-set-the-starting-point/m-p/320649#M70646</link>
      <description>&lt;P&gt;Do you have SAS ETS? That's a good way to calculate moving statistics.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 21 Dec 2016 23:14:01 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/rolling-standard-deviation-set-the-starting-point/m-p/320649#M70646</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2016-12-21T23:14:01Z</dc:date>
    </item>
    <item>
      <title>Re: rolling standard deviation set the starting point</title>
      <link>https://communities.sas.com/t5/SAS-Programming/rolling-standard-deviation-set-the-starting-point/m-p/320652#M70647</link>
      <description>&lt;P&gt;SQL really doesn't have a good concept of order in the data ("first&amp;nbsp;3")&amp;nbsp;other than to apply a sort order on output. I am not quite sure how to interpret what to keep/exclude.&lt;/P&gt;
&lt;P&gt;Does this get a start on what you want:&lt;/P&gt;
&lt;PRE&gt;data want;
   set have;
   lx1=lag(x);lx2=lag2(x);lx3=lag3(x);
   STD3yr = std(lx1,lx2,lx3);
   drop lx:;
run;&lt;/PRE&gt;</description>
      <pubDate>Wed, 21 Dec 2016 23:33:12 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/rolling-standard-deviation-set-the-starting-point/m-p/320652#M70647</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2016-12-21T23:33:12Z</dc:date>
    </item>
    <item>
      <title>Re: rolling standard deviation set the starting point</title>
      <link>https://communities.sas.com/t5/SAS-Programming/rolling-standard-deviation-set-the-starting-point/m-p/320654#M70648</link>
      <description>&lt;P&gt;Here are two other ways, which account for the 0 not being included.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;One is SQL and one is temporary arrays, each has it's own benefits.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
infile cards dlm=',' truncover;
input year x;
cards;
1980,0
1981,0
1982,0
1983,0
1984,5
1985,7
1986,0
1987,6
1988,2
1989,1
;
run;

/*Using a temporary array  - this assumes you have all records*/
data want1;
array p{0:2} _temporary_;
set have; 
p{mod(_n_,3)} = x;
if nmiss(of p(*)) = 0 and max(of p(*)) ne 0 then stdev = std(of p{*});

run;


proc print data=want1; run;

/*SQL approach*/
proc sql;
create table want2 as
select a.year, a.x, case when count(b.x) =3 and max(b.x) ne 0 then std(b.x) 
else . end as stdev 
from have as a
left join have as b
on b.year between a.year-2 and a.year
group by a.year, a.x;
quit;

proc print data=want2; run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 21 Dec 2016 23:39:29 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/rolling-standard-deviation-set-the-starting-point/m-p/320654#M70648</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2016-12-21T23:39:29Z</dc:date>
    </item>
    <item>
      <title>Re: rolling standard deviation set the starting point</title>
      <link>https://communities.sas.com/t5/SAS-Programming/rolling-standard-deviation-set-the-starting-point/m-p/320675#M70655</link>
      <description>&lt;PRE&gt;
Do you have to use SQL ? 




data have;
infile cards dlm=',' truncover;
input  year x;
cards;
1980,0
1981,0
1982,0
1983,0
1984,5
1985,7
1986,0
1987,6
1988,2
1989,1

;
run;
proc sql;
select *,case when (select count(*) from have where year lt a.year) le 4 then .
         else (select std(x) from have where year between a.year-2 and a.year) end as std
 from have as a;
quit;
&lt;/PRE&gt;</description>
      <pubDate>Thu, 22 Dec 2016 05:55:52 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/rolling-standard-deviation-set-the-starting-point/m-p/320675#M70655</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2016-12-22T05:55:52Z</dc:date>
    </item>
    <item>
      <title>Re: rolling standard deviation set the starting point</title>
      <link>https://communities.sas.com/t5/SAS-Programming/rolling-standard-deviation-set-the-starting-point/m-p/320760#M70691</link>
      <description>&lt;P&gt;The result is a little bit different. I am thinking that the first stdandard deviation is for 5,7,and 0(starting from 1984 to 1986), the second one should be for 7,0 and 6( from 1985 to 1987).&lt;/P&gt;</description>
      <pubDate>Thu, 22 Dec 2016 16:22:23 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/rolling-standard-deviation-set-the-starting-point/m-p/320760#M70691</guid>
      <dc:creator>Jennifer925</dc:creator>
      <dc:date>2016-12-22T16:22:23Z</dc:date>
    </item>
    <item>
      <title>Re: rolling standard deviation set the starting point</title>
      <link>https://communities.sas.com/t5/SAS-Programming/rolling-standard-deviation-set-the-starting-point/m-p/320776#M70699</link>
      <description>&lt;P&gt;proc sql;&lt;BR /&gt;select *,case when (select count(*) from have where year lt a.year) le &lt;FONT color="#FF0000"&gt;&lt;STRONG&gt;5&lt;/STRONG&gt; &lt;/FONT&gt;then .&lt;BR /&gt;else (select var(x) from have where year between a.year-2 and a.year) end as var&lt;BR /&gt;from have as a;&lt;BR /&gt;quit;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I just used this code get the result I wanted. But I am trying to use a function to describe the number &lt;STRONG&gt;&lt;FONT color="#FF0000"&gt;5&lt;/FONT&gt;&lt;/STRONG&gt;( count (X=0)before starting point plus 1),&amp;nbsp; I have a lot of different categories and their starting points are different. Could you help me out of these problem? Thanks.&lt;/P&gt;</description>
      <pubDate>Thu, 22 Dec 2016 17:32:37 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/rolling-standard-deviation-set-the-starting-point/m-p/320776#M70699</guid>
      <dc:creator>Jennifer925</dc:creator>
      <dc:date>2016-12-22T17:32:37Z</dc:date>
    </item>
    <item>
      <title>Re: rolling standard deviation set the starting point</title>
      <link>https://communities.sas.com/t5/SAS-Programming/rolling-standard-deviation-set-the-starting-point/m-p/320791#M70703</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/120563"&gt;@Jennifer925&lt;/a&gt; wrote:&lt;BR /&gt;
&lt;P&gt;The result is a little bit different. I am thinking that the first stdandard deviation is for 5,7,and 0(starting from 1984 to 1986), the second one should be for 7,0 and 6( from 1985 to 1987).&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;Not sure which post&amp;nbsp;you are responding to with this comment. You can use the QUOTE link at the top of the text box to include text from the one you are responding to.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The code I posted with Lag has the STD for 5,7,0 in the result for 1986. You could comment out or remove the DROP statement to see which values are used for calculating any given row. If the results aren't as you need perhaps adjust the lag interval. Maybe you mean lag1, lag2 and X instead of the lag3 I used.&lt;/P&gt;
&lt;P&gt;If you don't want a result for given years you could add something like &lt;BR /&gt;if year &amp;gt; (which ever boundary you want) then STD3yr = ... ;&lt;/P&gt;
&lt;P&gt;As I mentioned, I am still not sure which years you want results for.&lt;/P&gt;</description>
      <pubDate>Thu, 22 Dec 2016 18:22:26 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/rolling-standard-deviation-set-the-starting-point/m-p/320791#M70703</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2016-12-22T18:22:26Z</dc:date>
    </item>
    <item>
      <title>Re: rolling standard deviation set the starting point</title>
      <link>https://communities.sas.com/t5/SAS-Programming/rolling-standard-deviation-set-the-starting-point/m-p/320798#M70706</link>
      <description>&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/120563"&gt;@Jennifer925&lt;/a&gt; wrote:&lt;BR /&gt;
&lt;P&gt;proc sql;&lt;BR /&gt;select *,case when (select count(*) from have where year lt a.year) le &lt;FONT color="#FF0000"&gt;&lt;STRONG&gt;5&lt;/STRONG&gt; &lt;/FONT&gt;then .&lt;BR /&gt;else (select var(x) from have where year between a.year-2 and a.year) end as var&lt;BR /&gt;from have as a;&lt;BR /&gt;quit;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I just used this code get the result I wanted. But I am trying to use a function to describe the number &lt;STRONG&gt;&lt;FONT color="#FF0000"&gt;5&lt;/FONT&gt;&lt;/STRONG&gt;( count (X=0)before starting point plus 1),&amp;nbsp; I have a lot of different categories and their starting points are different. Could you help me out of these problem? Thanks.&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;Do you have a SAS ETS licence?&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;Run the following to see:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;proc setinit;&lt;/P&gt;
&lt;P&gt;run;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If you do, proc Expand is your best bet, see the third example in the documentation. &amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 22 Dec 2016 18:45:18 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/rolling-standard-deviation-set-the-starting-point/m-p/320798#M70706</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2016-12-22T18:45:18Z</dc:date>
    </item>
    <item>
      <title>Re: rolling standard deviation set the starting point</title>
      <link>https://communities.sas.com/t5/SAS-Programming/rolling-standard-deviation-set-the-starting-point/m-p/320802#M70708</link>
      <description>&lt;P&gt;You apparently want to include data only when the first non-zero value is encountered.&amp;nbsp; And then you want 3-yr&amp;nbsp; trailing STD, which means output doesn't start&amp;nbsp;until the&amp;nbsp; 2nd record following the first non-zero:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;FONT color="#000080" face="Courier New"&gt;&lt;STRONG&gt;data&lt;/STRONG&gt;&lt;/FONT&gt;&lt;FONT face="Courier New"&gt; want (&lt;/FONT&gt;&lt;FONT color="#0000ff" face="Courier New"&gt;drop&lt;/FONT&gt;&lt;FONT face="Courier New"&gt;=n_nonzeroes);&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face="Courier New"&gt;&amp;nbsp;&amp;nbsp;&lt;/FONT&gt;&lt;FONT color="#0000ff" face="Courier New"&gt;set&lt;/FONT&gt;&lt;FONT face="Courier New"&gt; have;&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face="Courier New"&gt;&amp;nbsp; n_nonzeroes+(x^=0); &lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;&lt;FONT color="#0000ff" face="Courier New"&gt;&amp;nbsp;if&lt;/FONT&gt;&lt;FONT face="Courier New"&gt; n_nonzeroes=&lt;/FONT&gt;&lt;STRONG&gt;&lt;FONT color="#008080" face="Courier New"&gt;0&lt;/FONT&gt;&lt;/STRONG&gt; &lt;FONT color="#0000ff" face="Courier New"&gt;then&lt;/FONT&gt; &lt;FONT color="#0000ff" face="Courier New"&gt;delete&lt;/FONT&gt;&lt;FONT face="Courier New"&gt;;&amp;nbsp;&amp;nbsp;&amp;nbsp; /* No input for std&amp;nbsp;until first&amp;nbsp;non-zero record*/&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face="Courier New"&gt;&amp;nbsp; std3yr=std(x,lag(x),lag2(x));&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT color="#0000ff" face="Courier New"&gt;&amp;nbsp; if&lt;/FONT&gt;&lt;FONT face="Courier New"&gt; lag2(n_nonzeroes)&amp;gt;0;&lt;/FONT&gt;&lt;FONT face="Courier New"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; /* No output until 2nd record following first non-zero*/&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT color="#000080" face="Courier New"&gt;&lt;STRONG&gt;run&lt;/STRONG&gt;&lt;/FONT&gt;&lt;FONT face="Courier New"&gt;;&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 22 Dec 2016 19:32:28 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/rolling-standard-deviation-set-the-starting-point/m-p/320802#M70708</guid>
      <dc:creator>mkeintz</dc:creator>
      <dc:date>2016-12-22T19:32:28Z</dc:date>
    </item>
    <item>
      <title>Re: rolling standard deviation set the starting point</title>
      <link>https://communities.sas.com/t5/SAS-Programming/rolling-standard-deviation-set-the-starting-point/m-p/320808#M70710</link>
      <description>I do not have the license of SAS ETS.</description>
      <pubDate>Thu, 22 Dec 2016 19:57:26 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/rolling-standard-deviation-set-the-starting-point/m-p/320808#M70710</guid>
      <dc:creator>Jennifer925</dc:creator>
      <dc:date>2016-12-22T19:57:26Z</dc:date>
    </item>
  </channel>
</rss>

