I want to convert the code from my SAS code to macro. Want to produce report either frequency or means based on a value of a parameter that is passed to the macro.Moreover then want to add a parameter to the macro that can control the variable which is used for the report. Can you suggest how to manage the macro code
There are many variables in dataset loantarget - like last name , first name, zip, DOB, income, LoanAmt etc
%macro loan(dataset,items);
proc means data=&dataset;
var &items;
%mend;
%loan(loantarget,loan);
Are you asking how to pass multiple variables? That would be something like:
%macro loan(dataset=,varlist=);
proc means data=&dataset;
var &varlist;
run;
%mend;
%loan (dataset=LoanTarget, varlist=loanamt income )
I recommend using keyword parameters with slightly more meaningful names. One big advantage of keywords you can include a default behavior. For instance with proc means you might want only Max and Min for most runs. So add a keyword Stats=MAX Min and have &stats on the Proc means statement. When you come to data set/ variable list you want other stats then you explicitly list them in the parameters but you wouldn't need to for just max and min.
When passing parameters this way they are space delimited. It is up to use to make sure that the variables are compatible with the useage (or write check code that will be a good learning experience). You should not pass text variables to Proc means as Var type variables for instance.
Note that I specifically included a Run; as part of the macro. Odd things have been known to happen when writing macros if you don't carefully structure them and with out the run you could run the macro as the last step of a program and wonder where your output went when it hadn't been created because a Run; had not been encountered by SAS to end the procedure.
You would have to be specific if you mean something more than multiple varaiables by "manage the macro code" or "add parameter to the macro" to do more than this. A good idea is to write the code without any macro as the most complex example you expect to need. Then look for the bits of code that you want to pass as a parameter.
You have your answer in the other post. What I would say is that if you are designing reusable code, then the level of detail you need to put in is far more than for a normal program. This code you create affects multiple programs possibly across whole companies depending on what it is used for. The reason I mentioned this is because I saw what you had posted:
%macro loan(dataset,items);
proc means data=&dataset;
var &items;
%mend;
%loan(loantarget,loan);
And you have fallen into quite a few pitfalls. Firstly and most importantly is the why. Proc means is well known Base SAS procedure which all SAS programmers know about. The question of why, is why are you hiding that well known bit of code in a macro, especially with a irrelevant name like loan() which has no bearing on the code? You will notice this when you join a new company and they have massive macro libraries dating back to the stone age and the programs look like:
%let %sysfunc(%amacro(&&&var_notdefined..%x___(&&qbc))));
(note thats just someting I typed here to illustrate), care to debug that. Me I just delete it now. So in your case, someone else comes into you role 5 years from now, not knowing what you know and they look at your code:
%loan(lastname,something else other);
%loan(firstname,something else other);
What does the above code do?
It is called obfuscation, and Macro is, in almost 99% of the time used in this manner. If its just one program, maybe fine, if its code used in 30 countries around the world by lots of users, not so much.
So take time in crafting well documented - 99.9% of programming is documentation - validated, worthwhile code. Use relateable names, use named parameters, indent code to make it readable, finish code blocks with run;. In this case I would just put the text of the proc means in your program, is it worth creating a debug nightmare just to save typing those few characters?
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 16. Read more here about why you should contribute and what is in it for you!
Learn how use the CAT functions in SAS to join values from multiple variables into a single value.
Find more tutorials on the SAS Users YouTube channel.
Select SAS Training centers are offering in-person courses. View upcoming courses for: