BookmarkSubscribeRSS Feed
surajmetha55
Fluorite | Level 6

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.

 

 

5 REPLIES 5
Reeza
Super User
Remove the comma's from the list - they're not needed either in this use case.

%cardataset(carlst="Audi" "BMW" "Ford")
ballardw
Super User

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).

 

rudfaden3
Calcite | Level 5

Try

%cardataset(carlst=%nrstr("Audi","BMW","Ford"))	
yabwon
Onyx | Level 15

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

_______________
Polish SAS Users Group: www.polsug.com and communities.sas.com/polsug

"SAS Packages: the way to share" at SGF2020 Proceedings (the latest version), GitHub Repository, and YouTube Video.
Hands-on-Workshop: "Share your code with SAS Packages"
"My First SAS Package: A How-To" at SGF2021 Proceedings

SAS Ballot Ideas: one: SPF in SAS, two, and three
SAS Documentation



Tom
Super User Tom
Super User

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 { } ).

hackathon24-white-horiz.png

The 2025 SAS Hackathon Kicks Off on June 11!

Watch the live Hackathon Kickoff to get all the essential information about the SAS Hackathon—including how to join, how to participate, and expert tips for success.

YouTube LinkedIn

How to Concatenate Values

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 5 replies
  • 1538 views
  • 3 likes
  • 6 in conversation