BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
ali11
Fluorite | Level 6

Hello! I'm trying to extract sum information from a relatively big dataset I'm working with. At this point, I have information about thousands of companies identified by CUSIP (a unique identifier), prices and quantity data and corresponding dates. Here is what I want to do: 

I am trying to write a loop (perfect if a loop is not necessary indeed) that selects a specific company (by CUSIP) and produce a value weighted number (Price*Quantity)/(Sum(Quantity| for a specific date) such that for example according to the image below, row 1-7 is replaced by a single row containing the aggregated data (CUSIP and date remain the same). I do not know where to start and any hints would be very much appreciated. FYI, I studied how loops work in SAS, but I do not know how to write a loop that at each step selects a CUSIP for a specific Execution_date and performs the aggregation, saves it and moves to the second CUSIP-date combination.

Thanks in advance!

Screen Shot 2020-01-13 at 7.05.07 PM.png

1 ACCEPTED SOLUTION

Accepted Solutions
PaigeMiller
Diamond | Level 26

What you are describing is a weighted avereage. This is done via PROC SUMMARY, no looping neeed.

 

Something like this:

 

proc summary data=have nway;
     class cusip_id execution_date;
     var price/weight=quantity;
     output out=aggregation mean=;
run;

 

Suggestion: since you are using SAS, which is one of the leading statistical analysis available, if not the leading statistical package available, please know that an extremely large number of analyses have been built into SAS, including summing and computing means. Before you come to the conclusion that you will have to program some loops somehow to do this, please save yourself some time and ask/investigate if it has already been programmed.

 
 
 
--
Paige Miller

View solution in original post

2 REPLIES 2
PaigeMiller
Diamond | Level 26

What you are describing is a weighted avereage. This is done via PROC SUMMARY, no looping neeed.

 

Something like this:

 

proc summary data=have nway;
     class cusip_id execution_date;
     var price/weight=quantity;
     output out=aggregation mean=;
run;

 

Suggestion: since you are using SAS, which is one of the leading statistical analysis available, if not the leading statistical package available, please know that an extremely large number of analyses have been built into SAS, including summing and computing means. Before you come to the conclusion that you will have to program some loops somehow to do this, please save yourself some time and ask/investigate if it has already been programmed.

 
 
 
--
Paige Miller
ali11
Fluorite | Level 6
thanks. very helpful and just what I needed.

hackathon24-white-horiz.png

The 2025 SAS Hackathon Kicks Off on June 11!

Watch the live Hackathon Kickoff to get all the essential information about the SAS Hackathon—including how to join, how to participate, and expert tips for success.

YouTube LinkedIn

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
  • 2 replies
  • 898 views
  • 2 likes
  • 2 in conversation