DATA Step, Macro, Functions and more

Using double quotation marks in a macro variable

Reply
Occasional Contributor
Posts: 14

Using double quotation marks in a macro variable

Dear all,

Please forgive me if I use any incorrect terminology, most of my knowledge in SAS programing is a result of self study.

I have a string variable in a data set. The values of this variable are names and some of them include a single double quotation mark, e.g. farm a"bc.

I would like to assign these values to a macro variable I call farmname using the following line (within a data step):

farmname = "&farmname";

This data step is embedded in a macro that works iteratively.

The problem is, the double quotation mark (DQM) in the variable name is recognized as a closing DQM, and the macro stops running and reports it has encountered a very long string. I have changed the DQM in the names to single quotation marks (e.g. farm a'bc) and everything runs perfectly.

My question is this: is it possible to leave a single DQM in the names, and still use them as a macro variables?

Thanks in advance,

Michael

Super User
Posts: 7,761

Re: Using double quotation marks in a macro variable

Posted in reply to MichaelvanStraten

Since single quotes around strings prevent the substitution of macro variables, I think you will be safe.

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
Occasional Contributor
Posts: 14

Re: Using double quotation marks in a macro variable

Posted in reply to KurtBremser

Thanks for your fast reply. If you mean:

farmname='&farmname';

I tried this and the macro names don't resolve.

PROC Star
Posts: 1,322

Re: Using double quotation marks in a macro variable

Posted in reply to MichaelvanStraten

I'm confused by your question (but still, the answer is yes : )

The statement:

farmname="&farmname";

does not assign a values to a macro variable.  It assigns a value to a data step variable (after resolving &farmname).

To get the solo double-quote into a macro variable, you could use something like:

%let x=farm%str(%")abc;

data _null_;
  call symputx("y",'farm"abc');
  call symputx("z","farm""abc");
run;

To use the macro variable in a datastep assignment statement you could use:

data want;
  farmname1="&x";
  farmname2="%superq(y)";
  farmname3="%superq(z)";
run;

Luckily for you, Art Carpenter has a paper just on quote marks, highly recommended reading:

http://support.sas.com/resources/papers/proceedings15/2221-2015.pdf

Occasional Contributor
Posts: 14

Re: Using double quotation marks in a macro variable

Sorry for confusing you.. and thanks for the answer.

Indeed, I assigned the values to a macro variable using:

call symput ('farm',trim(farm)); in a separate data step.

Then I assigned a value to a data step variable with:

farmname="&farmname";

I have to digest your answer to understand it. Will it work if the names have different structures? E.g. one name includes double quote marks yet another does not? String length also varies.

Also, thanks for the link.

Super User
Posts: 5,497

Re: Using double quotation marks in a macro variable

Posted in reply to MichaelvanStraten

Perhaps this combination would be easier.  In a DATA step:

length farmname $ 50;

farmname = symget('farmname');

If you omit the LENGTH statement, FARMNAME would receive a length of $200 but the code would still work.

Occasional Contributor
Posts: 14

Re: Using double quotation marks in a macro variable

Posted in reply to Astounding

Thanks Astounding.

data_null_;'s suggestion worked just fine.

FarmName = %sysfunc(quote(%superq(farmname))); (Needed one more ")" Smiley Happy

Super User
Posts: 7,761

Re: Using double quotation marks in a macro variable

Posted in reply to MichaelvanStraten

What I meant is that, if you can substitute single quotation marks for doubles in your data (and macro vars), you can then safely use them inside double quotation marks when resolving the macrovars. As long as you use double quotation marks, the macrovar itself can contain any number of SQMs without conflicts.

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
Occasional Contributor
Posts: 14

Re: Using double quotation marks in a macro variable

Posted in reply to KurtBremser

Thank you, but yes, I know that and it worked. Problem is input data will always contain DQM and I want to avoid the need to constantly change them to SQM.

Respected Advisor
Posts: 3,799

Re: Using double quotation marks in a macro variable

Posted in reply to MichaelvanStraten

Use the QUOTE function and let SAS figure it out.

Change

farmname = "&farmname";


To:

FarmName = %sysfunc(quote(%superq(farmname)));


Note: The argument of SUPERQ is macro name not the resolved value.  In other words NO ampersand.


Better:

Retain FarmName %sysfunc(quote(%superq(farmname)));


Another way to avoid the unmatched quote issue is to use SYMGET but you want to be careful you don't execute SYMGET unnecessarily.


length FarmName $16;

retain farmname;

if _n_ eq 1 then symget('farmname');

Occasional Contributor
Posts: 14

Re: Using double quotation marks in a macro variable

Posted in reply to data_null__

Thanks a lot data_null_; I'll digest, then try.

Ask a Question
Discussion stats
  • 10 replies
  • 424 views
  • 3 likes
  • 5 in conversation