%macro education(education, ORDER);
data education;
set history_education;
where education="&education";
run;
%mend
%education(University Bachelor's,8)
Above is my macro but the apostrophe in Bachelor's isn't allow me to run this statement. Is there anyway around this?
Use the %str function and "mask" the single unmatched apostrophe in the value.
The example code below writes the parameter to the log:
%macro dummy (parameter); %put the parameter is: ¶meter.; %mend; %dummy(simple string) %dummy(%str(text with problem%'s))
Note that you have very likely placed your SAS session into an unusable state and should save your code, shut down and restart SAS.
Try below. Untested.
%education(%nrstr(University Bachelor's),8)
Use the %str function and "mask" the single unmatched apostrophe in the value.
The example code below writes the parameter to the log:
%macro dummy (parameter); %put the parameter is: ¶meter.; %mend; %dummy(simple string) %dummy(%str(text with problem%'s))
Note that you have very likely placed your SAS session into an unusable state and should save your code, shut down and restart SAS.
Thank-you so much! This worked. And yes SAS session was in an unstable state and I had to save and reopen.
The caller needs to protect the characters that cause issues for the macro language. You will also have trouble with commas in the value of that parameter.
The easiest way to protect with macro quoting is the %BQUOTE() function as it will handle the unbalanced quotes.
%education(%bquote(University Bachelor's),8)
You could design your macro to assume the value is quoted already in the call. And then because the value is quoted in the call the extra single quote does not cause any issues.
%macro education(education, ORDER);
data education;
set history_education;
where education=&education;
run;
%mend ;
%education("University Bachelor's",8)
You could even make the macro smart enough to add quotes if they are not already there, but the bottom line is it is the macro callers responsibility to type a command that SAS can parse.
Registration is now open for SAS Innovate 2025 , our biggest and most exciting global event of the year! Join us in Orlando, FL, May 6-9.
Sign up by Dec. 31 to get the 2024 rate of just $495.
Register now!
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.
Ready to level-up your skills? Choose your own adventure.