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.

SAS Innovate 2025: Call for Content

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!

Submit your idea!

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

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