Search code examples
sassas-macro

How to use a macrovariable as string?


I want to do the following request :

data _null_;    
  call symputx("test",findw(' x y z ','x'));    
run;

using a macrovariable instead of the ' x y z ' part, i.e.

data _null_;    
  call symputx("test",findw(&mv,'x'));    
run;

where mv is a macrovariable, whose value is calculated by a macro-function that I can't modify. Let's assume that this value is :

%let mv=x y z;

My problem is that if mv does not contain quotation marks, the code won't work :

ERROR 388-185: Expecting an arithmetic operator.

I tried to add them using %str (again, I can't modify the way mv is calculated so I have to use &mv):

%let mv2=%str(%') &mv %str(%');

and using %put, I can see that mv2 value is 'x y z' But I get a similar error as before (not quite the same):

ERROR 386-185: Expecting an arithmetic expression.

If y try with %let mv=' x y z ';, it works but I can't do such a thing as I have to use &mv (or can I ?).

I don't understand what's wrong, what should I do ?


Solution

  • Macro variables are just text fills. The macro variable needs to be a legal SAS value in the data step.

    %let mv=123;
    data _null_;
    x="&mv";
    put x=;
    run;
    

    For example. " (double quotes) resolve macro variables, ' (single quotes) do not.

    In your example, you can also do this without the symput.

    %let mv2 = %sysfunc(findw(&mv,x,,ios))
    

    Important details: SYSFUNC doesn't take quotes for any parameters (it would consider the quote a character, not to be quoting anything), and with findw specifically if it is space delimited you need a way to get space in that field as leaving a blank space doesn't work. FINDW has some specific issues also related to the fact that it has multiple structures with numeric and character optional arguments.