BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
Phil_NZ
Barite | Level 11

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;
 
Thank you for your help, have a fabulous and productive day! I am a novice today, but someday when I accumulate enough knowledge, I can help others in my capacity.
1 ACCEPTED SOLUTION

Accepted Solutions
ChrisNZ
Tourmaline | Level 20

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.

View solution in original post

3 REPLIES 3
ChrisNZ
Tourmaline | Level 20

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.

Phil_NZ
Barite | Level 11

Thanks @ChrisNZ 

Next time I will Ctrl+I before paste.

Have a good week!

Thank you for your help, have a fabulous and productive day! I am a novice today, but someday when I accumulate enough knowledge, I can help others in my capacity.
japelin
Rhodochrosite | Level 12

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.

hackathon24-white-horiz.png

The 2025 SAS Hackathon Kicks Off on June 11!

Watch the live Hackathon Kickoff to get all the essential information about the SAS Hackathon—including how to join, how to participate, and expert tips for success.

YouTube LinkedIn

How to Concatenate Values

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 3 replies
  • 1004 views
  • 5 likes
  • 3 in conversation