Dear,
I come a sas expression. I want to understand the logic in this expression.
censor=1*(cnsr=0)+0*(cnsr=1); Please help me Thank you
I understand, if cnsr=0 then multiply with 1 or if cnsr=0 then multiply with 1. Is this correct.
data one;
input cnsr;
datalines;
0
1
0
1
1
;
data teo;
set one;
censor=1*(cnsr=0)+0*(cnsr=1);
run;
A convoluted way to say,
if cnsr=1 then censor=0, else if cnsr=0 then censor=1.
CNSR=0 will resolve to 1 if true, 0 if false.
If CNSR=0 then it becomes:
1*1+1*0 = 1
If CNSR=1 then it becomes:
1*0 + 0*1 = 0
This is one way to write "if then else" value assignment statements. Usually an "old programmer" trick as the numeric calculations run faster in most cases than actual If / then/ else. Especially if you have multiple else if following.
In this specific case of binary values you might also compare the result of:
data teo; set one; censor=1*(cnsr=0)+0*(cnsr=1); censor2 = not(cnsr); run;
You might want to confirm that you get the desired result for missing values of CNSR though.
This has already been solved but i think it is useful to break logic down when you don't understand it. The below code takes apart all of the elements of the logic and puts them together slowly. At the end you can see how the code could have been shortened or replaced with a simple not. I suggest running it an viewing the results yourself.
data have;
input cnsr;
datalines;
1
0
;
run;
data want;
set have;
cn0 = (cnsr=0);
OneCn0 = 1*(cnsr=0);
cn1 = (cnsr=1);
ZeorCn1 = 0*(cnsr=1);
censor=1*(cnsr=0)+0*(cnsr=1);
BetterCensor=(cnsr=0);
NotCNSR = Not(cnsr);
;
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.
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.