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
Since single quotes around strings prevent the substitution of macro variables, I think you will be safe.
Thanks for your fast reply. If you mean:
farmname='&farmname';
I tried this and the macro names don't resolve.
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
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.
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.
Thanks Astounding.
data_null_;'s suggestion worked just fine.
FarmName = %sysfunc(quote(%superq(farmname))); (Needed one more ")"
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.
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.
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');
Thanks a lot data_null_; I'll digest, then try.
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!
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.