05-06-2015 05:02 AM
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,
05-06-2015 05:10 AM
Since single quotes around strings prevent the substitution of macro variables, I think you will be safe.
05-06-2015 05:56 AM
I'm confused by your question (but still, the answer is yes : )
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:
05-06-2015 06:22 AM
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:
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.
05-06-2015 09:08 AM
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.
05-06-2015 06:16 AM
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.
05-06-2015 06:25 AM
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.
05-06-2015 06:44 AM
Use the QUOTE function and let SAS figure it out.
farmname = "&farmname";
FarmName = %sysfunc(quote(%superq(farmname)));
Note: The argument of SUPERQ is macro name not the resolved value. In other words NO ampersand.
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;
if _n_ eq 1 then symget('farmname');