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:
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.
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;
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.
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.
Ready to level-up your skills? Choose your own adventure.