Want to Learn SAS Macro Language? My Videos Will Help You Get Started!
Monika Wahi
Epidemiology & Biostatistics Consultant a/k/a Data Scientist | Exclusive and innovative solutions for data science challenges in public health, research and education
*This post contains links to educational resources for which I earn royalties if you actually purchase them.
I was reviewing trends in Google searches for SAS, and I found that a lot of people were looking for information on SAS macros. Therefore, I thought I’d make a few videos to help learners who are beginning to work with SAS macros.
SAS created a new online platform that is free to use called SAS OnDemand for Academics. It’s great for practicing your code! If you want help getting started using the platform, take my free course called “Getting Started with SAS ODA”.
In the videos below, I demonstrate code from my book, “Mastering SAS Programming for Data Warehousing”. In these videos, I demonstrate code from Chapter 8, where I talk about automating warehouse extract-transform-load (ETL) code with SAS macros.
What are SAS “Macros” and “Macro Variables”?
Macro variables in SAS are actually different than macros, but they tend to be incorporated in macro programming.
Actually, you can use a macro variable outside of a macro. So usually, the first thing you learn when approaching macro programming is how to make a simple macro variable outside of a macro, which is part of the steps to building a macro. Then, later, you learn how to build the macro, and then you learn how to combine using macro variables with macros together.
Four Steps to Making a Macro that Uses Macro Variables
Step 1. Make Base Code That Runs
This video shows me making some simple base code that does a data step and runs a frequency using PROC FREQ. The PROC FREQ has a WHERE criterion in it which could be changed each time it ran through using a macro variable.
Step 2: Add Macro Variables
In the next step of making a macro out of SAS code you already wrote, you have to choose what parts of the code you want to parameterize, and you turn those into macro variables. In this case, I parameterize the criterion on the WHERE clause in the PROC FREQ. I show a demonstration in this video.
Step 3. Make it Into a Macro with No Parameter Inputs
Now that we have our base code running well, and we figured out what macro variables we need, we can turn our base code into a macro. This video shows me converting the base code from Step 1 into a macro, and then launching the macro. But at this point, we still don’t take any parameter inputs through macro variables – we’ll add that in the next step.
Step 4: Parameterize the Macro with Macro Variables
Finally, in this video, we parameterize parts of the macro by adding macro variables (as we had imagined doing in Step 2). Now, the value of our macro variable needs to be declared whenever the macro is launched, or it won’t run. This allows us to reset the value of this variable each time we run the macro.
SAS Macros: The Big Picture
If you watch all the videos, you might begin to see through my simple examples how powerful SAS macro language can be for automation. I show just two functionalities – macros and macro variables. If you add the functionality of SAS arrays to the picture – which I cover in Chapter 6 of my SAS book – they can be made even more automated.
But the trick to any of this automation is to do it in baby steps, where you just change one little thing each step, and make sure the macro still works. This is why I emphasize that you only want to get into building macros in SAS if you really need to automate something, because the process of building them (as well as editing and troubleshooting them) is very time-consuming. However, if the gain in efficiency in automation is definitely there (as it is in data warehousing), I say – GO FOR IT!
Want to learn how to do research projects with SAS? Take Monika’s SAS courses on LinkedIn Learning.
Director - Big Data & Data Science & Department Head at IBM
1 年Are you a data pioneer in search of the SAS Certification treasure? Embark on your data expedition with www.analyticsexam.com/sas-certification practice exams. Blaze the data trails and become a data trailblazer! ???? #SAS #CertificationPioneer #DataExpedition #SuccessDiscovery ????
Senior Analyst - FFO Analytics at Novartis
3 年Thanks for sharing
Certified Sr. SAS Programmer (SP) | Statistical Programmer Enthusiast | Living, Learning & Growing | SASensei #1 AFRICA
3 年Hi Monika, great tutorial, keeps on posting more and more #sasprogramming
美国东北大学数据分析专业在读研究生。寻找2021年1月至6月的实习工作。
3 年Thank you so much Monika, recently I was thinking to learn more about SAS macro and you just posted this!
Epidemiology & Biostatistics Consultant a/k/a Data Scientist | Exclusive and innovative solutions for data science challenges in public health, research and education
3 年Thank you and FYI to my SAS learners from LinkedIn Learning: Thiru T Fatoumata Diallo abdul majeed fundie Dr. Maribel Soto - Handy LAVANYA GOPALAKRISHNAN SREEJITH SASIDHARAN narendra yaramati Ying Wang Ibrahim Saleh Al Mansoor