SAS Programming

DATA Step, Macro, Functions and more
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 is hosting free webinars!
Next up: Troy Martin Hughes presents Calling Open-Source Python Functions within SAS PROC FCMP: A Google Maps API Geocoding Adventure on Wednesday April 23.
Register now 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-white.png

Special offer for SAS Communities members

Save $250 on SAS Innovate and get a free advance copy of the new SAS For Dummies book! Use the code "SASforDummies" to register. Don't miss out, May 6-9, in Orlando, Florida.

 

View the full agenda.

Register now!

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

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