 Hi there. Thanks for joining me, Monika Wahee, as we glide down the path to knowledge together, exploring SAS on-demand for academics, otherwise known as SAS ODA. In this video, I'm going to show you how to add a binary flag variable to a dataset using a data step 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 that will help you, which is called Getting Started with SAS ODA. Pretty logical. I'll link you to it in the description. Okay, assuming you got started with SAS ODA, you can open a program window and copy and paste SAS code into it. So go on to GitHub and go copy this code and put it in your program window. The links to the code and the dataset are in the description. So all we are doing in this code is adding a variable to our existing dataset using a data step. Remember, we are working with data from the Behavioral Risk Factor Surveillance System, or BRFSS, which is an annual phone survey about health topics we do in the US. So in a previous video, we read in the dataset and named it BRFSS underscore A. I'll link you to the video where we did that in the description so you can follow along. Now what we're doing in this video is transforming the dataset BRFSS underscore A into a new dataset we will call BRFSS underscore B. But as we do that, we are adding a new variable based on the values of one of the variables already in BRFSS underscore A. Then we are running a two-way frequency on the old variable and the new variable to check if the new variable got recorded right. Don't worry if you feel overwhelmed by my description. I'll demonstrate all this for you. Okay, let's start unpacking this code. Remember classic data step commands? We see them right here. I always say data Y set X because that reminds me that the data command declares the name of the output dataset and the set command declares the name of the input dataset. You'll see I'm using naming conventions to increment the suffix character from A to B. Oh, and by the way, if you are running a SAS data warehouse or you are just learning how to code and want to practice running a data warehouse in your code exercises, you will want to use my handy dandy naming convention system for code files, datasets, and variables. Just read chapter four of my book Mastering SAS Programming for Data Warehousing and I'll hook you up. Okay, we went over the data and set parts of the data step. If you've watched my other videos, you'll know that everything between the set statement and the run statement are instructions we are giving to SAS on how to edit our dataset. That's this code. So what are we asking SAS to do to BRFSS underscore A to transform it into BRFSS underscore B? Well, as you can see in the first line, we are adding a variable called asthma underscore flag. So what is an asthma flag? Well, like other flags, it's a binary variable, meaning it can take on a value of either one or zero. And in the BRFSS, they asked respondents if they had been diagnosed with asthma and the respondents got to say yes and no, but they also got to say refused and unknown and don't know when blank. So what this asthma underscore flag variable does for us is we set it to one where the respondent actually said yes to the asthma question. And we keep it zero for those who said any other thing. Now, because I'm a manager, I like to do this maneuver in two steps. First, I make a variable called asthma underscore flag, and I set it to zero for everyone. Then I take the next pass and I have SAS evaluate the variable with the original answer from the respondent, which is asthma three. If asthma three equals one, it means they said yes to the asthma question. So in that case, I have SAS go back and set the asthma underscore flag to one. Okay, so let me issue a warning here. This is what I call manager's code. It's easy to read and easy to troubleshoot, but not that efficient in terms of IO. If you have already taken my free course and getting started in SAS ODA, you would have learned about the program data vector or PDV. I won't spoil it for you, but because of the PDV, it's way more efficient if you are using big data, if you make SAS edit a variable all in one pass by using an else statement. However, when we are learning and building our code, it's best to try simpler and clumsier commands until we are sure our code runs. Then we can rebuild it into more efficient code later. Okay, before we run this code, let me alert you to the fact that before we even put the run statement, we sneak in a proc freak, which will run a two-way frequency on our variables. If you aren't familiar with proc freak, don't worry, I'll link you to a video in the description so you can get up to speed. So what's going on here? Why did we do a two-way proc freak? You can see we used the tables command on the variables asm underscore flag and asma three, which includes the new flag variable and the original native asma three variable. What we are looking for is any botches in our recode. We know we want all the people who said one or yes to asma three to also have a one in the asma flag variable and any other values of asma three like two or nine or blank or whatever. We want to see those coded to zero. I want to call attention to the options I set on this tables command. If you saw my earlier videos, you know I set missing because I want sass to put any missings in their own category in the frequency distribution. Sass doesn't automatically do that. You have to tell it to do that or it just forgets about the missing. Then you go crazy because your total number of observations doesn't match what you know from the proc freak output. But what I really want to tell you about in this video is the list option. Those of you familiar with proc freak output know that if you don't set this option, you get this really busy output that's hard to read. Let me see if I can show you an example. Okay, this is a sass white paper by Richard Severino about proc freak titled proc freak. It's more than counts. I'll link you to it in the description. But see the box under output 12? That's a default output of proc freak. See how impossible it is to decode? Well, the list option fixes all of that. I'll show you. Okay, I've explained this all, so let's run all this code and see what happens with transforming brfs s underscore a into brfs s underscore b and adding our new asthma underscore flag variable. Okay, see how different this output looks from that white paper I showed you? It's actually a list. See how it shows the value of the flag variable in a column, then the value of the native variable in the next column. That way you can make sure you did your recode right. In fact, it is exactly right because where asthma underscore flag is one, asthma three is also one. And then all the values of asthma three are set to zero. So okay, this code got us what we wanted. This is when we might edit the code and try to add the else statement to make the code more efficient. The most important thing, though, is that you always run a two way proc freak after you recode a variable, where you include both the new variable and the native variable in the frequency. Otherwise, you aren't checking your recode and you could have a botched recode and not know it. Want to learn good programming habits in SAS? 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 extensive yet very readable book, Mastering SAS Programming for Data Warehousing. I'll link to these in the description. Thanks for watching and have a productive day.