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

Ready to join fellow brilliant minds for the SAS Hackathon?

Build your skills. Make connections. Enjoy creative freedom. Maybe change the world. Registration is now open through August 30th. Visit the SAS Hackathon homepage.

Register today!
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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

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