BookmarkSubscribeRSS Feed
Ankur
Calcite | Level 5

Hi,

In order to measure performance and whether we can reduce any execution time, I am trying to convert some of SAS code into SQL query. While doing it, I came across a piece of code weight is being calculated. Need to know how SAS calculates weight for any given column so that I can try and replicate the same using SQL.

proc means data=input_file noprint;

var C_A/weight=T_A;

by period;

output out=output_file

mean=

Any insight on this would be of great help to me.

Thanks,

ankur

5 REPLIES 5
Patrick
Opal | Level 21

The formulas used are  - if not obvious - normally documented as part of the documentation for the procedure.

Not sure if changing everything to SQL will help you a lot unless the data tables are stored in a data base. If stored in a data base: You might want to investigate the in-database capabilities of some of the PROCs (eg. Proc Means).

The quickest way of improving performance is in my experience to reduce passes through the data, reduction of volumes as fast as possible, re-design lookups (eg. by using hash tables).

Ankur
Calcite | Level 5

Patrick, Appreciate your response and I do agree with the Performance improvement point.

But as per the assignment, I need some way to do it in SQL. If some one could provide a sample input/output with the weight function, then I can re-engineer it to see how its calculated.

Thanks & Best Regards,

Ankur Vatsal

MarkFisher
Calcite | Level 5

It would take two passes through the data to get the same result in SQL, but the final formula would be:

C_A*weight/sum_of_weights as weighted_mean

where the sum of weights would need to be calculated for each period.  You would need to join the summed weights back to the original data.

MarkFisher
Calcite | Level 5

I clicked too soon--the weighted mean for each period would be the sum of C_A*weight divided by the sum of the weights, for each period.

Reeza
Super User

It's a weighted average

Weighted arithmetic mean - Wikipedia, the free encyclopedia

Additionally you need to account for missing and negative weights separately

Weight value... PROC MEANS...
0 counts the observation in the total number of observations
less than 0 converts the value to zero and counts the observation in the total number of observations
missing excludes the observation

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

Register now!

What is Bayesian Analysis?

Learn the difference between classical and Bayesian statistical approaches and see a few PROC examples to perform Bayesian analysis in this video.

Find more tutorials on the SAS Users YouTube channel.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 5 replies
  • 1022 views
  • 0 likes
  • 4 in conversation