Sum of the values of the previous dates in SAS

Kavitha

I want to take the sum of the values of previous dates except current date by Product. Given below is the dataset. In the output table, I have mentioned how my summation of values of previous dates should look like.

For Eg: For the date 7/27/2014 I want the summation of the previous dates except current date by product level. Similarly when you are taking the summation for 7/20/2014 ignore the values for 7/20/2014 and also 7/27/2014 and take rest of the previous dates. Also there is a exception here. When you see duplicates for example 7/20/2014 and 6/8/2014 consider just one value.

DATE    DEALID      PRODUCT      VALUE
7/27/2014   6575    CLIENT      4
7/20/2014   16701   CLIENT      6
7/20/2014   16701   CLIENT      6
7/13/2014   6601    CLIENT      4
7/6/2014    10871   SERVICES    5
6/29/2014   16661   SERVICES    2
6/22/2014   66757   SERVICES    1
6/15/2014   77757   SERVICES    5
6/8/2014    5675    SERVICES    8
6/8/2014    5675    SERVICES    8
5/25/2014   5756    SERVICES    4

output table

DATE    DEALID  PRODUCT       VALUE SUMM
7/27/2014   6575    CLIENT      4   10
7/20/2014   16701   CLIENT      6   4
7/20/2014   16701   CLIENT      6   4
7/13/2014   6601    CLIENT      4   0
7/6/2014    10871   SERVICES    5   20
6/29/2014   16661   SERVICES    2   18
6/22/2014   66757   SERVICES    1   17
6/15/2014   77757   SERVICES    5   12
6/8/2014    5675    SERVICES    8   4
6/8/2014    5675    SERVICES    8   4
5/25/2014   5756    SERVICES    4   0
Simke Nys

Plain old SAS data steps are usualy more practical and often run faster for very specific requirements.

Let us start like NEO_mental to create the data

data test;
                infile datalines;
                format date mmddyy10.; /** Make SAS print dates as a date instead of numbers **/
                input date : mmddyy10. 
                                  Dealid : $ 8.
                                  PRODUCT : $10.
                                  Value : 8.
                                  ;
datalines;
7/27/2014   6575    CLIENT      4
7/20/2014   16701   CLIENT      6
7/20/2014   16701   CLIENT      6
7/13/2014   6601    CLIENT      4
7/6/2014    10871   SERVICES    5
6/29/2014   16661   SERVICES    2
6/22/2014   66757   SERVICES    1
6/15/2014   77757   SERVICES    5
6/8/2014    5675    SERVICES    8
6/8/2014    5675    SERVICES    8
5/25/2014   5756    SERVICES    4
;
run;

To calculate a running total, I sort in ascending date order. I do not remove duplicates, as I handle them in my data step.

proc sort data=test out=ascendingTest;
                by Product Date;
run;

Here comes the good old data step, in which I do all the calculations

/** Create a dataset including the running total **/
Data summTest;

                /** Read in the data **/
                set ascendingTest;

                /** Enable things like first.Product and last.Date **/
                by Product Date;

                /** Create the running total **/
                /** variables are initialised for each observation (=row) unless you retain them **/
                retain Summ;
                if first.Product then Summ = 0; /** Start over for each product **/

                /** Write out the result BEFORE increasing the total **/
                output;

                /** Increase the running total for the later dates **/
                if last.Date then Summ = Summ + Value;
run;

As we did not remove duplicates, I do not need to merge, so all I have to do is to sort on descending date agian. Note: if performance is an issue, write 'Data summTest / view=summTest. This way the data step will not read any data and the calculations will only be done when the sort step consumes the results. ;

proc sort data=summTest out=final;
                by Product descending Date ;
run;

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

SAS: Joining dates to sum a column

From Dev

Oracle sum previous values

From Dev

Query to sum the previous values

From Dev

Query to sum the previous values

From Dev

r: sum values of previous observations

From Dev

How can sum values from the previous month?

From Dev

Get sum of previous 6 values including the group

From Dev

SAS Do-Loop and IF Statement to compare Current and previous row values

From Dev

How to Convert Date values in SAS into Regular Dates Using SSIS

From Dev

How to Convert Date values in SAS into Regular Dates Using SSIS

From Dev

Group (Find) the similar columns in to a group and sum the values of each group in SAS

From Dev

Sum of the values in the previous cell/s, if current cell/s is an error

From Dev

Sum every last value with all previous values in array

From Dev

Sum of values from 3rd previous month

From Dev

Gettting cumulative sum of previous values except for the first value

From Dev

Sum of the values in the previous cell/s, if current cell/s is an error

From Dev

Sum ONLY at the last row where previous rows have same values

From Dev

How to find MySQL rows based on sum of values on adjacent dates?

From Dev

Python Pandas Sum Values in Columns If date between 2 dates

From Dev

Combine array of dates and sum values per month in PHP

From Dev

Combine array of dates and sum values per month in Java

From Dev

Use VBA to make pivot table sum values based on dates only?

From Dev

Difference in dates in SAS by group

From Dev

SAS Macros dates issue

From Dev

SAS Dates Import issue

From Dev

SAS Macros dates issue

From Dev

Difference in dates in SAS by group

From Dev

Changing values in previous and post records when a numerical condition is met using SAS

From Dev

Disable previous dates in datepicker

Related Related

  1. 1

    SAS: Joining dates to sum a column

  2. 2

    Oracle sum previous values

  3. 3

    Query to sum the previous values

  4. 4

    Query to sum the previous values

  5. 5

    r: sum values of previous observations

  6. 6

    How can sum values from the previous month?

  7. 7

    Get sum of previous 6 values including the group

  8. 8

    SAS Do-Loop and IF Statement to compare Current and previous row values

  9. 9

    How to Convert Date values in SAS into Regular Dates Using SSIS

  10. 10

    How to Convert Date values in SAS into Regular Dates Using SSIS

  11. 11

    Group (Find) the similar columns in to a group and sum the values of each group in SAS

  12. 12

    Sum of the values in the previous cell/s, if current cell/s is an error

  13. 13

    Sum every last value with all previous values in array

  14. 14

    Sum of values from 3rd previous month

  15. 15

    Gettting cumulative sum of previous values except for the first value

  16. 16

    Sum of the values in the previous cell/s, if current cell/s is an error

  17. 17

    Sum ONLY at the last row where previous rows have same values

  18. 18

    How to find MySQL rows based on sum of values on adjacent dates?

  19. 19

    Python Pandas Sum Values in Columns If date between 2 dates

  20. 20

    Combine array of dates and sum values per month in PHP

  21. 21

    Combine array of dates and sum values per month in Java

  22. 22

    Use VBA to make pivot table sum values based on dates only?

  23. 23

    Difference in dates in SAS by group

  24. 24

    SAS Macros dates issue

  25. 25

    SAS Dates Import issue

  26. 26

    SAS Macros dates issue

  27. 27

    Difference in dates in SAS by group

  28. 28

    Changing values in previous and post records when a numerical condition is met using SAS

  29. 29

    Disable previous dates in datepicker

HotTag

Archive