<?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: Standard deviation of firm i's sales from year t-4 to t in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Standard-deviation-of-firm-i-s-sales-from-year-t-4-to-t/m-p/901776#M356370</link>
    <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
array p{0:4} _temporary_;
set have; 
by gvkey year;

if first.gvkey then do;
call missing(of p{*});
num_years=0;
end;
num_years+1;

p{mod(_n_,5)} = sale;

*if you only want to calculate this once you have 5 years of data;
if num_years&amp;gt;=5 then sale_std = std(of p(*));

*if not conditional remove the IF portion;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I would probably recommend PROC EXPAND as it will deal with missing data and years better, however, here's a rough data step approach. You will need to tailor to deal with missing years/data if necessary.&lt;/P&gt;</description>
    <pubDate>Mon, 06 Nov 2023 19:05:28 GMT</pubDate>
    <dc:creator>Reeza</dc:creator>
    <dc:date>2023-11-06T19:05:28Z</dc:date>
    <item>
      <title>Standard deviation of firm i's sales from year t-4 to t</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Standard-deviation-of-firm-i-s-sales-from-year-t-4-to-t/m-p/901559#M356281</link>
      <description>&lt;P&gt;Hello everyone,&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I have dataset which has gvkey, year, and sale. I need to calculate standard deviation pf sale for each firm (identified by gvkey) between year of t to t-4.&lt;/P&gt;&lt;P&gt;For example&amp;nbsp; standard deviation of sale for 1996 from 1996 to 1995, 1994, 1993, 1992.&amp;nbsp;&lt;/P&gt;&lt;P&gt;I appreciate your help.&amp;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D&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;1019&lt;/TD&gt;&lt;TD&gt;1994&lt;/TD&gt;&lt;TD&gt;6570&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;1019&lt;/TD&gt;&lt;TD&gt;1995&lt;/TD&gt;&lt;TD&gt;4298&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;1019&lt;/TD&gt;&lt;TD&gt;1996&lt;/TD&gt;&lt;TD&gt;4657&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;1019&lt;/TD&gt;&lt;TD&gt;1997&lt;/TD&gt;&lt;TD&gt;5498&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;1019&lt;/TD&gt;&lt;TD&gt;1998&lt;/TD&gt;&lt;TD&gt;6790&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;….&lt;/TD&gt;&lt;TD&gt;…&lt;/TD&gt;&lt;TD&gt;…&lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;</description>
      <pubDate>Sat, 04 Nov 2023 15:12:20 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Standard-deviation-of-firm-i-s-sales-from-year-t-4-to-t/m-p/901559#M356281</guid>
      <dc:creator>Fer_</dc:creator>
      <dc:date>2023-11-04T15:12:20Z</dc:date>
    </item>
    <item>
      <title>Re: Standard deviation of firm i's sales from year t-4 to t</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Standard-deviation-of-firm-i-s-sales-from-year-t-4-to-t/m-p/901731#M356355</link>
      <description>&lt;P&gt;Hello,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;You need a moving standard deviation with size of the window equal to 5 (t-4, t-3, t-2, t-1, t).&lt;/P&gt;
&lt;P&gt;You can easily do this with PROC EXPAND (SAS/ETS).&lt;BR /&gt;Here are some transformations that will be of interest to you :&lt;/P&gt;
&lt;UL class="lia-list-style-type-square"&gt;
&lt;LI&gt;CMOVSTD window Centered moving standard deviation&lt;/LI&gt;
&lt;LI&gt;CUSTD (n is optional) Cumulative standard deviation&lt;/LI&gt;
&lt;LI&gt;&lt;STRONG&gt;MOVSTD&lt;/STRONG&gt; window Backward moving weighted standard deviation:&lt;/LI&gt;
&lt;/UL&gt;
&lt;P&gt;Try for example this:&lt;BR /&gt;&lt;STRONG&gt;Take care!&lt;/STRONG&gt; : I am not sure if this will calculate std on&amp;nbsp;(t-4, t-3, t-2, t-1, t) or on (t-5, t-4, t-3, t-2, t-1).&lt;BR /&gt;Please check ... if it's not including the current line t (as you want) , I will tell you how to "shift" the window.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc expand data=sashelp.pricedata out=abc;
 by region line product;
 convert sale = sale_MOVSTD / transformout=(MOVSTD 5);
run;

proc datasets library=work NoList Nodetails memtype=DATA;
 modify abc;
  label sale_MOVSTD = "sale moving std window size = 5";
run;
QUIT;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;BR, Koen&lt;/P&gt;</description>
      <pubDate>Mon, 06 Nov 2023 14:21:22 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Standard-deviation-of-firm-i-s-sales-from-year-t-4-to-t/m-p/901731#M356355</guid>
      <dc:creator>sbxkoenk</dc:creator>
      <dc:date>2023-11-06T14:21:22Z</dc:date>
    </item>
    <item>
      <title>Re: Standard deviation of firm i's sales from year t-4 to t</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Standard-deviation-of-firm-i-s-sales-from-year-t-4-to-t/m-p/901766#M356366</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/60547"&gt;@sbxkoenk&lt;/a&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;You can actually assign the label during the PROC EXPAND, thereby avoiding the need for a subsequent PROC DATASETS.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The price?&amp;nbsp; An unavoidable warning note, as in:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;31   proc expand data=sashelp.pricedata out=abc;
32    by region line product;
33    convert sale = sale_MOVSTD / transformout=(MOVSTD 5);
34    label sale_MOVSTD = "sale moving std window size = 5";
WARNING: Variable SALE_MOVSTD not found in data set SASHELP.PRICEDATA.
35   run;
&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Mon, 06 Nov 2023 17:56:04 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Standard-deviation-of-firm-i-s-sales-from-year-t-4-to-t/m-p/901766#M356366</guid>
      <dc:creator>mkeintz</dc:creator>
      <dc:date>2023-11-06T17:56:04Z</dc:date>
    </item>
    <item>
      <title>Re: Standard deviation of firm i's sales from year t-4 to t</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Standard-deviation-of-firm-i-s-sales-from-year-t-4-to-t/m-p/901776#M356370</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
array p{0:4} _temporary_;
set have; 
by gvkey year;

if first.gvkey then do;
call missing(of p{*});
num_years=0;
end;
num_years+1;

p{mod(_n_,5)} = sale;

*if you only want to calculate this once you have 5 years of data;
if num_years&amp;gt;=5 then sale_std = std(of p(*));

*if not conditional remove the IF portion;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I would probably recommend PROC EXPAND as it will deal with missing data and years better, however, here's a rough data step approach. You will need to tailor to deal with missing years/data if necessary.&lt;/P&gt;</description>
      <pubDate>Mon, 06 Nov 2023 19:05:28 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Standard-deviation-of-firm-i-s-sales-from-year-t-4-to-t/m-p/901776#M356370</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2023-11-06T19:05:28Z</dc:date>
    </item>
  </channel>
</rss>

