From the course: SAS® 9.4 Cert Prep: Part 03 Exploring and Validating Data

Demo: Filtering rows using macro variables - SAS Tutorial

From the course: SAS® 9.4 Cert Prep: Part 03 Exploring and Validating Data

Demo: Filtering rows using macro variables

- [Woman] In this program, we'll modify it to take advantage of macro variables, to make out program more flexible and dynamic. This program includes two steps. Proc print and proc means. And you'll notice we have the same where statement in both steps. And there are three different expressions within this where statement. Filtering based on MaxwinndMPH, Basin and StartDate. What if we'd like to change those values in order to look at different segments of our data? Well let's start by just running this program to make sure there are no syntax errors, and examine the results. Looks good. Based on the current where expression, we have rows with MaxWind greater than 156, Basin is NA, and StartDate is on or after January 1, 2000. Let's take a quick look at the Log. And we see the filter criteria returned eight observations for both procedures. What if we'd like to more easily modify the values included in the where statement? Back in the program, I'm going to add Percent Let statements to create macro variables, to store the text that would be used in the where statement. I'll start with %let, and create a macro variable called WindSpeed. And I'll store the text 156. My next macro variable, will be BasinCode. And we'll store the text capital NA. Remember, I don't need to quote that text string. And my last macro variable, will be Date. And I'll store the text string 01JAN2000. Once those macro variables are created, now I can replace the hard coded values in my where statement. I'll replace 156 with &WindSpeed. NA, &BasinCode, and instead of our date, &Date. Now remember, when we do reference macro variables in quotation marks, we must use double quotation marks. I'll take that same where statement, copy it, and replace it in proc means. Now I'll run the program to verify that we get the same eight rows return compared to when we ran the program the first time. The output certainly looks similar, and if I check the log, wee see once again, eight rows were returned. That works great. Now, back in the program, we can take advantage of our macro variables in order to look at different segments. I'll change the WindSpeed to 100, the BasinCode to South Pacific, or SP. And let's change the date to 2010. Even though these values are used in multiple where statements, I don't have to make any further changes. I run the program. I'll take a look at the log first. And we can see the WHERE statement includes the values that we provided in our Percent Let statements. And I can certainly confirm those values in my results. Basin SP, 2010 for our StartDate, WindSpeeds greater than 100. Looks great. Macro variables provide such an efficient way to make your programs dynamic and easy to modify.

Contents