BookmarkSubscribeRSS Feed
wzl9527
Calcite | Level 5

found issue about resolving macro, here is the macro:

 

data test;
 set sashelp.class;
run;

data test2;
 set sashelp.cars;
run;

%macro resolvema(name=,out=);
	data cc_&out;
		set test(where=(name="&name"))   
		%if &out=1 %then %do;test2%end;;
	run;

%mend;

%resolvema(name=Alfred,out=1);
%resolvema(name=Thomas,out=2);

data final;
	set cc_:;
run;

after I resolved the macro , the space between 'test(where=(name="Alfred"))test2;' was removed,which cause an error,Does anyone know how to fix this,below is the resolve macro

%let path_1=D:\Practice;
%let path_2=D:\Practice2;

%macro resopa(name=);
/*  options nomprint nomfile;*/

  filename mprint "&path_2\&name..sas";     
  options mprint mfile;                    
  run;
  %include "&path_1\&name..sas";   

      
  
%mend resopa;

%resopa(name=remacro);

or Does anyone else know of other ways to resolve program(inclue macro),

 

9 REPLIES 9
PaigeMiller
Diamond | Level 26

I don't get any errors in the log when I run your first block of code. What errors do you see? (Hint: in the future, don't say you are getting an error — show us the error in the log that you are getting)

 

If the lack of a space bothers you, put a space in the macro code

 

%if &out=1 %then %do; test2%end;;

 

 

Your second block of code is unrelated to the first, and so I am not even sure what the question is about the 2nd block of code.

--
Paige Miller
wzl9527
Calcite | Level 5

set test%if &out=1 %then %do;test2%end;

actuall if code like above,after resoled,the log will show testtest2 dataset doesn't exit,but I find out that I need to add a space between %do; and test2,after resolved,the code will keep the space

ballardw
Super User

@wzl9527 wrote:

 

after I resolved the macro , the space between 'test(where=(name="Alfred"))test2;' was removed,which cause an error,Does anyone know how to fix this,below is the resolve macro


What space? Between what?  The quoted string you show, 'test(where=(name="Alfred"))test2;' , does not contain any space to remove.

 

I also ran your code for the first macro with no error.

 

A comment on coding style inside macros. It may help to separate the code out a bit more so you can see the part adding the second, or subsequent, data set name(s) and where the Set statement is closed.

%macro resolvema(name=,out=);
	data cc_&out;
	   set test(where=(name="&name"))   
   	      %if &out=1 %then %do;
                test2
             %end;
          ;  /* close set statement*/
	run;

%mend;

It is very easy to lose track of why some, simple here but that %if could well become a complex loop, semicolon is doubled and accidentally clean it up or confuse where an expansion of this %if to a loop needs to end.

 

 

 

wzl9527
Calcite | Level 5
set test%if &out=1 %then %do;test2%end; actuall if code like above,after resoled,the log will show testtest2 dataset doesn't exit,but I find out that I need to add a space between %do; and test2,after resolved,the code will keep the space
PaigeMiller
Diamond | Level 26

Please show us the log. Talking about the log is rather pointless.

--
Paige Miller
Quentin
Super User

Can you post an example and log that show the error?

 

In my head, I think this code, without the parentheses, should generate an error:

 


options mprint;

data test;
 set sashelp.class;
run;

data test2;
 set sashelp.cars;
run;

%macro resolvema(name=,out=);
	data cc_&out;
		set test%if &out=1 %then %do;test2%end;;
 run;
%mend;

%resolvema(name=Alfred,out=1);

Interestingly, MPRINT shows that the generated code has no space between test and test2, so the code does resolve to set test1test2; which should error. 

 

But due to some oddities in the tokenizer / word scanner, this actually builds separate tokens for test and test2.  The log is:

 

MPRINT(RESOLVEMA):   data cc_1;
MPRINT(RESOLVEMA):   set testtest2;
MPRINT(RESOLVEMA):   run;

NOTE: There were 19 observations read from the data set WORK.TEST.
NOTE: There were 428 observations read from the data set WORK.TEST2.
NOTE: The data set WORK.CC_1 has 447 observations and 19 variables.
The Boston Area SAS Users Group is hosting free webinars!
Next up: Joe Madden & Joseph Henry present Putting Power into the Hands of the Programmer with SAS Viya Workbench on Wednesday Nov 6.
Register now at https://www.basug.org/events.
Quentin
Super User

Your macros run fine for me, no errors.

Also, when debugging a macro, it's often helpful to test the SAS code with no macro.

 

When I submit:

data cc_1;
  set test(where=(name="Alfred"))test2;
run;

It runs fine, no complaint about lack of space after the closing parenthesis.

The Boston Area SAS Users Group is hosting free webinars!
Next up: Joe Madden & Joseph Henry present Putting Power into the Hands of the Programmer with SAS Viya Workbench on Wednesday Nov 6.
Register now at https://www.basug.org/events.
wzl9527
Calcite | Level 5
thanks ,i find out the answer
ballardw
Super User

@wzl9527 wrote:
thanks ,i find out the answer

Please share what you discovered so if someone comes to the forum with what they think is a similar issue they can see what the resolution could be.

SAS Innovate 2025: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

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
  • 9 replies
  • 939 views
  • 1 like
  • 4 in conversation