DATA Step, Macro, Functions and more

SAS Macro Variables with Apostrophes.

Accepted Solution Solved
Reply
Highlighted
Valued Guide
Posts: 531
Accepted Solution

SAS Macro Variables with Apostrophes.

SAS macro variables with apostrophes.

I’m missing something simple in the below code that is causing the macro to not have the same result as feeding in the string ‘2016’ please advise.

 

/*works
proc sql;
select strip(nliteral(name)) into: var_list separated by " "
from TestImportCols3
where year2 ='2016' or name = 'Institution Name';
quit;

 

/*does not have the same effect as '2016'
%macro CreateYears(yearVar, yearVarString);

/*create a list of just 2016 variables so we can assign a 2016 year to them.*/
proc sql;
select strip(nliteral(name)) into: var_list separated by " "
from TestImportCols3
where year2 =&yearVarString or name = 'Institution Name';
quit;
%mend;
%CreateYears(2016, '2016');


Accepted Solutions
Solution
a week ago
Super User
Super User
Posts: 7,401

Re: SAS Macro Variables with Apostrophes.

Posted in reply to DavidPhillips2

When you reference a macro variable the value just replaces the reference and the resulting text is interpreted by SAS as if you had typed it that way in the original code.

 

So if you set macro variable YEAR to 2016 then that is what appears in the code. If you set it to '2016' then that is what appears in the code.  There is a big difference between how SAS will interpret these two statements:

where year=2016
where year='2016'

In the first 2016 is numeric literal. In the second '2016' is character literal.

If you want to use the value of a macro variable to generate a character literal then you need to enclose it in double quote characters.

where year="&year"

If you try to enclose it in single quotes instead then the macro processor will not be called to evaluate the macro variable reference and your character literal will include the ampersand and the macro variable name instead of their being replaced by the value of the amcro variable.

View solution in original post


All Replies
Super User
Posts: 21,546

Re: SAS Macro Variables with Apostrophes.

Posted in reply to DavidPhillips2

Why do that in the first place? Place the quotes in the macro code.

 

%macro CreateYears(yearVar, yearVarString);
/*create a list of just 2016 variables so we can assign a 2016 year to them.*/
proc sql;
select strip(nliteral(name)) into: var_list separated by " "
from TestImportCols3
where year2 ="&yearVar" or name = 'Institution Name';
quit;
%mend;
%CreateYears(2016, '2016');
Super User
Posts: 6,006

Re: SAS Macro Variables with Apostrophes.

Posted in reply to DavidPhillips2

Your code looks fine, with only one possible reason for this result.  By any chance, are you intending this to be a comment statement in the actual code?

 

/*works

 

That statement actually comments out the entire code until a matching */ is found, which in this program appears here:

 

...them.*/

 

As @Reeza points out, you only really need one macro variable, not two.  But I suspect you have two because you were playing around with the code to see if you could get something to work (or perhaps because you simplified the sample code that you posted).  But the comment is valid that you only need a macro variable with the value of 2016 and you can add double quotes later if needed.

Solution
a week ago
Super User
Super User
Posts: 7,401

Re: SAS Macro Variables with Apostrophes.

Posted in reply to DavidPhillips2

When you reference a macro variable the value just replaces the reference and the resulting text is interpreted by SAS as if you had typed it that way in the original code.

 

So if you set macro variable YEAR to 2016 then that is what appears in the code. If you set it to '2016' then that is what appears in the code.  There is a big difference between how SAS will interpret these two statements:

where year=2016
where year='2016'

In the first 2016 is numeric literal. In the second '2016' is character literal.

If you want to use the value of a macro variable to generate a character literal then you need to enclose it in double quote characters.

where year="&year"

If you try to enclose it in single quotes instead then the macro processor will not be called to evaluate the macro variable reference and your character literal will include the ampersand and the macro variable name instead of their being replaced by the value of the amcro variable.

Valued Guide
Posts: 531

Re: SAS Macro Variables with Apostrophes.

Thanks, Tom your advice solved the issue I was running into.

☑ This topic is solved.

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

Discussion stats
  • 4 replies
  • 109 views
  • 2 likes
  • 4 in conversation