 Hi there, it's me, Monica Wahee, accompanying you as we reach out together to grasp a deeper understanding of SaaS on Demand for Academics, otherwise known as SaaS ODA. In this video, I'm going to show you how to automate some code I already wrote into a complex macro. Okay, here we are in the SaaS on Demand for Academics or SaaS ODA environment. If you need help setting up your free account in SaaS ODA, you can take my free online course that will help you, which is called Getting Started with SaaS ODA. Pretty logical. I'll link you to it in the description. Alright, so assuming you got started with SaaS ODA, you now know how to open a program window and put some code in it. So please, if you want to follow along, go get this code from GitHub and paste it into a program window. Alright, you can see there are two snippets of code here, a top snippet and a bottom snippet. I'm not really going to get into this top snippet of code. If you've watched my other SaaS videos, you can probably tell what's going on here. We are first using a data step to transform data set titled BRFSS underscore A into a new data set titled BRFSS underscore B. And as part of that transformation, we are adding a variable called asthma underscore flag, and we are basing the value of asthma underscore flag on the values of a native variable called asthma three. Then we are just running a two way frequency using proc freak between our native variable asthma three and our transform variable asthma underscore flag to make sure we didn't make a boo boo when transforming the variable. Oh, and all this talk of asthma, I should have formally introduced our data set. I'm demonstrating here with some data from the behavioral risk factor surveillance system or BRFSS, which is an annual health survey done by phone in the United States. So in the code, I'm taking the answer the respondent gave to the question about whether or not they have asthma, which has many different levels of answer for whatever reason. And I'm creating a flag variable called asthma underscore flag. I guess it makes more sense when you know what is going on in the background. But anyway, the whole point of this video is to automate that code with SAS macro programming. So now let's focus on the macro version of this upper code, which is down here. So this is what we are going over in this video. I admit SAS macro code looks pretty scary when you come upon it, but it won't bite. You can sit and look at it and diagnose it. Okay, so first we see the macro command and then we see the mend command. If you watch my videos on macros already, you know that this is a signal that the code sandwiched in between is our entire macro. And we see that the name of the macro is make underscore flag. And that is because that is the name I gave it in the macro command. Okay, but let's focus on the fact that when the macro is declared, there are a bunch of arguments and parentheses next to it. In fact, there are four of them. Each of these arguments has an equal sign next to it, meaning it wants you to input a parameter. Now you might remember that you can just have a simple macro that doesn't take any parameters. But then it's kind of a useless macro because you can't change the value of any of the macro variables in it. So it kind of defeats the purpose of automation. We want the macro to take in different values by way of macro variables. And if we want to do that, we have to declare the macro variables as arguments in the macro. So I made up the names native underscore var, flag underscore var, data underscore in and data underscore out, and you can kind of imagine what I'm going to use those for now that we talked about the above code. Now, as we diagnose this macro, we look for the next clue, which is that everywhere you see an amber sand preceding one of our macro variables, that's where we are asking SAS to fill in the variable with the value we set when we called the macro. Now above, we were making a flag for the asthma variable. The native variable was called asthma three, and the flag variable we named asthma underscore flag. So here is that code. What I'm doing is making the macro more flexible. Instead of the code always adding a flag for asthma, I can make it add flags for other diseases asked about in the BRFSS. The respondents were asked if they had a lot of different conditions, asthma, heart attack, heart disease, arthritis, kidney disease, makes you want to eat your vegetables. But all of the variables were coded the same annoying way, where one equals yes and two equals no, but there are a bunch of other values like seven for refused and nine for don't know. So we wanted to just make a flag variable for each of these conditions that says one if the respondents said yes to the condition and zero if they said no. And who wants to hard code all that ETL? So we are building in some flexibility and automation with our macro. Oh yeah, and up here are the two other variables I parameterized or macro variables. I named them data underscore in and data underscore out. Can you guess what I was talking about? Well, if you've read chapter four of my book Mastering SAS Programming for Data Warehousing, you already know that I'm a stickler for naming conventions. So what I do when I'm transforming data sets in SAS is that I increment the suffix letter each time I transform. That way I'm doing kind of like a manual rollback point, like I'm pretending I'm sequel. SAS is like a manual transmission. You have to use the clutch. SQL is more like an automatic transmission. So you have to learn a few SAS tricks or you will stall out. Anyway, this data underscore in and data underscore out allows me to name the input and output data sets. And then below the data step in the macro, we see the two-way proc free. OK, let's start by putting this macro in SAS's brain by highlighting macro to mend and running. OK, that went as well as we'd expect. Now let's go back to our code and see if the macro worked by calling it. OK, let's look at ourselves calling the macro below here. Here we call it the first time. We know we are calling it because it says percent and then it says make underscore flag, which is the name we call the macro. Then after that you'll see a parade of macro variables being set. First, I encourage you to run your macro with the values you used in your original code. That way you can see if the output is the same. And if it is, your macro is running right. So you will see I put in the original values I used in the code. Asma 3 for the native variable, asma underscore flag for the flag variable, and my data underscore in was BRFSS underscore A, and my data underscore out was BRFSS underscore B. Let's highlight and run this and see what happens. OK, this two-way frequency output looks good from a coding standpoint, like asma underscore flag has the right values in it based on asma 3. And the frequencies look familiar, but the best way to see if this ran the way we are thinking is to look at the log file. So let's click on it. OK, as I peruse the number of observations, number of variables, and names of data sets reported in the log file, I'm convinced the macro performed correctly. So now that we know that it works, let's go crazy calling it over and over. Sound fun? Let's go back to our code. Here is the next macro call. In this one, the native variable is named CVDINFR4. So obviously, that's the question about ever having had a heart attack. OK, it's not obvious, but this is how crappy some variables in SAS are named. It has to do with the fact that for much of SAS's life, you couldn't give variables long names like you can in SQL. OK, since you now know that CVDINFR4 means heart attack, you can see why I named the accompanying flag HA underscore flag. And since we are incrementing suffix characters for our rollback, data underscore in is now BRFS underscore B, and data underscore out is BRFSS underscore C. And then next in line, the daisy chain continues with native variable CVDCRHD4, which obviously stands for heart disease. And we've got the flag variable HDIS underscore flag set up to be created. And then data underscore in is BRFSS underscore C, and data underscore out is BRFSS underscore D. I'll spare you watching me run the code. You can do it yourself. But you can probably see that in real life, I might actually even automate this code with more code to automatically increment the names of the parameters being fed in. But again, macro programming is only for automation. If you are doing something one time, then don't use macros. Just save yourselves the trouble. Want to learn more about how SAS works? You can start by taking my free online course on how to use SAS on demand for academics. All these lessons are based on my explanatory yet very provocative book, Mastering SAS Programming for Data Warehousing. I'll link to these in the description. Thanks for watching, and have a blessed day.