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

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');

1 ACCEPTED SOLUTION

Accepted Solutions
Tom
Super User Tom
Super User

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

4 REPLIES 4
Reeza
Super User

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');
Astounding
PROC Star

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.

Tom
Super User Tom
Super User

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.

DavidPhillips2
Rhodochrosite | Level 12

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

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

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