Search code examples
sassas-macro

SAS 9.3 passing a variable with spaces to a macro


I need to pass a variable that contains spaces to a macro. And use this variable to make some logic and to buid a new column inside the macro.

I've tried something like:

%MACRO func(var);
    if first.id then &var = 0;
    retain &var;
    if descr = %unquote(%str(%'&var%')) then &var = 1;
%MEND;


proc sort data=work.table5a;
    by id;
run;

data temp;
set work.table5a;
by id;
    %func(PLUTO)
    %func(PAPERINO)
    %func(BANDA BASSOTTI)
if last.id;
run;

ERROR is:

NOTE: Line generated by the macro variable "VAR". 37 BANDA BASSOTTI _____ 180 ERROR 180-322: Statement is not valid or it is used out of proper order.

If i comment %prova(BANDA BASSOTTI) it works. Any suggestions ?

thanks


Solution

  • You're using &var to create a variable name, and if you want to have a variable name with spaces in it, you need to use the variable name literals, e.g. "BANDA BASSOTTI"n. I haven't done this myself, seems like it makes the code uglier and harder to write, but something like this seems to work:

    options validvarname=any;
    
    %MACRO func(var);
        retain "&var"n;
        if first.id then "&var"n = 0;
        if descr = "&var" then "&var"n = 1;
    %MEND;