BookmarkSubscribeRSS Feed
MichaelvanStraten
Fluorite | Level 6

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

10 REPLIES 10
MichaelvanStraten
Fluorite | Level 6

Thanks for your fast reply. If you mean:

farmname='&farmname';

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

Quentin
Super User

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

The Boston Area SAS Users Group (BASUG) is hosting our in person SAS Blowout on Oct 18!
This full-day event in Cambridge, Mass features four presenters from SAS, presenting on a range of SAS 9 programming topics. Pre-registration by Oct 15 is required.
Full details and registration info at https://www.basug.org/events.
MichaelvanStraten
Fluorite | Level 6

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.

Astounding
PROC Star

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.

MichaelvanStraten
Fluorite | Level 6

Thanks Astounding.

data_null_;'s suggestion worked just fine.

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

Kurt_Bremser
Super User

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.

MichaelvanStraten
Fluorite | Level 6

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.

data_null__
Jade | Level 19

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');

MichaelvanStraten
Fluorite | Level 6

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

SAS Innovate 2025: Call for Content

Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!

Submit your idea!

How to Concatenate Values

Learn how use the CAT functions in SAS to join values from multiple variables into a single value.

Find more tutorials on the SAS Users YouTube channel.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 10 replies
  • 11529 views
  • 3 likes
  • 5 in conversation