Search code examples
sassas-macro

SAS : Concatenate single quote with a value returned from %BQUOTE in a where clause


I am retrieving a few records from PROC SQL using a where clause like this:

where mailing_phone_numbers in (%bquote(&phone_numbers.)); 

But the macro variable phone_numbers does not have a initial "'" single quotation mark and it is creating problem.

How could I append a single quote before (%bquote(&phone_numbers.), so that the query does not fail?

Macro variable holds values like this: 2101172076', '2101172077', '2101172078', '2101172079', '2101172080', '2101172081' First single quote is missing in the data. As I am reading it from a csv file.


Solution

  • You can add the single quote inside the %bquote(), but you need to %unquote() the result to let PROC SQL recognize the quotes.

    where mailing_phone_numbers in (%unquote(%bquote('&phone_numbers.)))
    

    Sounds like the real problem is the CSV file. SAS will not have any trouble reading a string in a CSV file that starts with a single quote, but Excel might interpret the leading single quotes as a formatting signal. This could result in it writing a CSV file that does not include the leading single quote of a value. Perhaps you can remove Excel from the data processing stream? If the quotes are in the CSV file then show the SAS code that you are using to read it that removes the leading single quote.