<?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: moving standard deviation except t period. in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/moving-standard-deviation-except-t-period/m-p/698498#M213636</link>
    <description>Thank you for your help.&lt;BR /&gt;but the output value by the above code does not provide correct standard deviations as well.&lt;BR /&gt;I could not understand "x[mod(obs, 4)]=sale;" part, and whether this code is forward-looking or backward is not sure.&lt;BR /&gt;&lt;BR /&gt;I think I should use other than PROC EXPAND.&lt;BR /&gt;&lt;BR /&gt;Thanks for your helps again &lt;span class="lia-unicode-emoji" title=":slightly_smiling_face:"&gt;🙂&lt;/span&gt;</description>
    <pubDate>Thu, 12 Nov 2020 19:53:37 GMT</pubDate>
    <dc:creator>JKCho</dc:creator>
    <dc:date>2020-11-12T19:53:37Z</dc:date>
    <item>
      <title>moving standard deviation except t period.</title>
      <link>https://communities.sas.com/t5/SAS-Programming/moving-standard-deviation-except-t-period/m-p/698327#M213556</link>
      <description>&lt;P&gt;Hello!&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Simpl question.&amp;nbsp;&lt;/P&gt;&lt;P&gt;I want to obtain standard deviations of previous five-year sales. In other words, I need to calculate the std from &lt;STRONG&gt;t-1 to t-5.&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;As long as I know, the code below means the period between &lt;STRONG&gt;t and t-4&lt;/STRONG&gt;. I have tried to find an exact code for this command but could not find but assume I may use other than Proc expand.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Could anyone help me with how to write the code for t-1 to t-n period?&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thank you so much!&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;PROC EXPAND DATA=comp1 OUT=sale;
id fyear;
CONVERT sale=stdsale / TRANSFORMOUT=(MOVSTD 5);
by gvkey;
RUN;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 12 Nov 2020 07:44:08 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/moving-standard-deviation-except-t-period/m-p/698327#M213556</guid>
      <dc:creator>JKCho</dc:creator>
      <dc:date>2020-11-12T07:44:08Z</dc:date>
    </item>
    <item>
      <title>Re: moving standard deviation except t period.</title>
      <link>https://communities.sas.com/t5/SAS-Programming/moving-standard-deviation-except-t-period/m-p/698329#M213557</link>
      <description>&lt;P&gt;How about&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data comp1;
input gvkey fyear sale;
datalines;
1 2010 100
1 2011 100
1 2012 100
1 2013 100
1 2014 200
2 2010 100
2 2011 100
2 2012 100
2 2013 100
2 2014 200
;

PROC EXPAND DATA=comp1 OUT=sale;
id fyear;
CONVERT sale=stdsale / TRANSFORMOUT=(MOVSTD (1 1 1 1 0));
by gvkey;
RUN;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Thu, 12 Nov 2020 08:15:11 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/moving-standard-deviation-except-t-period/m-p/698329#M213557</guid>
      <dc:creator>PeterClemmensen</dc:creator>
      <dc:date>2020-11-12T08:15:11Z</dc:date>
    </item>
    <item>
      <title>Re: moving standard deviation except t period.</title>
      <link>https://communities.sas.com/t5/SAS-Programming/moving-standard-deviation-except-t-period/m-p/698334#M213559</link>
      <description>&lt;P&gt;Thank you for your reply.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;It seems that using&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="sas"&gt;TRANSFORMOUT=(MOVSTD (1 1 1 1 0))&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;means x multiplied by(X) 0, x-1 X 1, and so on to x-4 X 1...&amp;nbsp; which actually results in the period from t-1 to t-4 while the denominator is still 5. I just tried to test with the code below.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data work;
input gvkey fyear sale;
datalines;
1 2010 100
1 2011 100
1 2012 100
1 2013 200
1 2014 200
2 2010 100
2 2011 100
2 2012 100
2 2013 200
2 2014 200
;&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;What I obtained from it are just weird values.&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="11.PNG" style="width: 470px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/51598i679AFA8128D5F16A/image-size/large?v=v2&amp;amp;px=999" role="button" title="11.PNG" alt="11.PNG" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;The standard deviation of 21.65 looks out of nowhere. I can guess the variance there is sth like 468.7 so if I multiply 4 with this variance, then I get close to 1875, which is the nominator of the definition of STD, though I have no clue where this number comes from.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;my calculations are... (As the most recent observations are not used)&lt;/P&gt;&lt;P&gt;mean= (100+100+100+200)/4 = 175&lt;/P&gt;&lt;P&gt;variance = {(100-175)^2 +(100-175)^2+(100-175)^2+(200-175)^2} / 4= 4375 (not like 1875)&lt;/P&gt;&lt;P&gt;Std = 66.1438&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;This looks not quite fit with what I seek now standard deviations from t-5 to t-1, which conceptually needs observations at the t-1 period as well.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Any help?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 12 Nov 2020 09:02:34 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/moving-standard-deviation-except-t-period/m-p/698334#M213559</guid>
      <dc:creator>JKCho</dc:creator>
      <dc:date>2020-11-12T09:02:34Z</dc:date>
    </item>
    <item>
      <title>Re: moving standard deviation except t period.</title>
      <link>https://communities.sas.com/t5/SAS-Programming/moving-standard-deviation-except-t-period/m-p/698335#M213560</link>
      <description>&lt;P&gt;I think you're right. Is it a requirement to use Proc Expand?&lt;/P&gt;</description>
      <pubDate>Thu, 12 Nov 2020 09:10:34 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/moving-standard-deviation-except-t-period/m-p/698335#M213560</guid>
      <dc:creator>PeterClemmensen</dc:creator>
      <dc:date>2020-11-12T09:10:34Z</dc:date>
    </item>
    <item>
      <title>Re: moving standard deviation except t period.</title>
      <link>https://communities.sas.com/t5/SAS-Programming/moving-standard-deviation-except-t-period/m-p/698337#M213562</link>
      <description>Not at all, I am just working on replicating a journal paper to use their codes in my research as well. A variable in this paper is defined as standards deviations of the previous five years' sale. I thought PROC EXPAND can handle but facing this difficulty...</description>
      <pubDate>Thu, 12 Nov 2020 09:17:52 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/moving-standard-deviation-except-t-period/m-p/698337#M213562</guid>
      <dc:creator>JKCho</dc:creator>
      <dc:date>2020-11-12T09:17:52Z</dc:date>
    </item>
    <item>
      <title>Re: moving standard deviation except t period.</title>
      <link>https://communities.sas.com/t5/SAS-Programming/moving-standard-deviation-except-t-period/m-p/698340#M213565</link>
      <description>&lt;P&gt;Ok. I'm sure Proc Expand can handle this somehow, but I'm no expert.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Here is a data step approach that gives you what you want:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
input gvkey fyear sale;
datalines;
1 2010 100
1 2011 200
1 2012 300
1 2013 400
1 2014 500
2 2010 100
2 2011 200
2 2012 300
2 2013 400
2 2014 500
;

data want(drop=obs);
    array x[0:3] _temporary_;
    call missing(of x[*]);

    do obs=1 by 1 until (last.gvkey);    
        std = std(of x[*]);
        set have;
        by gvkey;
        x[mod(obs, 4)]=sale;
        output; 
    end;
   
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Thu, 12 Nov 2020 09:43:44 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/moving-standard-deviation-except-t-period/m-p/698340#M213565</guid>
      <dc:creator>PeterClemmensen</dc:creator>
      <dc:date>2020-11-12T09:43:44Z</dc:date>
    </item>
    <item>
      <title>Re: moving standard deviation except t period.</title>
      <link>https://communities.sas.com/t5/SAS-Programming/moving-standard-deviation-except-t-period/m-p/698498#M213636</link>
      <description>Thank you for your help.&lt;BR /&gt;but the output value by the above code does not provide correct standard deviations as well.&lt;BR /&gt;I could not understand "x[mod(obs, 4)]=sale;" part, and whether this code is forward-looking or backward is not sure.&lt;BR /&gt;&lt;BR /&gt;I think I should use other than PROC EXPAND.&lt;BR /&gt;&lt;BR /&gt;Thanks for your helps again &lt;span class="lia-unicode-emoji" title=":slightly_smiling_face:"&gt;🙂&lt;/span&gt;</description>
      <pubDate>Thu, 12 Nov 2020 19:53:37 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/moving-standard-deviation-except-t-period/m-p/698498#M213636</guid>
      <dc:creator>JKCho</dc:creator>
      <dc:date>2020-11-12T19:53:37Z</dc:date>
    </item>
    <item>
      <title>Re: moving standard deviation except t period.</title>
      <link>https://communities.sas.com/t5/SAS-Programming/moving-standard-deviation-except-t-period/m-p/698586#M213667</link>
      <description>&lt;P&gt;Sorry, the Std Function calculates the sample std and it seems you want the population std.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Just edited the program a little. See if this gives you the desired result. If so, I'll explain the logic &lt;span class="lia-unicode-emoji" title=":slightly_smiling_face:"&gt;🙂&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
input gvkey fyear sale;
datalines;
1 2010 100
1 2011 200
1 2012 300
1 2013 400
1 2014 500
2 2010 100
2 2011 200
2 2012 300
2 2013 400
2 2014 500
;

data want(drop=obs i);
    array x[0:3] _temporary_;
    array y[0:3] _temporary_;
    call stdize('replace', 'mult=', 0, of x[*], _N_);

    do obs=1 by 1 until (last.gvkey);    
        do i = 0 to dim(x)-1;
           y [i] = (x [i] - mean(of x[*])) ** 2;
        end;
        std = sqrt(divide(sum(of y[*]), 4));
        set have;
        by gvkey;
        x[mod(obs, 4)] = sale;
        output; 
    end;

run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Result:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;std      gvkey  fyear  sale 
0.000    1      2010   100 
43.301   1      2011   200 
82.916   1      2012   300 
111.803  1      2013   400 
111.803  1      2014   500 
0.000    2      2010   100 
43.301   2      2011   200 
82.916   2      2012   300 
111.803  2      2013   400 
111.803  2      2014   500 
&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 13 Nov 2020 08:16:44 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/moving-standard-deviation-except-t-period/m-p/698586#M213667</guid>
      <dc:creator>PeterClemmensen</dc:creator>
      <dc:date>2020-11-13T08:16:44Z</dc:date>
    </item>
    <item>
      <title>Re: moving standard deviation except t period.</title>
      <link>https://communities.sas.com/t5/SAS-Programming/moving-standard-deviation-except-t-period/m-p/698589#M213670</link>
      <description>&lt;P&gt;Btw, here mean = (100 + 100 + 100 + 200) / 4 = 125 &lt;span class="lia-unicode-emoji" title=":winking_face:"&gt;😉&lt;/span&gt;&lt;/P&gt;</description>
      <pubDate>Fri, 13 Nov 2020 08:21:03 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/moving-standard-deviation-except-t-period/m-p/698589#M213670</guid>
      <dc:creator>PeterClemmensen</dc:creator>
      <dc:date>2020-11-13T08:21:03Z</dc:date>
    </item>
    <item>
      <title>Re: moving standard deviation except t period.</title>
      <link>https://communities.sas.com/t5/SAS-Programming/moving-standard-deviation-except-t-period/m-p/698612#M213674</link>
      <description>lol... I made a huge but simple mistake! thank you for letting me know that &lt;span class="lia-unicode-emoji" title=":slightly_smiling_face:"&gt;🙂&lt;/span&gt;</description>
      <pubDate>Fri, 13 Nov 2020 09:47:09 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/moving-standard-deviation-except-t-period/m-p/698612#M213674</guid>
      <dc:creator>JKCho</dc:creator>
      <dc:date>2020-11-13T09:47:09Z</dc:date>
    </item>
    <item>
      <title>Re: moving standard deviation except t period.</title>
      <link>https://communities.sas.com/t5/SAS-Programming/moving-standard-deviation-except-t-period/m-p/698613#M213675</link>
      <description>Thank you for your ongoing efforts! I actually came up with easier programming but your code certainly works as well! Thank you so much!&lt;BR /&gt;&lt;BR /&gt;Have a good day!</description>
      <pubDate>Fri, 13 Nov 2020 09:48:51 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/moving-standard-deviation-except-t-period/m-p/698613#M213675</guid>
      <dc:creator>JKCho</dc:creator>
      <dc:date>2020-11-13T09:48:51Z</dc:date>
    </item>
  </channel>
</rss>

