I want to pass multiple value to one macro parameter.
%macro cardataset (carlst=);
data new;
set sashelp.cars;
if make in(&carlst);
run;
%mend cardataset;
%cardataset(carlst="Audi","BMW","Ford")
When I am trying to run above macro, I get following error
Statement is not valid or it is used out of proper order. ERROR: All positional parameters must precede keyword parameters.
Try
%cardataset(carlst="Audi" "BMW" "Ford")
The IN operator in a data step has not needed a comma between values since SAS 9.3 (or maybe 9.2, it has been a while ago anyway).
The comma is the delimiter between macro parameters.
When you used
%cardataset(carlst="Audi","BMW","Ford")
Then your carlst value was only "Audi" because of the comma. "BMW" and "Ford" were considered to be positional parameters that were 1) not defined in the macro definition and 2) since they did not use the Keyword parameter such as Parm="BMW" were considered positional (which order they appear in the call assigns the value). Positional parameters, since they do rely on the order, must be defined before the keyword parameters. One reason for this requirement is that Keyword parameters, such as Carlst= could have no assigned value (which is useful if it normally has a default that you only override sometimes).
Try
%cardataset(carlst=%nrstr("Audi","BMW","Ford"))
if you need commas in "carlist" chnge the macro to:
%macro cardataset (carlst=());
data new;
set sashelp.cars;
if make in &carlst; /* no brackets*/
run;
%mend cardataset;
%cardataset(carlst=("Audi","BMW","Ford")) /* brackets will mask commas */
other option would be to use PARMBUFF and SYSPBUFF(https://documentation.sas.com/doc/en/pgmsascdc/9.4_3.5/mcrolref/n0dwee153ed7b2n1wl471mgtczle.htm) in macro definition:
%macro cardataset () / PARMBUFF;
data new;
set sashelp.cars;
if make in &syspbuff.;
run;
%mend cardataset;
%cardataset("Audi","BMW","Ford")
Bart
No need to modify the macro.
1559 1560 %cardataset(carlst=("Audi","BMW","Ford")) MPRINT(CARDATASET): data new; MPRINT(CARDATASET): set sashelp.cars; MPRINT(CARDATASET): if make in(("Audi","BMW","Ford")); MPRINT(CARDATASET): run; NOTE: There were 428 observations read from the data set SASHELP.CARS. NOTE: The data set WORK.NEW has 62 observations and 15 variables. NOTE: DATA statement used (Total process time): real time 0.08 seconds cpu time 0.04 seconds
IN operator does not seem to mind extra random parentheses (although it would complain if actually included brackets like [ ] or { } ).
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.
Ready to level-up your skills? Choose your own adventure.