Stupid Mistakes with SAS I have made
One thing I like about our company a lot is none of our developers fit the stereotype of the 10x coder asshole. Don’t get me wrong, we have more than our share of people who are absolutely great at their jobs. What we don’t have is the arrogant attitude of:
What do you mean you don’t know how to integrate 3-D scenes from Unity with web pages? I knew how to do that when I was in the third grade!
First of all, I bet most of those people are liars and if you found their third-grade teacher she would say,
Oh, little Larry? I vaguely remember him. Wasn’t the brightest crayon in the box, now was he?
The fact is that all programmers make mistakes, including really dumb ones. As we get older, we may catch these before we hit the commit or run button, but, then again, maybe not.
Why is this code not working?
data fixdata ;
set fix1;
*** FIXES RECORDS WITH WRONG USERNAME ;
username = trim(username);
if username = "" then delete;
pos = index(username,'-') + 1;
username2 = substr(username,pos);
if upcase(index(username,'test)' > 0 then delete ;
Three mistakes with SAS in the code
I made all three of these mistakes lately (though not all the same day).
The first two SAS will catch for you if you read your log. Also, I cheated you here by not including the color coding that you’ll see in the SAS editor just to make it harder for you.
First of all, I have an unmatched parenthesis.
if upcase(index(username,'test)' ) > 0 then delete ;
This still doesn’t work because I have the closing quote in the wrong place.
if upcase(index(username,'test')) > 0 then delete ;
The hardest errors to find are when your code is running but still wrong
Now my log shows no errors but it’s still not working. I still have more users in the file than I should. The hardest kind of errors to find are logic errors. SAS will usually find the coding ones for you.
What I want to do is delete any usernames that have the word “test” in them, whether written as TEST, Test or test – or any other weird combination people might come up with, like TestAM or TestGarbanzoBeans12.
The problem here is that I used the UPCASE function after I had already searched for the value of “test” in the username. The INDEX function returns a number, which is the position at which the first character of the first occurrence of a string occurs.
NOTE: Numeric values have been converted to character values at the places given by: (Line):(Column). 74:12
Here is what I really need to do
if index(upcase(username),'TEST') > 0 then delete ;
I needed to change a few things. First, I need to put the UPCASE function inside the arguments passed to the INDEX function so that the first thing that happens is that username is set to upper case. Next, I need to change the string it’s matching to “TEST” since I set the username to uppercase. Now, finally, I pass that to an INDEX function and see if the string exists. If so, I delete the record.
This is a relatively simple bug to fix when presented like this. However, when it is hidden in hundreds of lines of code and all I know is that the number of records doesn’t match what I should have, it’s not so obvious, particularly when there are no errors showing in your SAS log. Did the username2 throw you off? Well, imagine hundreds more lines like that.
My points, and I do have more than one …
- Everyone makes mistakes, no matter how experienced they are.
- A bug fix may seem obvious after you have found it or if it’s been pointed out to you, but buried in hundreds or thousands of lines of code, it’s not so easy. An argument for modular development.
- READ THE NOTES! If you are deluded into thinking that if there is nothing in red in your SAS log all is good, get over that notion now.
- The enhanced editor is your friend. The incorrect quotation mark you could have picked up in the editor if you noticed that the closing parenthesis was in purple. In this particular case, it wasn’t so obvious because it was only off by one character. However, if you see a whole bunch of purple, meaning it is quoted, or green, for comments, it can be a tip right away that something is off.
One last thing – the SAS Enhanced Editor with color coding – I remember when it was brought up as a new, improved version of SAS and I laughed.
“Really? That’s what you’ve got? Color-coding? I mean, I’m sure it might help someone their first week on the job but we are all professionals here .. ha ha ha.”
Yeah, that was stupid, too. That enhanced editor has helped me catch so many bugs in code as I was writing it!
Random fact about me – I host a podcast
Yes, yes I do. It’s called More than Ordinary. This week’s co-host is Drew Kim who co-founded and runs an esports company with his mom. (Yes, you read that right.) Next week will be the continuation of a discussion of all things judo with Jason Harai of Ippon Dojo, in Washington. Basically, it’s all about having guests who are doing something out of the ordinary.