Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

removing single quotation marks from a string in SAS

Tags:

sas

sas-macro

I have a requirement to read the string with both single quotes and without quotes from a macro retrieve_context.

While calling the macro, users can call it with either single quotes or without quotes, like below:

%retrieve_context('american%s choice', work.phone_conv, '01OCT2015'd, '12OCT2015'd)
%retrieve_context(american%s choice, work.phone_conv, '01OCT2015'd, '12OCT2015'd)

How to read the first parameter in the macro without a single quote?

I tried %conv_quote = unquote(%str(&conv_quote)) but it did not work.

like image 381
Naga Vemprala Avatar asked Feb 09 '23 20:02

Naga Vemprala


2 Answers

You're running into one of those differences between macros and data step language.

In macros, there is a concept of "quoting", hence the %unquote macro function. This doesn't refer to traditional " or ' characters, though; macro quoting is a separate thing, with not really any quote characters [there are some sort-of-characters that are used in some contexts in this regard, but they're more like placeholders]. They come from functions like %str, %nrstr, and %quote, which tokenize certain things in a macro variable so that they don't get parsed before they're intended to be.

In most contexts, though, the macro language doesn't really pay attention to ' and " characters, except to identify a quoted string in certain parsing contexts where it's necessary to do so to make things work logically. Hence, %unquote doesn't do anything about quotation marks; they are simply treated as regular characters.

You need to, instead, call a data step function to remove them (or some other things, but all of them are more complicated, like using various combinations of %substr and %index). This is done using %sysfunc, like so:

%let newvar = %sysfunc(dequote(oldvar));

Dequote() is the data step function which performs largely the same function as %unquote, but for normal quotation characters (", '). Depending on your ultimate usage, you may need to do more than this; Tom covers several of these possibilities.

like image 120
Joe Avatar answered Feb 19 '23 09:02

Joe


If the users are supplying your macro with a value that may or may not include outer quotes then you can use the DEQUOTE() function to remove the quotes and then add them back where you need them. So if your macro is defined as having these parameters:

%macro retrieve_context(name,indata,start,stop);

Then if you want to use the value of NAME in a data step you could use:

name = dequote(symget('name'));

If you wanted to use the value to generate a WHERE clause then you could use the %SYSFUNC() macro function to call the DEQUOTE() function. So something like this:

where name = %sysfunc(quote(%qsysfunc(dequote(%superq(name)))))

If your users are literally passing in strings with % in place of single quotes then the first thing you should probably do is to replace the percents with single quotes. But make sure to keep the result macro quoted or else you might end up with unbalanced quotes.

%let name=%qsysfunc(translate(&name,"'","%"));
like image 22
Tom Avatar answered Feb 19 '23 08:02

Tom