Stata question; I need to count the number of occurrences of a value and average another variable?

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 custome

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!

or: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 (\u2018cusn\u2019). 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 \u2018cusn\u2019. Can anyone help with this?The relevant available variables are:\u2018cusn\u2019 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)\u2018quan\u2019 is the quantity sold to the customer in that particular transactionAs I see it; I first need to count the number of times a specific \u2018cusn\u2019 appears, then aggregate the total \u2018quan\u2019 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!

Tags: