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

%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?

1 ACCEPTED SOLUTION

Accepted Solutions
ballardw
Super User

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: &parameter.;
%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.

View solution in original post

4 REPLIES 4
ChanceTGardener
SAS Employee

Try below. Untested. 

 

%education(%nrstr(University Bachelor's),8)
ballardw
Super User

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: &parameter.;
%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.

mtee15
Calcite | Level 5

Thank-you so much! This worked. And yes SAS session was in an unstable state and I had to save and reopen.

Tom
Super User Tom
Super User

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.

 

SAS Innovate 2025: Register Now

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!

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

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