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-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

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