 Hello, thank you for allowing me, Monica Wahee, to coach you as you move along your pathway to knowledge about SAS on-demand for academics, otherwise known as SAS ODA. In this video, I'm going to show you how to code a bunch of flag variables in SAS ODA by using an array. 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 titled 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 let's say someone gives you a SAS dataset. Actually, let's say I give you a SAS dataset and tell you to get it into SAS ODA. Actually, if you don't know how to get this dataset into SAS ODA, I have a video I'll link you to in the description. But go out to GitHub and get this dataset and put it in. Actually, you'll see I kept some code from the previous video at the top, this lib name and data step code here. That way you can see how I read in the CHAP6 underscore 1 dataset from the folder map 2x with a lib name. And I put it in the work directory and I named it brfss underscore a. Now we will talk about the code we want to talk about, which is below. As you can see, this is actually just a huge data step. This video is about data stepping. I said it was about using an array to make flag variables. So we are making variables and that is using a data step, but this is a big data step. Let's look at it in three parts. We knew that this was a data step because we saw data blah set blah up here and run down here. Only we are not exactly saying data blah set blah. We are saying data example because we want to output a dataset named example into the work directory and set brfss underscore a because we want to input that dataset into the data step. Essentially, we are reading in brfss underscore a, adding a bunch of flag variables to it with our data step code and then reading it out as the dataset example. See these two chunks of code? The top one contains our arrays. The bottom one is a do loop that uses the arrays. Let's start by just looking at the array coding. See how these start? The first one says array comorbid underscore native underscore list. And the second one says array comorbid underscore flag underscore list. So the reason why we are saying comorbid is because brfss is a dataset from a health survey and we are trying to create binary flags if the respondent has certain comorbidities like asthma. These two names with the word list at the end, these are what we are naming our arrays. Someone taught me to name arrays with the suffix underscore list in SAS. So I can make sure every time I see something that looks like a named variable, if it has underscore list on it, I know I am looking at an array. Kind of like naming tables with the suffix underscore TBL in SQL, so you can easily tell them from views by their name. Now that you know why I chose comorbid and why I have the term list in these array names, you can probably guess what these two arrays are about. The first array comorbid underscore native underscore list is the list of native variables in the brfs data set that I am going to use as criteria for setting the flags. So the first one, asthma3, that has the answer to a question about whether the respondent has asthma or not. It is coded one for yes, two for no, and there are some other values like nine for don't know and seven for refused. And I think there are also some blanks. The first variable that is listed in the second array below in the flag list, asthma3 underscore F, that variable doesn't exist yet. That's the new flag variable we are going to make. That variable will be a cleaned up version of asthma3 where it just says zero for every value except where the respondent said yes, they have asthma, in which case we'll set it to one. That's going to be our binary flag for asthma. But as you can see, there are a whole bunch of different comorbidities in our array that we are going to make flakes for. Okay, now let's look at our do loop below the arrays. So the first line says do I equals one to dim and then in parentheses comorbid underscore native underscore list. What this does is create a variable called I, and this variable is initially set to one. Then in each round of the do loop, SAS will update this I to the next value of the iteration. The reason why the boundary is dim and then in parentheses comorbid underscore native underscore list is because we are telling SAS to go through the loop as many times as we have variables in that array. So I think we have 11 variables in that array. I forgot the count. But if I'm right, this code basically tells SAS to do this I loop 11 times. Okay, so here's where the data transformation starts. Now remember, as I say in my book and all my videos, you have to make sure this code works outside of the array first with a basic data step. Basically, if we ran this for asthma three, we'd say if asthma three equals one, then make asthma three underscore f equal one, or else make asthma three underscore f equals zero. That's an excellent. See all these curly brackets with the letter I in them. That is how we tell SAS, which member of the array to pull out for that processing. So when I equals one, meaning the first round of the loop, comorbid underscore native underscore list curly brackets I will evaluate to asthma three and comorbid underscore flag underscore list curly brackets I will evaluate to asthma three underscore f. And then you see the end of the loop, and it comes around again, and then I equals to the next time. And we do the next two variables and there are rays native variable CV D I N F R for and flag variable, whatever that was with a suffix underscore f. So what we should have when we are done is a new data set named example in the work directory that has the native comorbidity variables and 11 additional flags. Let's run this data step and see what happens. Okay, SAS put us over here in the output data pane. Look here on the left. We have our native variables. Let's scroll down. Okay, this all looks good. All of these variables look like what they are supposed to look like. Variables with underscore f at the end. Oh, wait, what's this? Yes, you guessed it. It's I. It's our iteration variable. And if we look at it, it would say 11 in every record, the value of the last iteration. I know what you're thinking. Is this like a kludge? No, it's not a kludge or better put, all of data step language is an elaborate kludge. So don't be surprised to see a lot of data step language where do loop processing is followed by a command drop I. And why I? You can actually use any letter you want. But you will see that SAS conventions are that you use I for your first loop. And if you have a loop within that, you use J for that one. And if you have another nested loop, it's K. Hey, don't drop your eyes. Look up instead. Maybe you only have eyes for SAS. If you do, especially if you are studying in the public health field, it would behoove you to take my free online course in how to use SAS on demand for academics. All these lessons are based on my engaging book, Mastering SAS Programming for Data Warehousing. I'll link to these in the description. Thanks for watching and have a pleasant day.