3

As is known to all, SAS needs special care to quotation marks inside a sentence.

E.g.

%let quoted="I'd like to";
data temp;
    set temp;
    quoted="&quoted";
run;

error is encounterred when submitting.

In fact I need to copy data to one dataset from another one, in which there are a lot of records containing quotation marks. When assigning, error occurrs and data step stop executing, causing rest of the code to be invalid. So in this case, it's impossible to modify original data set by adding duplicated quotation marks, which doesn't make sense.

So instead of having to add a duplicated one, like, "I''d like to", is there any other way of avoiding the error, or making data step keeping executing?

Thanks,

mj023119
  • 675
  • 5
  • 12
  • 19
  • You'll need to post your code. If there are quotation marks in text variables there is no issue "copying data from one dataset to another" because their values are never exposed in code. – sasfrog May 10 '11 at 09:58

2 Answers2

5

When using the macro language (including the %let command) you do not want to use quotes to identify text strings. To place a single quote in a string you must use one of the macro utility masking functions such as %str(). The correct syntax to place a single unmatched quote in a macro variable using %let is shown below. The % symbol before the single quote is an escape character to tell SAS that the following character (a single quote) should be used as a literal. Also note that I've removed the double quotes from the %let as they are not required.

%let quoted=%str(I%'d like to);
data temp;    
    quoted="&quoted";
run;

Cheers Rob

Robert Penridge
  • 8,424
  • 2
  • 34
  • 55
  • 1
    You could also put the whole string you want to assign to the let statement within a %str function like: %let quoted=%str(I%'d like to); This way you can escape each quote with a percent without adding another %str function. – Laurent de Walick May 11 '11 at 05:49
  • Thank You @Rob, and @Laurent as usual. Your answers do reminded me of concept of masking.:) I hope I could find the exact answer by myself Cheers – mj023119 May 12 '11 at 05:23
  • Is there a way to do this when you're setting one macro variable equal to another macro variable that may or may not include a single quote (i.e. if you're looping over a list in which some strings contain a single quote and some don't)? – Todd Burus May 29 '20 at 18:58
  • @ToddBurus Yes, there are other macro quoting functions other than `%str()` that exist for the purpose you describe. Off the top of my head I don't remember which is the right one but I'm sure if you post a new question one of the people better at macro quoting than myself will be able to respond pretty quickly. – Robert Penridge Jun 01 '20 at 19:01
  • 1
    @RobertPenridge Thanks. Yes, I was able to find that `%superq()` got the job done. – Todd Burus Jun 02 '20 at 04:09
2

I'm not sure what you're trying to achieve in the actual situation, but in the above situation it can be solved removing the double quotation marks in the data step.

%let quoted="I'd like to";
data temp;
    set temp;
    quoted=&quoted;
run;
Laurent de Walick
  • 2,154
  • 14
  • 11