BookmarkSubscribeRSS Feed
Eugenio211
Quartz | Level 8

Hi, 

 

Hope everybody is doing well.  I just want to ask if someone can look at the code I wrote if it's correct and if there's a way to shorten it.

 

Thanks in advance,

 

below is the code:

 
data Cust_Loc; set Cust_wLoans2 (drop=Dist);
if Store_Is_Open = 0 then do;
 
if (LOC_Inst_loc in (&closed_loc) or LOc_Inst_loc in (&web_loc)) and PDL_Loc = . and Service_Loc = . then Home_Branch = nearest_location;
if (LOC_Inst_loc in (&closed_loc) or LOc_Inst_loc in (&web_loc)) and PDL_Loc ne . and Service_Loc = . then Home_Branch = nearest_location;
if (LOC_Inst_loc in (&closed_loc) or LOc_Inst_loc in (&web_loc)) and PDL_Loc ne . and Service_Loc ne . then Home_Branch = nearest_location;
if (LOC_Inst_loc in (&closed_loc) or LOc_Inst_loc in (&web_loc)) and PDL_Loc = . and Service_Loc ne . then Home_Branch = nearest_location;
 
if (LOC_Inst_loc not in (&closed_loc) and LOc_Inst_loc not in (&web_loc)) and PDL_Loc = . and Service_Loc = . then Home_Branch = LOC_Inst_Loc;
if (LOC_Inst_loc not in (&closed_loc) and LOc_Inst_loc not in (&web_loc)) and PDL_Loc ne . and Service_Loc = . then Home_Branch = LOC_Inst_Loc;
if (LOC_Inst_loc not in (&closed_loc) and LOc_Inst_loc not in (&web_loc)) and PDL_Loc ne . and Service_Loc ne . then Home_Branch = LOC_Inst_Loc;
if (LOC_Inst_loc not in (&closed_loc) and LOc_Inst_loc not in (&web_loc)) and PDL_Loc = . and Service_Loc ne . then Home_Branch = LOC_Inst_Loc;
 
if LOC_Inst_Loc = . and (PDL_Loc in (&closed_loc) or PDL_Loc in (&web_loc)) and Service_Loc = . then Home_Branch = nearest_location;
if LOC_Inst_Loc = . and (PDL_Loc in (&closed_loc) or PDL_Loc in (&web_loc)) and Service_Loc ne . then Home_Branch = nearest_location;
 
if LOC_Inst_Loc = . and (PDL_Loc not in (&closed_loc) and PDL_Loc not in (&web_loc)) and Service_Loc = . then Home_Branch = PDL_Loc;
if LOC_Inst_Loc = . and (PDL_Loc not in (&closed_loc) and PDL_Loc not in (&web_loc)) and Service_Loc ne . then Home_Branch = PDL_Loc;
 
if LOC_Inst_Loc = . and PDL_Loc = . and Service_Loc in (&closed_loc) then Home_Branch = nearest_location;
if LOC_Inst_Loc = . and PDL_Loc = . and Service_Loc not in (&closed_loc) then Home_Branch = Service_Loc;
 
end;
 
if Store_Is_Open = 1 then do;
 
if (LOC_Inst_Loc ne . and LOC_Inst_Loc not in (&web_loc)) and PDL_Loc = . and Service_Loc = . then Home_Branch = LOC_Inst_Loc;
if (LOC_Inst_Loc ne . and LOC_Inst_Loc not in (&web_loc)) and PDL_Loc ne . and Service_Loc = . then Home_Branch = LOC_Inst_Loc;
if (LOC_Inst_Loc ne . and LOC_Inst_Loc not in (&web_loc)) and PDL_Loc ne . and Service_Loc ne . then Home_Branch = LOC_Inst_Loc;
 
if LOC_Inst_loc = . and (PDL_Loc ne . and PDL_loc not in (&web_loc)) and Service_loc = . then Home_Branch = PDL_Loc;
if LOC_Inst_loc = . and (PDL_Loc ne . and PDL_loc not in (&web_loc)) and Service_loc ne . then Home_Branch = PDL_Loc;
 
if LOC_Inst_loc = . and PDL_Loc = . and (Service_loc ne . and Service_loc not in (&web_loc)) then Home_Branch = Service_loc;
 
if (LOC_Inst_Loc ne . and LOC_Inst_Loc in (&web_loc)) and PDL_Loc = . and Service_Loc = . then Home_Branch = nearest_location;
if (LOC_Inst_Loc ne . and LOC_Inst_Loc in (&web_loc)) and PDL_Loc ne . and Service_Loc = . then Home_Branch = nearest_location;
if (LOC_Inst_Loc ne . and LOC_Inst_Loc in (&web_loc)) and PDL_Loc ne . and Service_Loc ne . then Home_Branch = nearest_location;
 
if LOC_Inst_loc = . and (PDL_Loc ne . and PDL_loc in (&web_loc)) and Service_loc = . then Home_Branch = nearest_location;
if LOC_Inst_loc = . and (PDL_Loc ne . and PDL_loc in (&web_loc)) and Service_loc ne . then Home_Branch = nearest_location;
 
if (LOC_Inst_Loc ne . and LOC_Inst_Loc not in (&web_loc)) and PDL_Loc = . and Service_Loc ne . then Home_Branch = LOC_Inst_Loc;
if (LOC_Inst_Loc ne . and LOC_Inst_Loc in (&web_loc)) and PDL_Loc = . and Service_Loc ne . then Home_Branch = Service_Loc;
 
if LOC_Inst_Loc = . and PDL_Loc = . and Service_Loc = . then Home_Branch = nearest_location;
 
if (LOC_Inst_Loc ne . and LOC_Inst_Loc in (&closed_loc)) and PDL_Loc = . and Service_Loc = . then Home_Branch = Nearest_Location;
if (LOC_Inst_Loc ne . and LOC_Inst_Loc in (&closed_loc)) and PDL_Loc ne . and Service_Loc = . then Home_Branch = Nearest_Location;
if (LOC_Inst_Loc ne . and LOC_Inst_Loc in (&closed_loc)) and PDL_Loc ne . and Service_Loc ne . then Home_Branch = Nearest_Location;
if (LOC_Inst_Loc ne . and LOC_Inst_Loc in (&closed_loc)) and PDL_Loc = . and Service_Loc ne . then Home_Branch = Nearest_Location;
 
if LOC_Inst_loc = . and (PDL_Loc ne . and PDL_loc in (&closed_loc)) and Service_loc = . then Home_Branch = Nearest_Location;
if LOC_Inst_loc = . and (PDL_Loc ne . and PDL_loc in (&closed_loc)) and Service_loc ne . then Home_Branch = Nearest_Location;
 
if LOC_Inst_Loc in (&web_loc) and PDL_Loc = . and Service_Loc in (&closed_loc) then Home_Branch = Nearest_Location;
 
end;
 
format Home_Branch best.;
;run;

 

2 REPLIES 2
PaigeMiller
Diamond | Level 26

I just want to ask if someone can look at the code I wrote if it's correct and if there's a way to shorten it.

 

How can we know if it is correct? You know if it is correct, because it either does what you want, or it does not do what you want.

 

As far as shortening it, let's look at this part:

 

if (LOC_Inst_loc in (&closed_loc) or LOc_Inst_loc in (&web_loc)) and PDL_Loc = . and Service_Loc = . then Home_Branch = nearest_location;
if (LOC_Inst_loc in (&closed_loc) or LOc_Inst_loc in (&web_loc)) and PDL_Loc ne . and Service_Loc = . then Home_Branch = nearest_location;
if (LOC_Inst_loc in (&closed_loc) or LOc_Inst_loc in (&web_loc)) and PDL_Loc ne . and Service_Loc ne . then Home_Branch = nearest_location;
if (LOC_Inst_loc in (&closed_loc) or LOc_Inst_loc in (&web_loc)) and PDL_Loc = . and Service_Loc ne . then Home_Branch = nearest_location;

 

In all four of the statements above, you set Home_Brach to be equal to nearest_location. Then you don't need four statements. In fact, it seems that no matter what the values of PDL_Loc and Service_Loc are, you set Home_Branch to be equal to nearest_location.

 

So the four statements can really be reduced to one statement

 

if (LOC_Inst_loc in (&closed_loc) or LOc_Inst_loc in (&web_loc)) then Home_Branch = nearest_location;

 

I didn't look carefully at the rest of your code.

--
Paige Miller
Tom
Super User Tom
Super User

What is it supposed to be doing?

You shouldn't code a boolean expression like

A or B and C

What does that mean?  Which operator should SAS execute first?

Add parentheses so you are certain it will do what you want because these two possibilities will yield different results:

(A or B) and C
A or (B and C)

 

Also it does not make much sense to code 

if A and B and C then X=1;
if A and B and not C then X=1;
if A and not B and C then X=1;
if A and not B and not C then X=1;

Since all combinations of B and C and set the value of X to the same thing each time you simplify to 

if A then X=1;

sas-innovate-wordmark-2025-midnight.png

Register Today!

Join us for SAS Innovate 2025, our biggest and most exciting global event of the year, in Orlando, FL, from May 6-9. Sign up by March 14 for just $795.


Register now!

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
  • 2 replies
  • 644 views
  • 0 likes
  • 3 in conversation