“Talk is cheap. Show me the code.”
― Linus Torvalds
In working on a program just today, I found myself typing the same eight names over and over again. Sometimes they were variables and sometimes they were file names, but I must have typed them dozens of times. I found my mind wandering towards what I would do in an OOP. I would have assigned all eight names to a variable as a list/array to start. Then I would have written functions or for loops depending on the specific use.
My final SAS program was an even 300 lines with comments and including one macro for importing the eight files. It would have added nearly 100 more lines without that particular macro. Knowing what the program did, I’m fairly certain that I could come to the same outcome in R with fewer than 100 lines of code. In fact, I think I’ll try it out just to see and report back next week to see how many lines it took. But I know that there are methods to reduce the code in SAS using macros and arrays.
Macros work kind of like a function in a OOP, at least in that they produce reusable bits of code. However, they are not quite as robust as functions. Macros seem to be more like a shortcut to cut/paste. I used the macro for file imports instead of cutting and pasting the Proc Import statement seven times and just changing the file name.
I think my challenge for myself over the next few weeks will be to learn more about macros and arrays so that I can become a more efficient programmer. I’m sure that I’m going to be reading a lot of error messages in the log file along the way. Of course that could be because I’m yet to remember to always end my lines with a ;. Old habits of just hitting return seem to die hard.