 Hi there, I'm grateful that you allow me, Monica Wahee, to escort you down the path to mastery of SAS On Demand for Academics, otherwise known as SAS ODA. In this video, I'm going to show you how to use a macro variable in SAS ODA. 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 with a title that says it all, getting started with SAS ODA. I'll link you to it in the description. Okay, so assume you got started with SAS ODA and now you can open a program window and copy and paste code into it. So go on to GitHub and get this code and paste it into your program window. Now, this video is focused on how you use a macro variable in SAS. And we are going to do it in the context of a proc freak. So first you need to make sure you can get this data set into SAS. If you have trouble with that, you can watch my video on it. And you need to be familiar with how to run proc freak with criteria in order to understand my macro variable example. So if you aren't, please watch my video on proc freak with where criteria. Okay, all this code up here, everything above the let statement. This we all ran before in my previous video. Remember, it was a proc freak, which is a procedure that does frequencies. We ran a one way frequency on the diabetes three variable, which is the question about diabetes. Remember, we are using data from the BRFSS, which is a US health survey. So people said all kinds of answers to the diabetes question, like, yes, I have pre diabetes and yes, I had diabetes during pregnancy. It's apparently not a simple yes, no question. Anyway, this code up here ran just fine and gave us the one way frequency of diabetes three for the records in the data set from Florida, which is what is meant by underscore state equals 12. That filtered the data set by Florida records only. So what we are going to do now is to make this code more flexible. You might remember from our other videos that we have three states in here, 12, 25 and 27. So I might want to choose 25 or 27 instead of 12. What if I wanted to do that? Well, we could become more flexible by setting what's called a macro variable in SAS. So this code down here is us redoing the code above, but adding the flexibility of the macro variable. First, we are using the let statement. Notice how it has a percent sign before it, which usually is a signal you are doing something with macro programming in SAS. It says let selected underscore state equal 12. What's important here is that you understand that the term selected underscore state is actually the macro variable. This is the variable that can be set to anything, although in this simple case we are setting it to 12. So now we created a macro variable called selected underscore state and we set it to the value of 12. And we did that all with the let statement. Okay, so this is the code that comes after the let statement. I want you to compare it to the code above and tell me what's different. Okay, here's the first difference. Instead of saying where underscore state equals 12, like it does in the above code, in this code it calls out the macro variable. Instead, it says where underscore state equals ampersand selected underscore state. The key here is that ampersand, that and sign. See, with the let command, we use the percent to tell SAS we were setting a macro variable. Now we use the ampersand to tell SAS to plug in the value of that macro variable we named selected underscore state. And if this code listens to our let statement, it will set selected underscore state to 12. And we have another difference here. Above for the title, we lazily called the output procfreak with Florida only. Now that we are using macro variables, we need a more flexible title. So now I retitled it procfreak with underscore state equals and then I called up our macro variable. You'll see that I needed two and signs, two ampersands, because this title is part of text. So SAS will fill that in with the value 12, and then the whole title should say procfreak with underscore state equals 12 only. Okay, let's just run this bottom code, the let statement and the procfreak using the macro variable, and see what happens. Okay, well, it listened to our macro variable in terms of the title. See here, it filled it in with the value 12. But let's look at the log file. See, if you watch my video where we hard coded in the 12, you'll realize the log file looked identical. And that's what you want. You want to first hard code a variable, then rewrite your code adding the macro variable and see if it runs the same way. See, now we can test it. Let's go back to our code. Remember how I said the other two states in this data set are 25 and 27? Let's change the selected state to 25. Okay, there, haha. Now let's highlight and rerun this code and see if SAS listens to our change in the macro variable value. Look at that. We have evidence in the title that SAS listened to our change in macro variable value. See how it says underscore state equals 25 now? Let's click on the log file. See that in the log file SAS acknowledged the where clause evaluated to 25. More evidence that SAS listened to us when we changed the value of the macro variable we set, which is good. I like it when SAS listens to me. If you want SAS to listen to you too, I suggest you take my free online course on how to use SAS on demand for academics. All these lessons are based on my accessible yet academic book, Mastering SAS Programming for Data Warehousing. I'll link to these in the description. Thanks for watching and have an inspired day.