 Hi there, thank you for letting me, Monica Wahi, usher you along the poorly lit path to wisdom with respect to SAS On Demand for Academics, otherwise known as SAS ODA. In this video, I'm going to show you how to build some proc-free code into probably the simplest macro in the world. Okay, here we are in the SAS On Demand for Academics, or SAS ODA environment. If you need help setting up your free account in SAS ODA, you can take my free online course called Getting Started with SAS ODA, which is a pretty sensible name. I'll link you to it in the description. Okay, so assuming you got started with SAS ODA, you now know how to open a program window and copy and paste code into it. So please go to GitHub and get the code I made for you, open a program window and paste the code in. Now before we get started, if you don't know what a macro variable is, you better watch my previous video where we went over setting a macro variable in SAS, because we are going to use that macro variable approach here when we build our macro in this video. Now I just want to be clear, macros and macro variables are not the same thing. You can have a macro variable and not have a macro. That's what my previous video goes over. Or you can use a macro, but it might not deploy any macro variables, although that's not what we are doing here. Here we are making a simple macro that does use a macro variable, which is why I did that macro variable in a different video. So we could just concentrate on the macro in this video. So if you did watch my video on setting a macro variable, then you already know about this code here. This is the exact same code we used. See how we used the let command to set the macro variable selected underscore state to the value of 12? Oh, let's remind ourselves of our data set. This data set is from the BRFSS, which is a health survey done by phone annually in the U.S. So they keep track of what state each respondent is from. And I included only three states in this exercise data set, which are the states with codes 12, 25, and 27. Just in case you were curious, 12 stands for Florida. But anyway, in the last video, we set the macro variable we named selected underscore state to 12 in the let statement, then proceeded with our proc freak code to run a frequency on the diabetes 3 variable. Here's the variable where the respondent says whether or not they have diabetes. Next, you'll see there's a where statement that uses the macro variable. The where statement filters the results by just the Florida records. So if this code runs, we will get to see the frequency distribution of the values in diabetes 3 just for where the state equals 12 or Florida, because that's what we set selected underscore state to in the let command. Oh, and then we call up the value of 12, our macro variable selected underscore state again in the title command. So it will print that we are running this for state equals 12 on the output. That way, we won't get mixed up. We will know what state is being represented on the output. So let's turn this code snippet into a macro. What's a macro? Briefly described, it's a snippet of code in SAS that can be automated. So macros are really not used a lot when you do research projects. That's because you are doing something experimental and innovative, and those things aren't usually automated. But if you're running a data warehouse, you are doing the same thing all the time, extract, transform, load, extract, transform, load. So in the situation where you have a data warehouse, you have a great case for devoting the time to build your well-formed SAS code into macros. But if you are not running a data warehouse or otherwise doing a bunch of repetitive tasks that need to be automated, I suggest you just totally steer clear of macro language. It's complex, messy, hard to write, hard to troubleshoot if it bombs, and hard to fix if you want to revise it. And more importantly, you have to start by writing good SAS code anyway. So for one-time projects, it's probably just good enough to get your code to run. So getting back to our macro programming, all we have to do to turn the snippet of code into a macro is to add the macro statement at the top, before the code snippet, and add the men statement at the end after the snippet. You'll notice that in the macro statement, I gave our macro a name. I named it state underscore freak one. And I said it again in the men statement, men state underscore freak one. But technically, you can just put men, and SAS figures out that you are ending the macro you started most recently. So that's all you do to make a macro out of existing code. You put a macro statement at the beginning, name the macro, and then put the men at the end. I forgot to mention this line of code. This is new to you. This sets global options in SAS. If you want to learn more about global options, please read chapter eight of my book, Mastering SAS Programming for Data Warehousing, where I explain the details. For now, I'll just say I added this line because SAS sometimes misbehaves when you call up macros if you don't turn off certain global options. So that's what this line does. It turns off a bunch of global options. Okay, now when we run this macro code, meaning the code between and inclusive of the statements macro and men, SAS will learn the macro. SAS will put the macro in its brain. And if we want to launch the macro, we will have to tell SAS to run it. And we name that macro state underscore freak one. So if we launch that macro, SAS will know what to do. But that's only after we run the macro. So let's highlight all this code between macro and men and run the macro. Okay, so you saw that it opened the log file for us. And there's no real feedback in the log file. It just has black writing. Meaning no green or blue or red messages from SAS. And also, it just repeats back our code for us. So we don't really know if the macro works or not at this point. All we know is that SAS knows about it and has it in its memory. So let's go back to our code and try to actually launch the macro and see how it does with our data. Theoretically, it should run because the code ran just fine in my last video when it wasn't in a macro. Okay, see this code here? It has a percent sign. Then says state underscore freak one. Remember how I said in an earlier video that a percent sign in SAS is usually a signal that you are doing some sort of macro programming? Well, here it is the symbol you use before the name of the macro to launch the macro. So let's highlight this one line of code and run it. And see if it actually successfully launches the macro we just made. Hey, look, it ran. Now, if you watch my previous videos where we carefully built this code, you know that we already know what this output is supposed to look like. So if I launch the macro and got different numbers here, then when I hard coded this as regular code, I can tell that something is evaluating wrong with my macro. And there are more tools in your toolbox to troubleshoot macros. I write about them in chapter nine of my book, where I talk about how to debug your code in SAS. But for now, I have to say we successfully revised our code from just regular code into a named and launchable macro in SAS. Good times. And if you want to keep having good times in SAS, I recommend you take my free online course on how to use SAS on demand for academics. All these lessons are based on my cyclopedic, yet plain spoken book, Mastering SAS Programming for Data Warehousing. I'll link to these in the description. Thanks for watching, and have an exciting day.