Hi all, I have the code below
DATA want;
SET have;
If cvt_yr=-4 then dkt_4=1;
else if cvt_yr=-3 then dkt_3=1;
else if cvt_yr=-2 then dkt_2=1;
else if cvt_yr=-1 then dkt_1=1;
else if cvt_yr=0 then dkt=1;
else if cvt_yr=1 then dkt1=1;
else if cvt_yr=2 then dkt2=1;
else if cvt_yr=3 then dkt3=1;
else if cvt_yr>=4 then dkt4=1;
run;
I am wondering what is the difference between this code with
DATA want;
SET have;
If cvt_yr=-4 then dkt_4=1;
if cvt_yr=-3 then dkt_3=1;
if cvt_yr=-2 then dkt_2=1;
if cvt_yr=-1 then dkt_1=1;
if cvt_yr=0 then dkt=1;
if cvt_yr=1 then dkt1=1;
if cvt_yr=2 then dkt2=1;
if cvt_yr=3 then dkt3=1;
if cvt_yr>=4 then dkt4=1;
run;
The result will be the same in this precise case, but the first code is more efficient as not all tests are executed.
Also please align/format your code properly 🙂 This is much helped by never using tabs and using spaces instead. This way any cut-and-paste is sure to retain the original format.
The result will be the same in this precise case, but the first code is more efficient as not all tests are executed.
Also please align/format your code properly 🙂 This is much helped by never using tabs and using spaces instead. This way any cut-and-paste is sure to retain the original format.
Thanks @ChrisNZ
Next time I will Ctrl+I before paste.
Have a good week!
The same is true for this condition.
However, repeating if may cause unintended conditional branches in data and conditions. For exclusive cases, it is better to use if-else or select-when.
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!
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.