SAS Tutorial | 3 Steps to Build a SAS Macro Program

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey i'm excited to talk to you today about creating sas macro programs so to begin imagine this scenario let's say you write a sas program and it works great but you need to run it fairly routinely and each time you run it you might need to change some values maybe in where statements or you might need to just run portions of the program conditionally depending on some criteria well you may be kind of the intelligence behind making those decisions and those edits what needs to be modified in the code and what should execute but wouldn't it be great if that could just happen automatically and really that's the beauty the elegance of the sas macro language it allows you to write code that rewrites itself and a sas macro program can have that kind of functionality where you can actually dynamically substitute values into your code or allow the syntax to determine what portion of the program should run or should not so in order to show you how to create a macro program i'll break it down into three steps the first step will be just creating a macro program and then calling it or executing it the second step will be using macro parameters to substitute values dynamically within your code and then the third step will be to execute code conditionally based on some criteria so let's go ahead and dive right in the first thing i want to show you is this table sas help cars it's a free sample table that comes with sas and we will look specifically at filtering based on drive train drivetrain has three values all front and rear and then msrp manufacturers suggested retail price is a currency value so in my program cars.sas we're using sashhelp.cars to create two different pieces of output a bar chart using sgplot and proc means to create some summary statistics you'll notice that my where statement is filtering based on drivetrain equal front and msrp less than or equal to thirty thousand and i have those values also in the title this program looks good but i'll go ahead and run it and there are the results so now i'll go back to my program and let's try step one of building a macro program the first thing i need to do is what i call create a macro sandwich at the beginning of the program i'm going to add a statement that's just percent macro and i'm going to name my macro cars report this will essentially be the name for this stored portion of code the other side of the macro sandwich is going to be at the conclusion where i'll close with percent mend or macro end and again i'll name the macro cars report i'm also going to add an option statement just to provide another more in-depth messages in the log to help us determine if our macro compiled and saved correctly so options and it's m compile note equal all all right i'll run my program so we see in the log this message that the macro cars report completed compilation without errors but there are no other notes that indicate that these two steps actually ran that's because the code didn't actually execute all we did was we created this macro definition named cars report there were no results produced this car's report macro is actually saved in the work library and will exist for the duration of my sas session so how do i actually execute this macro now if i come back to my program i'll add one line of code percent cars report and i don't need a semicolon at the end of that or any other information but that one what we call a macro call will then execute all of the code that's contained within my macro sandwich so i'll run that line and i get exactly the same results that were created before now i'm ready to move on to step two so what if instead of looking at front wheel drive cars under thirty thousand i wanna look at all-wheel drive cars under forty thousand i could certainly manually make changes within my macro definition recompile it and then call the macro but what if i want to make it really easy to update front in all three places in the program or the number 30 000 again in all three places well if you're familiar with macro variables we could certainly do this with a percent let statement that you might already be familiar with maybe i want to create a macro variable called dt for drivetrain equal and then store the value front or all or rear and then i could change my program to instead reference the macro variable ampersand dt and i could do that in all the appropriate places by the way if creating macro variables is a new topic for you i encourage you to take a look at the resources down below and we'll provide a link for a full video on creating macro variables so while we could use percent let statements here to dynamically substitute the values for front and max price instead i would like to actually build this in a little more efficiently into my macro program and this can be done with what we call macro parameters parameters come in parentheses after the macro name when we define the macro program so i will use what's called positional parameters first to create a macro variable named dt and another macro variable named max price then what i can do is reference those macro variables in the code so i've already substituted ampersand dt in all the right places i'll do the same thing for ampersand max price and copy and paste that in the three locations now these positional parameters suggest that the order matters the order is important so when i define my macro dt comes first in those parentheses and then max price comes second so when i call my macro card percent cars report what i do in parentheses is provide the corresponding values in the same order so the value for dt will do all and then the value for max price will be 40 000. so first thing i'll do is i'll go ahead and just recompile my macro and confirm in the log it compiled without errors and now when i come back to the program i can call the macro with my new values and you can see the title is updated the results are new and the log indicates those macro variables or those parameter values were substituted in the code there's another way that you can do parameters and that's with what we call keyword parameters with keyword parameters you list the macro variable name and then you can provide a default value so let's say i want the default value to be front and i want the default value of max price to be 30 000 what we originally started with with keyword parameters when you call the macro then you also have to put the macro variable name dt equal and then the value and the same i'll do for max price equal 40 000. now with keyword parameters order doesn't matter so i actually could reverse and put max price first and dt second that's perfectly fine but the other neat thing with keyword parameters is that i don't have to necessarily provide a value for every single parameter when i call the macro if i leave off the second parameter for max price when i call the macro that's perfectly fine we established a default value in the macro program itself and so that's what will be used in the code so i'll recompile my macro first looks good and then i'll call the macro with only a value assigned to dt and notice we have all-wheel drive cars but we're using the default value of max price as thirty thousand now we're ready to move on to step three conditional processing within our macro so let's start with this little scenario here where i'll say i'll use max price equal and i put a value of ten thousand dollars well if i run my macro program you'll notice there's no results produced there are no errors in the log but i do see notes that there were zero observations read well the value that i provided for max price there are no cars that are below ten thousand dollars so it didn't exactly generate what i was hoping what if i could build my program in a way where if i provide an invalid parameter value i want the log to notify me with a helpful message that indicates what a valid parameter value could be well i can do that with some conditional processing you may already be accustomed to if then processing in the data step when we use if then processing in the data step it's usually based on a data value if a column is a certain value then execute some other statement and that happens for every single row that we process in the input table now with if then conditional processing in a macro program we're not building our conditions or executing statements based on data values we're actually executing code conditionally or parts of our program conditionally based on whatever criteria we set up so what i'm going to do is right after my initial macro statement i'm going to add a percent if statement percent if and i want to look at the value of max price if max price is less than whatever the minimum value is for msrp which i happen to know is 10 280. so if that parameter value is less than that minimum percent then percent due so that syntax like likely looks familiar to data step if then syntax with the one change there's a percent sign in front of each of those keywords i'm sure you've noticed by now that that percent sign is a macro trigger it's part of the macro facility or the macro language that indicates we're looking at statements that are modifying our code really so if this condition is met if our parameter value is invalid i want to do two things the first thing i want to do is i want to write a message to the log a custom error message and i can do that with another macro statement percent put if i start my message with error all in capital letters it's going to look like an error with the red coloring and everything so error max price must be and i'll just do greater than or equal to 10 280. the next thing that i want to happen after that custom error message is i want macro execution to halt to just stop right here and i can do that with percent return so i want those two macro statements to execute if i provide an invalid value for max price i'll close that block with percent end now if max price is a valid value in other words if it's greater than or equal to 10 280 then i want the rest of my program to run so i can do that with percent else percent due include all of the rest of the program and just before my percent men statement i'll close that percent else percent due block with percent end so with those two conditions in place let's go ahead and try it again first thing i'll do is compile my macro to verify that it runs success or i should say that it compiles successfully that's a good sign now i'll come back to my code and i'll call the macro with an invalid value for max price there's my custom error message and you can see the log doesn't have anything else in there nothing else ran let's test the alternative which is a valid value for max price so i'll change it to 30 and call the macro i don't need to recompile the macro again that only needs to happen once but i can call the cars report macro max price is valid results are generated and if i take a look at the log i can see obviously both steps ran successfully so i hope these three steps were enough to get you started and intrigued to learn more about creating your own sas macro programs if you want to learn more i encourage you to look down below with the resources section we'll provide several links to some additional information that may help get you deeper into what's possible also if you have any questions or anything you'd like to share please use the comment section as well we love getting your input and hope that you can participate in this discussion and finally if you want to learn more some tips and tricks from other instructors like me about macro or anything else in the sas language please subscribe we hope that you'll join us in your educational journey to learning more about sas [Music]
Info
Channel: SAS Users
Views: 8,870
Rating: 4.9819002 out of 5
Keywords: sas tutorial, 3 steps to build a sas macro program, build a sas macro, build a sas macro program, sas macro, sas macro program, sas how to tutorial, sas macro language, using macro parameters, using macro parameters in sas, calling a macro program, sas, executing code conditionally in sas, calling a macro program in sas, calling a sas macro program
Id: Fe_Efkl3enM
Channel Id: undefined
Length: 14min 24sec (864 seconds)
Published: Mon Aug 24 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.