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');
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.
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');
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.
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.
Thanks, Tom your advice solved the issue I was running into.
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!
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.