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

Hi everyone,

 

I'm trying to create a dataset from scratch with datalines using to feed it some values contained in macrovariables that I want to define as arguments of a macro. I was first trying this code outside the macro:

 

%let study = aaa bbb;
%let protocol = ccc ddd;
%let cutoff = eee fff;
%let rationale = ggg hhh;

		data _auxsummary;
		   	input studyi $8. protocoli $11. cutoffi $8. rationalei $11.;
		    /* The RESOLVE function is used so that the ampersand within the */
  			/* value of text is treated as a macro variable.                 */
  			studyr=dequote(resolve(quote(studyi)));
			protocolr=dequote(resolve(quote(protocoli)));
			cutoffr=dequote(resolve(quote(cutoffi)));
			rationaler=dequote(resolve(quote(rationalei)));
		   datalines;
		&study &protocol &cutoff &rationale
		;

and it is working fine. &variable are the macrovariables, variablei are the input ones in the datalines data step and varaibler are the ones containing the resolved macrovariable value. You can check here the output data set:

 

Capture.PNG

 

In the next step I just want to report the resolved values.

 

If now I include this piece of code in a macro and execute it, it is not longer working.

 

%macro macro_test(
		study = 
		,protocol = 
		,cutoff =  
		,rationale = );

		data _auxsummary;
		   	input studyi $8. protocoli $11. cutoffi $8. rationalei $11.;
		    /* The RESOLVE function is used so that the ampersand within the */
  			/* value of text is treated as a macro variable.                 */
  			studyr=dequote(resolve(quote(studyi)));
			protocolr=dequote(resolve(quote(protocoli)));
			cutoffr=dequote(resolve(quote(cutoffi)));
			rationaler=dequote(resolve(quote(rationalei)));
		   datalines;
		&study &protocol &cutoff &rationale
		;

%mend;

%macro_test(
		study = aaa bbb
		,protocol = ccc ddd
		,cutoff = eee fff 
		,rationale = ggg hhh);

I get the following error:

 

MLOGIC(MACRO_TEST): Beginning execution.
 study = aaa bbb
 ,protocol = ccc ddd
 ,cutoff = eee fff
 ,rationale = ggg hhh);
MLOGIC(MACRO_TEST): Parameter STUDY has value aaa bbb
The SAS System

MLOGIC(MACRO_TEST): Parameter PROTOCOL has value ccc ddd
MLOGIC(MACRO_TEST): Parameter CUTOFF has value eee fff
MLOGIC(MACRO_TEST): Parameter RATIONALE has value ggg hhh
MPRINT(MACRO_TEST): data _auxsummary;
MPRINT(MACRO_TEST): input studyi $8. protocoli $11. cutoffi $8. rationalei $11.;
MPRINT(MACRO_TEST): studyr=dequote(resolve(quote(studyi)));
MPRINT(MACRO_TEST): protocolr=dequote(resolve(quote(protocoli)));
MPRINT(MACRO_TEST): cutoffr=dequote(resolve(quote(cutoffi)));
MPRINT(MACRO_TEST): rationaler=dequote(resolve(quote(rationalei)));
MPRINT(MACRO_TEST): datalines;

 

ERROR: The macro MACRO_TEST generated CARDS (data lines) for the DATA step, which could cause incorrect results. The DATA step and the macro will stop executing.

 

What am I doing wrong? Maybe I'm making easy things difficult, is there any other way to accomplish what I need?

 

Thanks a lot for any tips or suggestions!!

1 ACCEPTED SOLUTION

Accepted Solutions
Kurt_Bremser
Super User

Since this:

data _auxsummary;
		   	input studyi $8. protocoli $11. cutoffi $8. rationalei $11.;
		    /* The RESOLVE function is used so that the ampersand within the */
  			/* value of text is treated as a macro variable.                 */
  			studyr=dequote(resolve(quote(studyi)));
			protocolr=dequote(resolve(quote(protocoli)));
			cutoffr=dequote(resolve(quote(cutoffi)));
			rationaler=dequote(resolve(quote(rationalei)));
		   datalines;
		&study &protocol &cutoff &rationale
		;

is the equivalent of

data _auxsummary;
studyr = "&study";
protocolr = "&protocol";
cutoffr = "&cutoff";
rationaler = "&rationalei";
run;

it's just an unnecessary (and stupid) way of obfuscating code.

View solution in original post

5 REPLIES 5
RW9
Diamond | Level 26 RW9
Diamond | Level 26

Why would you need to create test data inside a macro in the first place?  Macro is not a replacement for Base SAS, it is an extra component for a specific purpose, only use it when it actually adds something to the code, which in this case it doesn't.

emera86
Quartz | Level 8

Hi @RW9,

 

The general purpose of the macro (this code that I'm showing is just a little piece) is to generate an extense report. At the beginning of this report I would like to add some general info related to the the specific project in table format. That is why I wanted to create it from some of the macro arguments. You are right, is not a clean way to do it (maybe you or other could suggest another way). I could create this table in advance and feed the macro with it, but I think that including these parameters as arguments of the macro will make it more easy to use for future users. What do you think?

 

Thanks for your comment anyway.

Kurt_Bremser
Super User

Since this:

data _auxsummary;
		   	input studyi $8. protocoli $11. cutoffi $8. rationalei $11.;
		    /* The RESOLVE function is used so that the ampersand within the */
  			/* value of text is treated as a macro variable.                 */
  			studyr=dequote(resolve(quote(studyi)));
			protocolr=dequote(resolve(quote(protocoli)));
			cutoffr=dequote(resolve(quote(cutoffi)));
			rationaler=dequote(resolve(quote(rationalei)));
		   datalines;
		&study &protocol &cutoff &rationale
		;

is the equivalent of

data _auxsummary;
studyr = "&study";
protocolr = "&protocol";
cutoffr = "&cutoff";
rationaler = "&rationalei";
run;

it's just an unnecessary (and stupid) way of obfuscating code.

emera86
Quartz | Level 8

You are totally right @Kurt_Bremser, as I was suspecting I was trying to solve a simple problem in a complex (and stupid) way.

 

Thanks a lot for your help!

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
  • 5 replies
  • 957 views
  • 2 likes
  • 4 in conversation