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

I have a dataset with below data:

i need sum of deposit & sum of withdrawals of each employee

 

namemoney
Andrea-10000
Mark149513
Kassidy-5016
Andrea17500
Andrea2500
Jim100000
Jim-50
Jim-50
Jim-50
Kassidy-2013

 

Output should be like this:

namesum of depositsum of withdraws
Andrea2000010000
Mark100000150
Kassidy07029
Jim1495130
1 ACCEPTED SOLUTION

Accepted Solutions
novinosrin
Tourmaline | Level 20
proc sort data=have;
by name;
run;

data want;
set have;
by name;
if first.name then do;
sumofdeposit=0;
sumofwithdraw=0;
end;
sumofdeposit+(money>=0)*money;
sumofwithdraw+(money<0)*money;
if last.name;
sumofwithdraw=abs(sumofwithdraw);
keep name sum:;
run;

 

View solution in original post

5 REPLIES 5
novinosrin
Tourmaline | Level 20

data have;
input name $	money;
cards;
Andrea	-10000
Mark	149513
Kassidy	-5016
Andrea	17500
Andrea	2500
Jim	100000
Jim	-50
Jim	-50
Jim	-50
Kassidy	-2013
;

proc sql;
create table want as
select name, sum((money>=0)*money) as sumofdeposit,abs((sum((money<0)*money))) as sumofwithdraw
from have
group by name;
quit;
novinosrin
Tourmaline | Level 20
proc sort data=have;
by name;
run;

data want;
set have;
by name;
if first.name then do;
sumofdeposit=0;
sumofwithdraw=0;
end;
sumofdeposit+(money>=0)*money;
sumofwithdraw+(money<0)*money;
if last.name;
sumofwithdraw=abs(sumofwithdraw);
keep name sum:;
run;

 

aman23
Obsidian | Level 7

why below variables are multiplied?? can you show or explain with the help of any of the observation from data only?

 

(money>=0)*money;
(money<0)*money;

 

novinosrin
Tourmaline | Level 20

Just converting the values to boolean 1's and 0's  based on the need .

So when 

money>=0)

is true, the value would be 1 and when false the value is 0

 

And the same for withdrawal expression 

aman23
Obsidian | Level 7

Got it thanks a lot 🙂

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

Register now!

Mastering the WHERE Clause in PROC SQL

SAS' Charu Shankar shares her PROC SQL expertise by showing you how to master the WHERE clause using real winter weather data.

Find more tutorials on the SAS Users YouTube channel.

Discussion stats
  • 5 replies
  • 1274 views
  • 3 likes
  • 2 in conversation