DATA Step, Macro, Functions and more

sas code interpretation help

Reply
Super Contributor
Posts: 312

sas code interpretation help

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;

 

 

Super User
Posts: 23,224

Re: sas code interpretation help

Posted in reply to knveraraju91

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

 

 

Super User
Posts: 13,283

Re: sas code interpretation help

Posted in reply to knveraraju91

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.

 

Occasional Contributor
Posts: 10

Re: sas code interpretation help

[ Edited ]
Posted in reply to knveraraju91

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;

 

Ask a Question
Discussion stats
  • 3 replies
  • 102 views
  • 4 likes
  • 4 in conversation