Solved
Contributor
Posts: 42

# Suming variables calculation

I have a data set with two columns and  I need to calculate a variable where we give +1 point  if col A =col B but if col A not equal to col B than give variable A -1 and variable B +1

Data set:

A                   B

Tim           Jack ( jack get +1 and Tim -1)

Jack          Bob  (Bob gets +1 and jack -1)

sarah        Sarah ( sarah gets +1)

Bob           Tim (Tim gets +1 and Bob -1)

Tim            Bob (Bob gets +1 and Tim-1)

Jack        Jack (jack gets +1)

sarah      sarah (sarah get +1 )

Bob         Bob ( Bob gets +1)

want a column where it sum them

Bob = +1+1+1-1 = 2

sarah = +1+1 =2

Tim = -1+1-1 = -1

jack= +1-1+1 =1

Accepted Solutions
Solution
‎02-20-2018 05:41 PM
Super User
Posts: 6,933

## Re: Suming variables calculation

To avoid accidents, I'm going to uppercase the names.  In the SAS world, "Sarah" is not equal to "sarah".  But for your analysis, I suspect you want them to be the same person.

data want;

set have;

name = upcase(B);

value = 1;

output;

if upcase(A) ne upcase(B) then do;

name = upcase(A);

value = -1;

output;

end;

keep name value;

run;

proc means data=want sum maxdec=0;

class name;

var value;

run;

All Replies
Super User
Posts: 24,004

## Re: Suming variables calculation

[ Edited ]

How many unique values do you have in A/B?

I would probably change my data format and then sum it up using PROC MEANS or SQL.

This approach means you don't need to know the number of names or mappings at all.

EDITED: to handle the A=B case, but still untested.

``````data want;
set have (rename=A=Name in=A keep=Name where=(Name ne B))
have (rename=B=Name in=B keep=Name where=(Name ne A))
have (rename=B=Name in=C keep=Name where=(Name eq B))
;

if A or C then value=1;
if B then value=-1;

run;``````

hk2013 wrote:

I have a data set with two columns and  I need to calculate a variable where we give +1 point  if col A =col B but if col A not equal to col B than give variable A -1 and variable B +1

Data set:

A                   B

Tim           Jack ( jack get +1 and Tim -1)

Jack          Bob  (Bob gets +1 and jack -1)

sarah        Sarah ( sarah gets +1)

Bob           Tim (Tim gets +1 and Bob -1)

Tim            Bob (Bob gets +1 and Tim-1)

Jack        Jack (jack gets +1)

sarah      sarah (sarah get +1 )

Bob         Bob ( Bob gets +1)

want a column where it sum them

Bob = +1+1+1-1 = 2

sarah = +1+1 =2

Tim = -1+1-1 = -1

jack= +1-1+1 =1

Posts: 3,275

## Re: Suming variables calculation

[ Edited ]

UNTESTED CODE

``````data want;
set have;
name=upcase(a);
if upcase(a)=upcase(b) then value=1;
else value=-1;
output;
name=upcase(b);
if upcase(a)^=upcase(b) then value=1;
output;
drop a b;
run;
proc summary nway data=want;
class name;
var value;
output out=sums sum=;
run;
``````
--
Paige Miller
Solution
‎02-20-2018 05:41 PM
Super User
Posts: 6,933

## Re: Suming variables calculation

To avoid accidents, I'm going to uppercase the names.  In the SAS world, "Sarah" is not equal to "sarah".  But for your analysis, I suspect you want them to be the same person.

data want;

set have;

name = upcase(B);

value = 1;

output;

if upcase(A) ne upcase(B) then do;

name = upcase(A);

value = -1;

output;

end;

keep name value;

run;

proc means data=want sum maxdec=0;

class name;

var value;

run;

☑ This topic is solved.