<?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: How to calculate std with group by , on a growing data in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/How-to-calculate-std-with-group-by-on-a-growing-data/m-p/944873#M370204</link>
    <description>&lt;P&gt;Thanks it works !!&lt;/P&gt;
&lt;P&gt;Great&lt;/P&gt;</description>
    <pubDate>Mon, 23 Sep 2024 08:38:56 GMT</pubDate>
    <dc:creator>J111</dc:creator>
    <dc:date>2024-09-23T08:38:56Z</dc:date>
    <item>
      <title>How to calculate std with group by , on a growing data</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-calculate-std-with-group-by-on-a-growing-data/m-p/944864#M370199</link>
      <description>&lt;P&gt;Hello,&lt;/P&gt;
&lt;P&gt;My understanding, a new topic should be opened for this by group question:-&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Next, if we wanted to add another field such as group by and&lt;/P&gt;
&lt;P&gt;calculate the std for each value of gr in the data below:-&lt;/P&gt;
&lt;P&gt;what would be the best solution ? Thanks&amp;nbsp; a lot.&lt;/P&gt;
&lt;P&gt;&lt;BR /&gt;data available ;&lt;BR /&gt;input gr dt y ;&lt;BR /&gt;cards ;&lt;BR /&gt;1 1 80&lt;BR /&gt;1 2 20&lt;BR /&gt;1 3 40&lt;BR /&gt;1 4 60&lt;BR /&gt;2 1 80&lt;BR /&gt;2 2 20&lt;BR /&gt;2 3 40&lt;BR /&gt;2 4 50&lt;BR /&gt;;&lt;BR /&gt;run ;&lt;/P&gt;
&lt;P&gt;data wanted ;&lt;BR /&gt;input gr dt y std ;&lt;BR /&gt;cards ;&lt;BR /&gt;1 1 80&lt;BR /&gt;1 2 20 42.426406871&lt;BR /&gt;1 3 40 30.550504633&lt;BR /&gt;1 4 60 25.819888975&lt;BR /&gt;2 1 80&lt;BR /&gt;2 2 20 42.426406871&lt;BR /&gt;2 3 40 30.550504633&lt;BR /&gt;2 4 50 25&lt;BR /&gt;;&lt;BR /&gt;run ;&lt;/P&gt;</description>
      <pubDate>Mon, 23 Sep 2024 06:20:19 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-calculate-std-with-group-by-on-a-growing-data/m-p/944864#M370199</guid>
      <dc:creator>J111</dc:creator>
      <dc:date>2024-09-23T06:20:19Z</dc:date>
    </item>
    <item>
      <title>Re: How to calculate std with group by , on a growing data</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-calculate-std-with-group-by-on-a-growing-data/m-p/944868#M370201</link>
      <description>&lt;P&gt;Did you try my code ?&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data available ;
input gr dt y ;
cards ;
1 1 80
1 2 20
1 3 40
1 4 60
2 1 80
2 2 20
2 3 40
2 4 50
;
data want;
 set available;
 by gr;
 array x{999999} _temporary_;
 if first.gr then do;n=0;call missing(of x{*});end;
 n+1;
 x{n}=y;
 std=std(of x{*});
 drop n;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Mon, 23 Sep 2024 07:59:55 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-calculate-std-with-group-by-on-a-growing-data/m-p/944868#M370201</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2024-09-23T07:59:55Z</dc:date>
    </item>
    <item>
      <title>Re: How to calculate std with group by , on a growing data</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-calculate-std-with-group-by-on-a-growing-data/m-p/944869#M370202</link>
      <description>&lt;P&gt;Hello&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/386728"&gt;@J111&lt;/a&gt;,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;My solution from the &lt;A href="https://communities.sas.com/t5/SAS-Programming/how-to-calculate-std-on-an-increasing-data-similar-to/m-p/944852/highlight/true#M370191" target="_blank" rel="noopener"&gt;previous thread&lt;/A&gt; can be generalized to BY groups as well:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want(drop=_:);
_s=0; _v=0;
do _n=1 by 1 until(last.gr);
  set available;
  by gr;
  _s+y; /* cumulative sum */
  _m=_s/_n; /* cumulative mean */
  _d=dif(_m); /* mean change */
  _q=(y-_m)**2; /* new term in sum of squares */
  if _n&amp;gt;1 then do;
    std=sqrt(_v+_d**2+_q/(_n-1)); /* cumulative standard deviation */
    _v=((_n-1)*(_v+_d**2)+_q)/_n; /* cumulative population variance */
  end;
  output;
end;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Mon, 23 Sep 2024 08:10:07 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-calculate-std-with-group-by-on-a-growing-data/m-p/944869#M370202</guid>
      <dc:creator>FreelanceReinh</dc:creator>
      <dc:date>2024-09-23T08:10:07Z</dc:date>
    </item>
    <item>
      <title>Re: How to calculate std with group by , on a growing data</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-calculate-std-with-group-by-on-a-growing-data/m-p/944872#M370203</link>
      <description>&lt;P&gt;Thanks a lot it works&amp;nbsp;&lt;/P&gt;
&lt;P&gt;super&lt;/P&gt;</description>
      <pubDate>Mon, 23 Sep 2024 08:36:41 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-calculate-std-with-group-by-on-a-growing-data/m-p/944872#M370203</guid>
      <dc:creator>J111</dc:creator>
      <dc:date>2024-09-23T08:36:41Z</dc:date>
    </item>
    <item>
      <title>Re: How to calculate std with group by , on a growing data</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-calculate-std-with-group-by-on-a-growing-data/m-p/944873#M370204</link>
      <description>&lt;P&gt;Thanks it works !!&lt;/P&gt;
&lt;P&gt;Great&lt;/P&gt;</description>
      <pubDate>Mon, 23 Sep 2024 08:38:56 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-calculate-std-with-group-by-on-a-growing-data/m-p/944873#M370204</guid>
      <dc:creator>J111</dc:creator>
      <dc:date>2024-09-23T08:38:56Z</dc:date>
    </item>
    <item>
      <title>Re: How to calculate std with group by , on a growing data</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-calculate-std-with-group-by-on-a-growing-data/m-p/944874#M370205</link>
      <description>&lt;P&gt;It would be nice to see the solution using the logic below (from P.Miller),&lt;/P&gt;
&lt;P&gt;but just for the by group..&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE class="language-sas"&gt;&lt;CODE&gt;data _NULL_;
	if 0 then set available nobs=n;
	call symputx('nrows',n);
	stop;
run;


data intermediate;
    set available;
    do group=_n_ to &amp;amp;nrows;
        thisy=y;
        output;
    end;
run;
        
proc summary data=intermediate nway;
    class group;
    var thisy;
    output out=want stddev=;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;using&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 23 Sep 2024 08:46:44 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-calculate-std-with-group-by-on-a-growing-data/m-p/944874#M370205</guid>
      <dc:creator>J111</dc:creator>
      <dc:date>2024-09-23T08:46:44Z</dc:date>
    </item>
    <item>
      <title>Re: How to calculate std with group by , on a growing data</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-calculate-std-with-group-by-on-a-growing-data/m-p/944881#M370208</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data available ;
input gr dt y ;
cards ;
1 1 80
1 2 20
1 3 40
1 4 60
2 1 80
2 2 20
2 3 40
2 4 50
;

proc freq data=available;
    tables gr/noprint out=_a_;
run;

data intermediate;
    merge available _a_;
    by gr;
    if first.gr then seq=0;
    seq+1;
    do group=seq to count;
        thisy=y;
        output;
    end;
run;

proc summary data=intermediate nway;
    class gr group;
    var thisy;
    output out=want stddev=;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Mon, 23 Sep 2024 11:06:39 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-calculate-std-with-group-by-on-a-growing-data/m-p/944881#M370208</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2024-09-23T11:06:39Z</dc:date>
    </item>
    <item>
      <title>Re: How to calculate std with group by , on a growing data</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-calculate-std-with-group-by-on-a-growing-data/m-p/944882#M370209</link>
      <description>&lt;P&gt;Thanks a lot -&lt;/P&gt;
&lt;P&gt;May I point out that this solution has much better performance compared to the array post&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 23 Sep 2024 11:37:27 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-calculate-std-with-group-by-on-a-growing-data/m-p/944882#M370209</guid>
      <dc:creator>J111</dc:creator>
      <dc:date>2024-09-23T11:37:27Z</dc:date>
    </item>
  </channel>
</rss>

