BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
Midi
Obsidian | Level 7

Hello Everyone , I have This DataSet :

 

 User   SPENDING
User_1    50,32$
User_1   250,36$
User_1   322,2$
User_1   222,2$
User_2    30,2$
User_2    563$
User_2   24,23$
User_2   210,32$

 

And What I Want To Do , is to make an iterative sum for each user  ,i.e: 

like for user_1 i want , for the first line , the first spending , for the second column , i want , to sum of spending between line 1 and line 2 , for the third column , i want the sum of spending in line1 , line2 , line 3 .....ect , like my output would look like this :

 

 User   SPENDING Total
User_1    50,32$ 50,32$
User_1 250,36$ 300,68$
User_1 322,2$ 622,88$
User_1 222,2$ 845,08$
User_2 30,2$ 30,2$
User_2 563$ 593,2$
User_2 24,23$ 617,43$
User_2 210,32$ 827,75$

 

Any suggestions on how to do that , would be much appreciated , thank you.

1 ACCEPTED SOLUTION

Accepted Solutions
PeterClemmensen
Tourmaline | Level 20

Do like this

 

data have;
input User $ SPENDING commax10.;
datalines;
User_1	   50,32$
User_1	  250,36$
User_1	  322,2$
User_1	  222,2$
User_2	   30,2$
User_2	   563$
User_2	  24,23$
User_2	  210,32$
;

data want;
    set have;
    by User;
    if first.User then Total=0;
    Total+SPENDING;
    format SPENDING Total dollar10.2;
run;

Result:

 

User    SPENDING    Total
User_1	$50.32	    $50.32
User_1	$250.36	    $300.68
User_1	$322.20	    $622.88
User_1	$222.20	    $845.08
User_2	$30.20	    $30.20
User_2	$563.00	    $593.20
User_2	$24.23	    $617.43
User_2	$210.32	    $827.75

View solution in original post

1 REPLY 1
PeterClemmensen
Tourmaline | Level 20

Do like this

 

data have;
input User $ SPENDING commax10.;
datalines;
User_1	   50,32$
User_1	  250,36$
User_1	  322,2$
User_1	  222,2$
User_2	   30,2$
User_2	   563$
User_2	  24,23$
User_2	  210,32$
;

data want;
    set have;
    by User;
    if first.User then Total=0;
    Total+SPENDING;
    format SPENDING Total dollar10.2;
run;

Result:

 

User    SPENDING    Total
User_1	$50.32	    $50.32
User_1	$250.36	    $300.68
User_1	$322.20	    $622.88
User_1	$222.20	    $845.08
User_2	$30.20	    $30.20
User_2	$563.00	    $593.20
User_2	$24.23	    $617.43
User_2	$210.32	    $827.75

sas-innovate-wordmark-2025-midnight.png

Register Today!

Join us for SAS Innovate 2025, our biggest and most exciting global event of the year, in Orlando, FL, from May 6-9. Sign up by March 14 for just $795.


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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 1 reply
  • 843 views
  • 1 like
  • 2 in conversation