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.
BASUG is hosting free webinars Next up: Mark Keintz presenting History Carried Forward, Future Carried Back: Mixing Time Series of Differing Frequencies on May 8. Register now at the Boston Area SAS Users Group event page: 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.

BASUG is hosting free webinars Next up: Mark Keintz presenting History Carried Forward, Future Carried Back: Mixing Time Series of Differing Frequencies on May 8. Register now at the Boston Area SAS Users Group event page: 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-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!

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.

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