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
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;
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
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;
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;
Don't miss out on SAS Innovate - Register now for the FREE Livestream!
Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.
What’s the difference between SAS Enterprise Guide and SAS Studio? How are they similar? Just ask SAS’ Danny Modlin.
Find more tutorials on the SAS Users YouTube channel.