<?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 get weighted stdev in SQL/SAS?! in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/How-to-get-weighted-stdev-in-SQL-SAS/m-p/975819#M378184</link>
    <description>&lt;P&gt;Weird - I corrected what I thought the issue was, and I still get very different results from PROC SUMMARY.&amp;nbsp; If anyone is math-inclined and bored, I'd be interested to know what's wrong:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data temp2;
set _temp;
array xs {4} x1-x4;
array ws {4} wt_x1-wt_x4;
sumXW=.;  * this will hold the sum of the products of Xi and Wi ;
sumW=sum(of ws[*]);  * sum of the weights ;
n_nzw=0;  * this will hold the number of non-zero weights ;
do j=1 to dim(xs);
	sumXW+(xs[j]*ws[j]);
	n_nzw+(ws[j]&amp;gt;0);
end;
rawmean_x=mean(of xs[*]);	* raw mean of Xs ;
wtmean_x=sumXW/sumW;		* wt mean of Xs ;
sumD2w=.;  ** this will hold the sum of the weighted, squared differences (deviations) ;
sumWcorr=.; * this will hold the sum of the weights after correcting for the number of non-zero weights ;
do j=1 to dim(xs);
	sumD2w+((xs[j]-wtmean_x)**2*ws[j]);
	sumWcorr+(ws[j]*((n_nzw-1)/n_nzw));
end;
rawSD=std(of xs[*]);		* raw SD of Xs ;
wtSD=sqrt(sumD2w/sumWcorr);	* wt SD of Xs ;
drop j;
run;

proc print data=temp2; run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Result:&lt;/P&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="quickbluefish_0-1758912919153.png" style="width: 400px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/110164iC833D257871590B1/image-size/medium?v=v2&amp;amp;px=400" role="button" title="quickbluefish_0-1758912919153.png" alt="quickbluefish_0-1758912919153.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Fri, 26 Sep 2025 18:55:27 GMT</pubDate>
    <dc:creator>quickbluefish</dc:creator>
    <dc:date>2025-09-26T18:55:27Z</dc:date>
    <item>
      <title>How to get weighted stdev in SQL/SAS?!</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-get-weighted-stdev-in-SQL-SAS/m-p/975777#M378170</link>
      <description>&lt;P&gt;Here is a time-series sample dataset, x1-x4[time=i=1,2,3,...].&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;How to get the weighted standard deviation?! weight for x1-x4 are wt_x1-wt_x4.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data _temp;
do i=1 to 1000;
	x1=sin(i/10);
	x2=cos(i/20);
	x3=sin(i/100)-cos(i/30);
	x4=sin(i/50)+cos(i/200);
	wt_x1=10; 
	wt_x2=25;
	wt_x3=30;
	wt_x4=35;
	output;
end;
run;quit;

/*how to get weighted stdev in SQL/SAS*/
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Thanks,&amp;nbsp;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 26 Sep 2025 10:50:36 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-get-weighted-stdev-in-SQL-SAS/m-p/975777#M378170</guid>
      <dc:creator>hellohere</dc:creator>
      <dc:date>2025-09-26T10:50:36Z</dc:date>
    </item>
    <item>
      <title>Re: How to get weighted stdev in SQL/SAS?!</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-get-weighted-stdev-in-SQL-SAS/m-p/975780#M378172</link>
      <description>&lt;P&gt;This type of data is much better arranged as a long data set, rather than a wide data set. Why? Because SAS PROCs that can compute weighted statistics are designed to work from a long data set; and this gives you the benefit that SAS has tested these statistical calculations, and also the benefit that you don't have to program it yourself. So the first step is to create a long data set. The second step is to run PROC SUMMARY to get the weighted standard deviation.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc transpose data=_temp out=aaa;
    var x:;
    by i;
run;
proc transpose data=_temp out=bbb;
    var wt:;
    by i;
run;
data long;
    merge aaa(rename=(col1=x)) bbb(rename=(col1=weights));
    by i;
run;
proc summary data=long nway;
    class i;
    var x;
    weight weights;
    output out=want stddev=s;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;BR /&gt;Making the data into a long data set is even easier if you use the %MAKELONG macro at&amp;nbsp;&lt;A href="https://communities.sas.com/t5/SAS-Communities-Library/Transpose-your-analysis-data-with-the-MAKELONG-and-MAKEWIDE/ta-p/791732" target="_blank" rel="noopener"&gt;https://communities.sas.com/t5/SAS-Communities-Library/Transpose-your-analysis-data-with-the-MAKELONG-and-MAKEWIDE/ta-p/791732&lt;/A&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;or this method&amp;nbsp;&lt;A href="https://communities.sas.com/t5/SAS-Global-Forum-Proceedings/Switching-Things-Up-With-the-TRANSPOSE-Procedure/ta-p/741768" target="_blank" rel="noopener"&gt;https://communities.sas.com/t5/SAS-Global-Forum-Proceedings/Switching-Things-Up-With-the-TRANSPOSE-Procedure/ta-p/741768&lt;/A&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;or this method&amp;nbsp;&lt;A href="http://support.sas.com/resources/papers/proceedings15/2785-2015.pdf" target="_blank" rel="noopener"&gt;http://support.sas.com/resources/papers/proceedings15/2785-2015.pdf&lt;/A&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 26 Sep 2025 12:00:15 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-get-weighted-stdev-in-SQL-SAS/m-p/975780#M378172</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2025-09-26T12:00:15Z</dc:date>
    </item>
    <item>
      <title>Re: How to get weighted stdev in SQL/SAS?!</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-get-weighted-stdev-in-SQL-SAS/m-p/975785#M378173</link>
      <description>Anyway to do with SAS/SQL?

Actually this is toy dataset. I am trying calculation with charging weights[weight for each each var is
changing, calc'd from columns not shown].</description>
      <pubDate>Fri, 26 Sep 2025 11:40:09 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-get-weighted-stdev-in-SQL-SAS/m-p/975785#M378173</guid>
      <dc:creator>hellohere</dc:creator>
      <dc:date>2025-09-26T11:40:09Z</dc:date>
    </item>
    <item>
      <title>Re: How to get weighted stdev in SQL/SAS?!</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-get-weighted-stdev-in-SQL-SAS/m-p/975786#M378174</link>
      <description>&lt;P&gt;Yes, I'm sure it can be done in SQL, but I personally refuse to accept the limitation that it must be in SQL, which then prevents me from using the power of SAS and prevents me from using all the features of SAS. In addition to the reasons I gave for doing this on a long data set and using PROC SUMMARY, if you have code that works for 4 values of x, and then at some point in the future you have more than 4 values of x (like 5 or 6 or 12 or 107 values of x), you have to modify the SQL code, whereas in my code no modification is necessary, even if you have 107 values of x. If you have a lot more than 4 values of x, this modification of SQL would become tedious and error-prone. So, I will stick with my answer. I'm sure someone will come along and provide SQL code to do this. For anyone else reading along, don't do this in SQL, don't make it harder than it needs to be.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The code I gave works if the weights change for every value of i.&lt;/P&gt;</description>
      <pubDate>Fri, 26 Sep 2025 12:45:38 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-get-weighted-stdev-in-SQL-SAS/m-p/975786#M378174</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2025-09-26T12:45:38Z</dc:date>
    </item>
    <item>
      <title>Re: How to get weighted stdev in SQL/SAS?!</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-get-weighted-stdev-in-SQL-SAS/m-p/975810#M378180</link>
      <description>&lt;P&gt;I really like SQL for doing simple weighted averages because the code can be written just like the equation, that is, &lt;EM&gt;the sum of the products over the sum of the weights&lt;/EM&gt;.&amp;nbsp; However, partly for the reasons that&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/10892"&gt;@PaigeMiller&lt;/a&gt;&amp;nbsp;pointed out re: wide data and partly because the formula for weighted SD is more complex than for a weighted mean, SQL is going to be very awkward.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Just for fun, here's a way you can do it in the DATA step (will readily admit I had to remind myself of the formula):&lt;/P&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="quickbluefish_0-1758908348820.png" style="width: 400px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/110160i8C7FD8C2B331555F/image-size/medium?v=v2&amp;amp;px=400" role="button" title="quickbluefish_0-1758908348820.png" alt="quickbluefish_0-1758908348820.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;In the DATA step, you can carry out the above like this:&lt;/P&gt;
&lt;P&gt;** create your data (note only creating 20 rows here)...&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data _temp;
do i=1 to 20;
	x1=sin(i/10);
	x2=cos(i/20);
	x3=sin(i/100)-cos(i/30);
	x4=sin(i/50)+cos(i/200);
	wt_x1=10; 
	wt_x2=25;
	wt_x3=30;
	wt_x4=35;
	output;
end;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;...then do the rest in a separate data step:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data temp2;
set _temp;
array xs {4} x1-x4;
array ws {4} wt_x1-wt_x4;
sumXW=.; sumW=.;
do j=1 to dim(xs);
	sumXW+(xs[j]*ws[j]);
	sumW+ws[j];
end;
rawmean_x=mean(of xs[*]);	* raw mean of Xs ;
wtmean_x=sumXW/sumW;		* wt mean of Xs ;
sumD2w=.;
n_nzw=0;
do j=1 to dim(xs);
	sumD2w+((xs[j]-wtmean_x)**2*ws[j]);
	n_nzw+(ws[j]&amp;gt;0);
end;
rawSD=std(of xs[*]);		* raw SD of Xs ;

wtSD=					/* WT MEAN OF Xs = ... */
	sqrt(
		sumD2w/				/* sum of the weighted deviations */
						/* divided by... */
		(sumW*(				/* the sum of the weights... */
						/* corrected for the number of non-zero weights... */
			(n_nzw-1)		
			/n_nzw
			)
		)
	);
drop j;
run;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Result -- note raw and weighted values.&amp;nbsp; You can verify this works by setting all the weights in the input data set (first step) to be the same value -- in this case, the raw values would equal the weighted values.&lt;/P&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="quickbluefish_1-1758908711021.png" style="width: 400px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/110161i5C00FF43CB6CE46E/image-size/medium?v=v2&amp;amp;px=400" role="button" title="quickbluefish_1-1758908711021.png" alt="quickbluefish_1-1758908711021.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 26 Sep 2025 17:45:29 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-get-weighted-stdev-in-SQL-SAS/m-p/975810#M378180</guid>
      <dc:creator>quickbluefish</dc:creator>
      <dc:date>2025-09-26T17:45:29Z</dc:date>
    </item>
    <item>
      <title>Re: How to get weighted stdev in SQL/SAS?!</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-get-weighted-stdev-in-SQL-SAS/m-p/975811#M378181</link>
      <description>&lt;P&gt;If you have missing values for x, I think you get the wrong answer here when missing values are present, whereas using PROC SUMMARY you get the right answers. PROC SUMMARY gives different weighted means and weighted standard deviations for observation 19 than your program does. So in addition to the difficulty of writing your own formulas, you don't get the benefit of the work SAS has already done (and your company or university has paid for) and you potentially get wrong answers writing your own formulas.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;BTW — I think your weighted standard deviations are wrong even when there are no missings, they do not match PROC SUMMARY.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Which is why I strongly advise people against writing their own formulas when SAS has already done that for you and tested it thoroughly.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data _temp;
do i=1 to 20;
	x1=sin(i/10);
	x2=cos(i/20);
	x3=sin(i/100)-cos(i/30);
	x4=sin(i/50)+cos(i/200);
	wt_x1=10; 
	wt_x2=25;
	wt_x3=30;
	wt_x4=35;
    if i=19 then x4=.;
	output;
end;
run;
proc transpose data=_temp out=aaa;
    var x:;
    by i;
run;
proc transpose data=_temp out=bbb;
    var wt:;
    by i;
run;
data long;
    merge aaa(rename=(col1=x)) bbb(rename=(col1=weights));
    by i;
run;
proc summary data=long nway;
    class i;
    var x;
    weight weights;
    output out=want_weighted stddev=s mean=m;
run;
proc summary data=long nway;
    class i;
    var x;
    output out=want_unweighted mean= stddev=/autoname;
run;
&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;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 26 Sep 2025 18:15:22 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-get-weighted-stdev-in-SQL-SAS/m-p/975811#M378181</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2025-09-26T18:15:22Z</dc:date>
    </item>
    <item>
      <title>Re: How to get weighted stdev in SQL/SAS?!</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-get-weighted-stdev-in-SQL-SAS/m-p/975814#M378182</link>
      <description>&lt;P&gt;Yeah, for some reason, PROC SUMMARY is giving some very bizarre answers for weighted SD here.&amp;nbsp; They don't seem to make sense:&lt;/P&gt;
&lt;P&gt;Raw data:&lt;/P&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="quickbluefish_0-1758911047450.png" style="width: 400px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/110162i2E68989CA02C73C5/image-size/medium?v=v2&amp;amp;px=400" role="button" title="quickbluefish_0-1758911047450.png" alt="quickbluefish_0-1758911047450.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;output from PROC SUMMARY:&lt;/P&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="quickbluefish_1-1758911088440.png" style="width: 400px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/110163iCD0482F221BE17BD/image-size/medium?v=v2&amp;amp;px=400" role="button" title="quickbluefish_1-1758911088440.png" alt="quickbluefish_1-1758911088440.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 26 Sep 2025 18:26:06 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-get-weighted-stdev-in-SQL-SAS/m-p/975814#M378182</guid>
      <dc:creator>quickbluefish</dc:creator>
      <dc:date>2025-09-26T18:26:06Z</dc:date>
    </item>
    <item>
      <title>Re: How to get weighted stdev in SQL/SAS?!</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-get-weighted-stdev-in-SQL-SAS/m-p/975816#M378183</link>
      <description>ah, now I see what I did wrong!</description>
      <pubDate>Fri, 26 Sep 2025 18:41:06 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-get-weighted-stdev-in-SQL-SAS/m-p/975816#M378183</guid>
      <dc:creator>quickbluefish</dc:creator>
      <dc:date>2025-09-26T18:41:06Z</dc:date>
    </item>
    <item>
      <title>Re: How to get weighted stdev in SQL/SAS?!</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-get-weighted-stdev-in-SQL-SAS/m-p/975819#M378184</link>
      <description>&lt;P&gt;Weird - I corrected what I thought the issue was, and I still get very different results from PROC SUMMARY.&amp;nbsp; If anyone is math-inclined and bored, I'd be interested to know what's wrong:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data temp2;
set _temp;
array xs {4} x1-x4;
array ws {4} wt_x1-wt_x4;
sumXW=.;  * this will hold the sum of the products of Xi and Wi ;
sumW=sum(of ws[*]);  * sum of the weights ;
n_nzw=0;  * this will hold the number of non-zero weights ;
do j=1 to dim(xs);
	sumXW+(xs[j]*ws[j]);
	n_nzw+(ws[j]&amp;gt;0);
end;
rawmean_x=mean(of xs[*]);	* raw mean of Xs ;
wtmean_x=sumXW/sumW;		* wt mean of Xs ;
sumD2w=.;  ** this will hold the sum of the weighted, squared differences (deviations) ;
sumWcorr=.; * this will hold the sum of the weights after correcting for the number of non-zero weights ;
do j=1 to dim(xs);
	sumD2w+((xs[j]-wtmean_x)**2*ws[j]);
	sumWcorr+(ws[j]*((n_nzw-1)/n_nzw));
end;
rawSD=std(of xs[*]);		* raw SD of Xs ;
wtSD=sqrt(sumD2w/sumWcorr);	* wt SD of Xs ;
drop j;
run;

proc print data=temp2; run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Result:&lt;/P&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="quickbluefish_0-1758912919153.png" style="width: 400px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/110164iC833D257871590B1/image-size/medium?v=v2&amp;amp;px=400" role="button" title="quickbluefish_0-1758912919153.png" alt="quickbluefish_0-1758912919153.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 26 Sep 2025 18:55:27 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-get-weighted-stdev-in-SQL-SAS/m-p/975819#M378184</guid>
      <dc:creator>quickbluefish</dc:creator>
      <dc:date>2025-09-26T18:55:27Z</dc:date>
    </item>
    <item>
      <title>Re: How to get weighted stdev in SQL/SAS?!</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-get-weighted-stdev-in-SQL-SAS/m-p/975837#M378186</link>
      <description>&lt;P&gt;Well, now this is just a rabbit hole.&amp;nbsp; I found several slightly different versions of the formula (population-based, sample/unbiased version, etc.), and they all produce a weighted SD (for the first row) of between 0.89 and 1.06 using the DATA step in SAS.&amp;nbsp; I &lt;U&gt;also&lt;/U&gt; ran this in R several different ways.&amp;nbsp; Same result (using R code generated by both Google AI and ChatGPT).&amp;nbsp; SAS, on the other hand (both PROC SUMMARY and PROC MEANS) produces a wildly different number -- over 5 -- for the same values.&amp;nbsp;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data _temp;
do i=1 to 20;
	x1=sin(i/10);
	x2=cos(i/20);
	x3=sin(i/100)-cos(i/30);
	x4=sin(i/50)+cos(i/200);
	wt_x1=10; 
	wt_x2=25;
	wt_x3=30;
	wt_x4=35;
	output;
end;
run;

** transpose, and, for testing, just keep the first record ;
data t2;
set _temp (obs=1);
array xs {*} x1-x4;
array ws {*} wt_x1-wt_x4;
do i=1 to dim(xs);
	x=xs[i];
	w=ws[i];
	output;
end;
keep i x w;
run;

proc print data=t2; run;

title 'weighted SD from SAS';
proc means data=t2 std;
weight w;
var x;
run;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Result:&lt;/P&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="quickbluefish_0-1758922822551.png" style="width: 400px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/110179iBAD10514B23BBC8E/image-size/medium?v=v2&amp;amp;px=400" role="button" title="quickbluefish_0-1758922822551.png" alt="quickbluefish_0-1758922822551.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;...same value as Paige Miller's PROC SUMMARY code produced for the first record.&lt;/P&gt;
&lt;P&gt;&lt;BR /&gt;In R, on the other hand:&lt;/P&gt;
&lt;PRE&gt;vals &amp;lt;- c(0.09983, 0.99875, -0.98944, 1.01999)
wts  &amp;lt;- c(10, 25, 30, 35)

# weighted mean
wmean &amp;lt;- weighted.mean(vals, wts)

# weighted variance
wvar &amp;lt;- sum(wts * (vals - wmean)^2) / sum(wts)

# weighted standard deviation
wsd &amp;lt;- sqrt(wvar)

wsd&lt;/PRE&gt;
&lt;P&gt;Result:&lt;/P&gt;
&lt;PRE id="rstudio_console_output" class="GNTYACMBB4B" tabindex="0" role="document" aria-label="Console Output"&gt;&lt;SPAN class="GNTYACMBC4B"&gt;&lt;SPAN class="GNTYACMBB4B"&gt;0.8977237&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;/PRE&gt;
&lt;P&gt;If anyone can explain this, that would be great.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 26 Sep 2025 21:49:18 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-get-weighted-stdev-in-SQL-SAS/m-p/975837#M378186</guid>
      <dc:creator>quickbluefish</dc:creator>
      <dc:date>2025-09-26T21:49:18Z</dc:date>
    </item>
    <item>
      <title>Re: How to get weighted stdev in SQL/SAS?!</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-get-weighted-stdev-in-SQL-SAS/m-p/975846#M378191</link>
      <description>Thanks to all. I really appreciate your helps. 

Still I need stick to SAS/SQL. I know how to do with matrix in Matlab or R. 
But the reality with huge data and trying varied calculations still make sense
stay with SAS/SQL. Or I need find any SQL can do this with build-in function
or alike. 

Approach with dataset, surely it is doable and I know how. But may I say 
not what I want.</description>
      <pubDate>Sat, 27 Sep 2025 06:01:44 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-get-weighted-stdev-in-SQL-SAS/m-p/975846#M378191</guid>
      <dc:creator>hellohere</dc:creator>
      <dc:date>2025-09-27T06:01:44Z</dc:date>
    </item>
    <item>
      <title>Re: How to get weighted stdev in SQL/SAS?!</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-get-weighted-stdev-in-SQL-SAS/m-p/975850#M378194</link>
      <description>&lt;P&gt;Here is an example for X1.&lt;/P&gt;
&lt;P&gt;But I don't know why the result is different with SAS. Maybe function used in sas is different.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data _temp;
do i=1 to 20;
	x1=sin(i/10);
	x2=cos(i/20);
	x3=sin(i/100)-cos(i/30);
	x4=sin(i/50)+cos(i/200);
	wt_x1=10; 
	wt_x2=25;
	wt_x3=30;
	wt_x4=35;
	output;
end;
run;

proc sql;
create table temp as
select *,sum(x1*wt_x1)/sum(wt_x1) as wt_mean_x1, (select count(wt_x1) from _temp where wt_x1 ne 0) as n
 from _temp;

/*select sqrt( sum(wt_x1*(x1-wt_mean_x1)**2)  /  sum(wt_x1*( (n-1)/n   )) ) as wt_std*/
 select sqrt( sum(wt_x1*(x1-wt_mean_x1)**2)  /  sum(wt_x1) ) as wt_std
 from temp;
quit;

proc means data=_temp mean stddev ;
var x1;
weight wt_x1;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Ksharp_0-1758959255886.png" style="width: 400px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/110180i51D0EBC96B43AF66/image-size/medium?v=v2&amp;amp;px=400" role="button" title="Ksharp_0-1758959255886.png" alt="Ksharp_0-1758959255886.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sat, 27 Sep 2025 07:48:12 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-get-weighted-stdev-in-SQL-SAS/m-p/975850#M378194</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2025-09-27T07:48:12Z</dc:date>
    </item>
    <item>
      <title>Re: How to get weighted stdev in SQL/SAS?!</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-get-weighted-stdev-in-SQL-SAS/m-p/975853#M378195</link>
      <description>&lt;P&gt;I think I found the answer, you need specify VARDF= option to specify divisor.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;data _temp;
input x1 wt_x1;
cards;
0.09983 10 
0.99875 25
-0.98944 30
1.01999 35&lt;BR /&gt;;


proc sql;
create table temp as
select *,mean(x1) as mean_x1,sum(x1*wt_x1)/sum(wt_x1) as wt_mean_x1, (select count(wt_x1) from _temp where wt_x1 ne 0) as n
 from _temp;

/*select sqrt( sum(wt_x1*(x1-wt_mean_x1)**2)  /  sum(wt_x1*( (n-1)/n   )) ) as wt_std*/
 select sqrt( sum(wt_x1*(x1-wt_mean_x1)**2)  /  sum(wt_x1) ) as wt_std
 from temp;
quit;

proc means data=_temp mean stddev  &lt;FONT color="#FF0000"&gt;&lt;STRONG&gt;VARDEF=WEIGHT&lt;/STRONG&gt;&lt;/FONT&gt;;
var x1;
weight wt_x1;
run;&lt;/PRE&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Ksharp_0-1758960959148.png" style="width: 400px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/110181iAC37DA260679AB45/image-size/medium?v=v2&amp;amp;px=400" role="button" title="Ksharp_0-1758960959148.png" alt="Ksharp_0-1758960959148.png" /&gt;&lt;/span&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="Ksharp_1-1758960988161.png" style="width: 400px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/110182iD0A10E8DE9B52ACF/image-size/medium?v=v2&amp;amp;px=400" role="button" title="Ksharp_1-1758960988161.png" alt="Ksharp_1-1758960988161.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sat, 27 Sep 2025 08:20:40 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-get-weighted-stdev-in-SQL-SAS/m-p/975853#M378195</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2025-09-27T08:20:40Z</dc:date>
    </item>
    <item>
      <title>Re: How to get weighted stdev in SQL/SAS?!</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-get-weighted-stdev-in-SQL-SAS/m-p/975855#M378197</link>
      <description>Thanks, 

The wt_std is on time/i, I bet. That is why diff.</description>
      <pubDate>Sat, 27 Sep 2025 09:44:45 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-get-weighted-stdev-in-SQL-SAS/m-p/975855#M378197</guid>
      <dc:creator>hellohere</dc:creator>
      <dc:date>2025-09-27T09:44:45Z</dc:date>
    </item>
    <item>
      <title>Re: How to get weighted stdev in SQL/SAS?!</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-get-weighted-stdev-in-SQL-SAS/m-p/975856#M378198</link>
      <description>Oh, I mean to get the weighted stdev of x1-x4 EACH ROW/i.</description>
      <pubDate>Sat, 27 Sep 2025 10:06:16 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-get-weighted-stdev-in-SQL-SAS/m-p/975856#M378198</guid>
      <dc:creator>hellohere</dc:creator>
      <dc:date>2025-09-27T10:06:16Z</dc:date>
    </item>
    <item>
      <title>Re: How to get weighted stdev in SQL/SAS?!</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-get-weighted-stdev-in-SQL-SAS/m-p/975858#M378199</link>
      <description>&lt;P&gt;Thanks&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/18408"&gt;@Ksharp&lt;/a&gt;&amp;nbsp;- I had never seen the VARDEF= option.&amp;nbsp; I think, though, that&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/409584"&gt;@hellohere&lt;/a&gt;&amp;nbsp;is trying to get the weighted SD &lt;EM&gt;for each row&lt;/EM&gt;.&amp;nbsp; &amp;nbsp;In any case, I am still baffled by how the other PROC MEANS / SUMMARY outputs (further up) are producing such a large SD (&amp;gt;5).&amp;nbsp; Seems nonsensical.&lt;/P&gt;</description>
      <pubDate>Sat, 27 Sep 2025 12:44:09 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-get-weighted-stdev-in-SQL-SAS/m-p/975858#M378199</guid>
      <dc:creator>quickbluefish</dc:creator>
      <dc:date>2025-09-27T12:44:09Z</dc:date>
    </item>
    <item>
      <title>Re: How to get weighted stdev in SQL/SAS?!</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-get-weighted-stdev-in-SQL-SAS/m-p/975860#M378201</link>
      <description>&lt;P&gt;PROC SQL is almost certainly going to be the least efficient option, both computationally and from a coding perspective.&amp;nbsp; This is especially the case since your data are "row-wise", whereas SQL is best suited for column-wise operations.&amp;nbsp; You would need to transpose.&amp;nbsp; But even so, PROC SQL is not nearly as efficient as "real" SQL engines, plus you cannot nest summary functions in PROC SQL, e.g., SUM ( ... SUM (...) ), which is going to make this equation impossible without pre-computing certain parameters and joining back to the original data.&amp;nbsp; In short, a nightmare.&amp;nbsp; It is possible there's a &lt;STRONG&gt;PROC IML&lt;/STRONG&gt;&amp;nbsp;(which does vectorized / matrix operations) solution if for some reason the other options here don't work for you, but really, &lt;U&gt;is your input data that big?&lt;/U&gt;&amp;nbsp; Do you have 10 billion records??&amp;nbsp; &amp;nbsp;I don't know PROC IML well enough to help on that front.&amp;nbsp; &amp;nbsp;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sat, 27 Sep 2025 13:06:09 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-get-weighted-stdev-in-SQL-SAS/m-p/975860#M378201</guid>
      <dc:creator>quickbluefish</dc:creator>
      <dc:date>2025-09-27T13:06:09Z</dc:date>
    </item>
    <item>
      <title>Re: How to get weighted stdev in SQL/SAS?!</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-get-weighted-stdev-in-SQL-SAS/m-p/975878#M378209</link>
      <description>&lt;P&gt;OK. But you need to transpose your data from wide to long.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data _temp;
do i=1 to 20;
	x1=sin(i/10);
	x2=cos(i/20);
	x3=sin(i/100)-cos(i/30);
	x4=sin(i/50)+cos(i/200);
	wt_x1=10; 
	wt_x2=25;
	wt_x3=30;
	wt_x4=35;
	output;
end;
run;

** transpose ;
data t2;
set _temp;
array xs {*} x1-x4;
array ws {*} wt_x1-wt_x4;
do j=1 to dim(xs);
	x1=xs[j];
	wt_x1=ws[j];
	output;
end;
keep i x1 wt_x1;
run;



proc sql;
create table temp as
select *,mean(x1) as mean_x1,sum(x1*wt_x1)/sum(wt_x1) as wt_mean_x1
 from t2 as a
  group by i;

/*select sqrt( sum(wt_x1*(x1-wt_mean_x1)**2)  /  sum(wt_x1*( (n-1)/n   )) ) as wt_std*/
 select i as row_id,sqrt( sum(wt_x1*(x1-wt_mean_x1)**2)  /  sum(wt_x1) ) as wt_std
 from temp
  group by i;
quit;


*Check the result is consisted with PROC MEANS or not;
proc means data=t2 mean stddev  VARDEF=WEIGHT;
by i;
var x1;
weight wt_x1;
run;&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;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Ksharp_0-1759064312591.png" style="width: 400px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/110189i3A5EA8AA63E32A57/image-size/medium?v=v2&amp;amp;px=400" role="button" title="Ksharp_0-1759064312591.png" alt="Ksharp_0-1759064312591.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sun, 28 Sep 2025 13:05:34 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-get-weighted-stdev-in-SQL-SAS/m-p/975878#M378209</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2025-09-28T13:05:34Z</dc:date>
    </item>
    <item>
      <title>Re: How to get weighted stdev in SQL/SAS?!</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-get-weighted-stdev-in-SQL-SAS/m-p/975879#M378210</link>
      <description>When SAS calculate weighted stddev, the divisor is N-1 , not sum of weight.&lt;BR /&gt;Therefore, you need to specify this option&lt;BR /&gt;VARDEF=WEIGHT&lt;BR /&gt;to make divisor is sum of weight.</description>
      <pubDate>Sun, 28 Sep 2025 13:11:02 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-get-weighted-stdev-in-SQL-SAS/m-p/975879#M378210</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2025-09-28T13:11:02Z</dc:date>
    </item>
    <item>
      <title>Re: How to get weighted stdev in SQL/SAS?!</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-get-weighted-stdev-in-SQL-SAS/m-p/975880#M378211</link>
      <description>&lt;P&gt;Thanks&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/18408"&gt;@Ksharp&lt;/a&gt;&amp;nbsp;- good to know.&amp;nbsp; I saw posts complaining about this issue as far back as 2008.&amp;nbsp; Seems really dumb that this is not the default, because I have yet to see a single version of the equation for weighted SD that involves using a simple N-1 denominator.&amp;nbsp; That said, perhaps the effect is magnified when the N is very small (only 4 in this case), and I honestly can't imagine why anyone would want the SD of 4 numbers.&amp;nbsp;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sun, 28 Sep 2025 13:22:16 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-get-weighted-stdev-in-SQL-SAS/m-p/975880#M378211</guid>
      <dc:creator>quickbluefish</dc:creator>
      <dc:date>2025-09-28T13:22:16Z</dc:date>
    </item>
  </channel>
</rss>

