Heya,

I have a dataset including the total number of sales of a certain item, the price and a lot of other variables. Also recorded is a unique customer ID for returning customers (‘cusn’). Now I need a new variable avquan which gives the average quantity sold to that specific person in the entire time period. Thus, first I need to generate the sum of all quantitities sold per customer, and then average this using the number of occurrences of that ‘cusn’. Can anyone help with this?

The relevant available variables are:

‘cusn’ a customer number for each unique customer (1-258), recorded with any transaction done by that customer (has a lot of missing values for non-recurring customers)

‘quan’ is the quantity sold to the customer in that particular transaction

As I see it; I first need to count the number of times a specific ‘cusn’ appears, then aggregate the total ‘quan’ over all these appearences, and then divide TOTALQUAN by the number of appearances. Can anyone help me with the quickest and most efficient code for this?

If it's not clear like this, I am very sorry, tell me and I will try to elaborate.
Thanks!