DATA Step, Macro, Functions and more

Global macro variables

Accepted Solution Solved
Reply
Super Contributor
Posts: 1,040
Accepted Solution

Global macro variables

Hi,

I was trying to create dataset names like shown below and get an error message

"OPEN CODE RECURSION DETECTED"

Could you help me resolve this issue??

Thanks

%let month3=JAN

%let month2=FEB

data &month3 &month2;

if conditions...then output "&month3";

if conditions...then output "&month2";

run;


Accepted Solutions
Solution
‎08-27-2013 10:55 AM
Super User
Posts: 17,928

Re: Global macro variables

Generally, that error means you have something wrong in your code, for example missing a semi colon or mismatched quotation marks.

Also, think of how that resolves:

%let month3=JAN

%let month2=FEB

data &month3 &month2;

if conditions...then output "&month3";

if conditions...then output "&month2";

run;

BECOMES

data JAN FEB;

if conditions...then output "JAN";

if conditions...then output "FEB";

run;

Hopefully you can easily see from that your macro variables should not be in quotation marks.

The best practice for what you're trying to do: DON"T DO IT.

If you ABSOLUTELY, really decide you want to go that way then see the best ways:

http://www.sascommunity.org/wiki/Split_Data_into_Subsets

View solution in original post


All Replies
Solution
‎08-27-2013 10:55 AM
Super User
Posts: 17,928

Re: Global macro variables

Generally, that error means you have something wrong in your code, for example missing a semi colon or mismatched quotation marks.

Also, think of how that resolves:

%let month3=JAN

%let month2=FEB

data &month3 &month2;

if conditions...then output "&month3";

if conditions...then output "&month2";

run;

BECOMES

data JAN FEB;

if conditions...then output "JAN";

if conditions...then output "FEB";

run;

Hopefully you can easily see from that your macro variables should not be in quotation marks.

The best practice for what you're trying to do: DON"T DO IT.

If you ABSOLUTELY, really decide you want to go that way then see the best ways:

http://www.sascommunity.org/wiki/Split_Data_into_Subsets

Super Contributor
Posts: 1,040

Re: Global macro variables

Hi,

Thanks for the quick response.

I was thinking that for a macro variable to be resolved I have to use  DOUBLE QUOTES always

So from your explanation it seems that when using macro variables for the data then it has to be in QUOTES

For example to pick all the records in JAN from TIME variable below then we need to use " quotes like "&start3"

Am i right??????

Thanks

%let month3=JAN

%let month2=FEB

data &month3 &month2;

"&start3"D <=datepart(TIME)<="&end3"D.then output &month3;

if conditions...then output &month2;

run;

Super User
Posts: 17,928

Re: Global macro variables

No you're not right.

See the documentation for examples of usage.

SAS(R) 9.2 Macro Language: Reference

Super Contributor
Posts: 1,040

Re: Global macro variables

Thanks for the help.

Super User
Super User
Posts: 6,502

Re: Global macro variables

If you want a macro variable to resolve INSIDE OF QUOTES then the outer quotes need to be dquote marks (") not single quote marks (').  If there are no quotes involved then resolution of a macro variable reference (or other macro statements) is not an issue.

Super Contributor
Posts: 1,040

Re: Global macro variables

Hi Tom.

Is it not that the INNER QUOTE HAS TO BE A DOUBLE QUOTE AND NOT THE OUTER!!!!!

example:

footnote1 'Quoting while resolving macro variable "&start3" in the single quotes';

               /\                                                              /\                                            /\

               |                                                               |inner                                     outer                                 

           outer Quote

Thanks

Super User
Super User
Posts: 6,502

Re: Global macro variables

Try it and let us know. :smileygrin:

Super Contributor
Posts: 1,040

Re: Global macro variables

OOPS ,

I am sorry.

That was an outer double quote with no need of quotes for the macro variables as highlighted!!!!!

line "Report Period from &start3 to &end3";

☑ This topic is solved.

Need further help from the community? Please ask a new question.

Discussion stats
  • 8 replies
  • 296 views
  • 6 likes
  • 3 in conversation