0
votes

I am looking to create a macro in SAS which will calculate the mean for an arbitrary number of numeric variables (variables are the parameter and are input by the user) from a data set. I only know how to input the variables one by one and am unsure on how to set it so any number of variables can be input when invoking the macro, thanks.

Right now I have the code

%macro meanStat/parmbuff;
%put Syspbuff contains: &syspbuff;
proc means data = sashelp.baseball mean;
var &syspbuff;

%mend meanStat;

%meanStat(hits, runs)`

And with this I get the error:

ERROR 22-322: Syntax error, expecting one of the following: a name, ;, /, ALL, CHARACTER, CHAR, NUMERIC.

I'm supposed to use "parmbuff" as well

2
Why must this be a macro? Why can't you use proc summary? Perhaps it would be easier to help you if you provided a dummy example of what your input and expected output look like.Therkel
If by variables you mean in the typical SAS sense (i.e. dataset columns) then you may want to look into array.Therkel
Ok I edited the question with some updated info/what I have now, if that helps. It does have to be a macro, it is for an assignment.J.M.M
How are the variable names being input ? Are they being validated ? If the assignment is homework that requires syspbuff I would say it's a bad problem. Run the code with OPTIONS MPRINT and the log will show you need to remove the parenthesis and commas from SYSPBUFF. You can use %SYSFUNC(COMPRESS(...)) to invoke the COMPRESS function (whose purpose is to remove characters from a value) on &SYSPBUFFRichard

2 Answers

0
votes

No idea why you'd do this. Best practice is to use SAS built in short cut lists.

You can reference variables as:

*indexed list;
VAR var1-var20

*With common prefix;
var var: ; *will select all variables that start with var;

*with general reference;
var _numeric_ ; *runs for all numeric variables;

If you want a macro though, as long as you pass a space delimited list there should be no issue.

%macro mean_useless(dsn=, var_list=);
  proc means data=&dsn;
  var &var_list;
  run;

%mend;

%mean_useless(dsn=sashelp.class, var_list = weight height age);
0
votes

You don't need to use /parmbuff option to enter a list of variable names. Just don't use commas in your list. Why would you want the commas anyway?

%macro meanStat(varlist);
proc means data = sashelp.baseball mean;
  var &varlist;
run;
%mend meanStat;

If you do want to use /parmbuff then look carefully at the value that is passed into the SYSPBUFF macro variable. It will include the () and the commas. For your problem you don't want either. Try this:

%macro meanStat/parmbuff;
%put Syspbuff contains: &syspbuff;
%put We want this: %sysfunc(translate(&syspbuff,%str( ),(,)));
%mend meanStat;