# Why does LaTeX replace some spaces with brackets or underscores in the listings environment?

I'm using LaTeX on Windows with MikTeX and I'm seeing some odd behaviour. In the output, some of the spaces are replaced with bracket-like underscores. This is the code:

\documentclass[10pt]{article}
\usepackage{listings}
\begin{document}
\begin{lstlisting}[language=SAS, title=SAS code to format data on government patients]
code code;
code code;

code;
code;
code;
c
identity = "Subject's identity"
variable = "Subject's name in government archive of files";
FORMAT gender genderft.
code
response responseft.;
RUN;
\end{lstlisting}
\end{document}


This generates the following output:

I've replaced all tabs with spaces, copied the code into a plain text editor and retyped all the spaces, and still the problem persists. Even copying the code into the editor from my browser after the question is posted doesn't work. The bracket/underscores are still there.

No other code snippets in my document present this problem, yet they also use identical listings syntax to this example and also use SAS code.

-
it's the  '  in Subject's that is the problem, remove that and it goes away. Presumably a bug in the language parser but I suppose you could enable TeX escaping and then hide the apostrophe in a macro –  David Carlisle Oct 15 '12 at 15:20

The text between the two apostrophes (the one in the identity = "Subject' line and the apostrophe in the following line) is being interpreted as a string. Passing the option showstringspaces=false to the environment renders the desired output, namely

\begin{lstlisting}[showstringspaces=false,language=SAS, title=SAS code to
format data on government patients, frame=single]


A cleaner solution would be to mark the quotes as string delimiters.

This could be accomplished by adding the line \lstdefinelanguage{SAS}{morestring=[b]"} in the preamble. The [b] allows quotes inside strings by preceding them by a backslash. Note that the showstringspaces=false option is still required.

-
"A cleaner solution would be to mark the quotes as string delimiters." - How would I do this? –  Ricardo Altamirano Oct 15 '12 at 15:18
Well, adding the line \lstdefinelanguage{SAS}{morestring=[b]"} in the preamble seems to do the trick. The [b] is to allow quotes inside a string by preceding them by a . You still need to explicitly state showstringspaces=false, so it is a longer solution after all! –  guillem Oct 15 '12 at 15:37
Either solution works, though, so your answer is still extremely helpful! –  Ricardo Altamirano Oct 15 '12 at 16:23
Oops! the previous comment got messed up. I was saying that quotes are allowed inside strings if they are preceded by a backslash (that's the meaning of the b in morestring=[b]" according to the manual). I'm glad it helped! –  guillem Oct 15 '12 at 16:27