BookmarkSubscribeRSS Feed
hellohere
Pyrite | Level 9
Oh, I mean to get the weighted stdev of x1-x4 EACH ROW/i.
Ksharp
Super User

OK. But you need to transpose your data from wide to long.

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;

 

 

Ksharp_0-1759064312591.png

 

quickbluefish
Barite | Level 11

Thanks @Ksharp - I had never seen the VARDEF= option.  I think, though, that @hellohere is trying to get the weighted SD for each row.   In any case, I am still baffled by how the other PROC MEANS / SUMMARY outputs (further up) are producing such a large SD (>5).  Seems nonsensical.

Ksharp
Super User
When SAS calculate weighted stddev, the divisor is N-1 , not sum of weight.
Therefore, you need to specify this option
VARDEF=WEIGHT
to make divisor is sum of weight.
quickbluefish
Barite | Level 11

Thanks @Ksharp - good to know.  I saw posts complaining about this issue as far back as 2008.  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.  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.  

PaigeMiller
Diamond | Level 26

If ever there was an argument for not writing your own code to do this type of calculation, this thread is it.

--
Paige Miller

hackathon24-white-horiz.png

The 2025 SAS Hackathon has begun!

It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.

Latest Updates

How to Concatenate Values

Learn how use the CAT functions in SAS to join values from multiple variables into a single value.

Find more tutorials on the SAS Users YouTube channel.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 20 replies
  • 781 views
  • 4 likes
  • 4 in conversation