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.

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

Register now!

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.

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