BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
tpage
Fluorite | Level 6

Sorry if the title is confusing. I am trying to create a macro routine for two datasets, SASUSER.MOVIES and SASUSER.ACTORS. Below is the code that I've created:

 

%LET DSN = SASUSER.MOVIES ;
%LET DSN2 = SASUSER.ACTORS
%LET VAR = TITLE ;
%LET CLASS_VAR = Rating ;
%MACRO stats ;
proc means data=&DSN,&DSN2 maxdec=0 ;
var &VAR ;
class &CLASS_VAR ;
run ;
%MEND stats;

%stats ;

 

Also, when I tried to run it, the log produces 6 error messages: 

 

ERROR: Open code statement recursion detected.
76 %LET CLASS_VAR = Rating ;
ERROR: Open code statement recursion detected.
77 %MACRO stats ;
ERROR: Open code statement recursion detected.
78 proc means data=&DSN,&DSN2 maxdec=0 ;
79 var &VAR ;
79 var &VAR ;
___
180
ERROR 180-322: Statement is not valid or it is used out of proper order.
 
 
80 class &CLASS_VAR ;
80 class &CLASS_VAR ;
_____
180
ERROR 180-322: Statement is not valid or it is used out of proper order.
 
 
ERROR: No matching %MACRO statement for this %MEND statement.
81 run ;
82 %MEND stats ;
83 %stats ;
 
How can I fixed this code so that it can run 2 different DSN's and allow me to work with the TITLE column/variable? Thanks in advance.

 

 

1 ACCEPTED SOLUTION

Accepted Solutions
r_behata
Barite | Level 11

Try this Modified Code :

 

%LET DSN = SASUSER.MOVIES ;
%LET DSN2 = SASUSER.ACTORS;
%LET VAR = TITLE ;
%LET CLASS_VAR = Rating ;

%MACRO stats(dsname);
	proc means data=&dsname maxdec=0;
		var &VAR;
		class &CLASS_VAR;
	run;

%MEND stats;

%stats(&DSN)
%stats(&DSN2)

Issues Identified in your code :

 

1. Statement not ended with a semi-colon :

%LET DSN2 = SASUSER.ACTORS

The above has resulted in the error :

 

ERROR: Open code statement recursion detected.

 

2.  Invalid Syntax :

proc means data=&DSN,&DSN2 

You cannot pass multiple dataset names to the proc at the same time ,the syntax is invalid.  You can parameterize the macro and call it twice , each time with a different dataset name in this fashion :

 

%MACRO stats(dsname);
....
...
%Mend;

%stats(&DSN) %stats(&DSN2)

 

View solution in original post

4 REPLIES 4
unison
Lapis Lazuli | Level 10

Just on a quick glance, you need a semicolon here:

 

%LET DSN2 = SASUSER.ACTORS;

-unison

-unison
Reeza
Super User

You cannot pass two data sets to proc means in this fashion. You need to loop it through the data sets. Or call the macro once for each data set. The macro appendix has examples of loop and the tutorial has examples on calling it multiple times. 


Tutorial on converting a working program to a macro

This method is pretty robust and helps prevent errors and makes it much easier to debug your code. Obviously biased, because I wrote it 🙂 https://github.com/statgeek/SAS-Tutorials/blob/master/Turning%20a%20program%20into%20a%20macro.md

Examples of common macro usage

https://communities.sas.com/t5/SAS-Communities-Library/SAS-9-4-Macro-Language-Reference-Has-a-New-Ap...

 


@tpage wrote:

Sorry if the title is confusing. I am trying to create a macro routine for two datasets, SASUSER.MOVIES and SASUSER.ACTORS. Below is the code that I've created:

 

%LET DSN = SASUSER.MOVIES ;
%LET DSN2 = SASUSER.ACTORS
%LET VAR = TITLE ;
%LET CLASS_VAR = Rating ;
%MACRO stats ;
proc means data=&DSN,&DSN2 maxdec=0 ;
var &VAR ;
class &CLASS_VAR ;
run ;
%MEND stats;

%stats ;

 

Also, when I tried to run it, the log produces 6 error messages: 

 

ERROR: Open code statement recursion detected.
76 %LET CLASS_VAR = Rating ;
ERROR: Open code statement recursion detected.
77 %MACRO stats ;
ERROR: Open code statement recursion detected.
78 proc means data=&DSN,&DSN2 maxdec=0 ;
79 var &VAR ;
79 var &VAR ;
___
180
ERROR 180-322: Statement is not valid or it is used out of proper order.
 
 
80 class &CLASS_VAR ;
80 class &CLASS_VAR ;
_____
180
ERROR 180-322: Statement is not valid or it is used out of proper order.
 
 
ERROR: No matching %MACRO statement for this %MEND statement.
81 run ;
82 %MEND stats ;
83 %stats ;
 
How can I fixed this code so that it can run 2 different DSN's and allow me to work with the TITLE column/variable? Thanks in advance.

 

 


 

r_behata
Barite | Level 11

Try this Modified Code :

 

%LET DSN = SASUSER.MOVIES ;
%LET DSN2 = SASUSER.ACTORS;
%LET VAR = TITLE ;
%LET CLASS_VAR = Rating ;

%MACRO stats(dsname);
	proc means data=&dsname maxdec=0;
		var &VAR;
		class &CLASS_VAR;
	run;

%MEND stats;

%stats(&DSN)
%stats(&DSN2)

Issues Identified in your code :

 

1. Statement not ended with a semi-colon :

%LET DSN2 = SASUSER.ACTORS

The above has resulted in the error :

 

ERROR: Open code statement recursion detected.

 

2.  Invalid Syntax :

proc means data=&DSN,&DSN2 

You cannot pass multiple dataset names to the proc at the same time ,the syntax is invalid.  You can parameterize the macro and call it twice , each time with a different dataset name in this fashion :

 

%MACRO stats(dsname);
....
...
%Mend;

%stats(&DSN) %stats(&DSN2)

 

PaigeMiller
Diamond | Level 26

An extremely important concept in writing macros is that they must produce legal valid working SAS code after the macro variables are resolved.

 

proc means data=&DSN,&DSN2 maxdec=0 ;

resolves to

 

proc means data=sasuser.movies,sasuser.actors maxdec=0 ;

which is not legal valid working SAS code. You can't have DATA= followed by the names of two SAS data sets, and you can't have DATA= with a comma in it.

--
Paige Miller

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

Register now!

SAS Enterprise Guide vs. SAS Studio

What’s the difference between SAS Enterprise Guide and SAS Studio? How are they similar? Just ask SAS’ Danny Modlin.

Find more tutorials on the SAS Users YouTube channel.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 4 replies
  • 800 views
  • 6 likes
  • 5 in conversation