1
votes

I have a data set with the following variables (Segment, Quantity and Price). What I need is to compute the sum of “Quantity” and the average “Price” for every segment, up until the last Segment in the series, as shown in the example. I tried different approaches with DO IF, LOOP and LAG but unsuccessfully. Thanks!

Segment Cuantity Price Sum_Quantity Average_Prrice

1 4 20
1 5 10
1 1 20
1 4 10
1 3 20 17 16

2 6 40
2 9 20
2 23 30 38 30

1 4 30
1 6 20 10 25

2 15 15
2 12 20
2 3 10 30 15

...

1

1 Answers

1
votes

Look up AGGREGATE

To add the SUM and MEAN as variables into the existing dataset:

AGGREGATE OUTFILE=* /BREAK=SEGMENT
 /Sum_Quantity=SUM(Quantity)
 /Ave_Price=MEAN(Price).

Alternatively, to create as new dataset:

DATESET DECLARE dsAgg.
AGGREGATE OUTFILE=dsAgg /BREAK=SEGMENT
 /Sum_Quantity=SUM(Quantity)
 /Ave_Price=MEAN(Price).