Search code examples
sqlsassas-macrolet

Is there a way to edit values in list after %let in SAS


I created a macro variable: %let hi = ('g', 'c', 'v', 'd'); I want to put this list in code but need to add %% for each value, looks like this('%g%', '%c%', '%v%', '%d%'). is there a way i can get the result?

I want to have %% because I want to use LIKE in sql.


Solution

  • The most straight forward way would be loop over the items in the list and build a new list. Dealing with % and commas is a pain in macro code. So perhaps it would be a lot easier to just do it in a data step.

    Example;

    %let list=('a','b','c');
    data _null_;
      length old new word $1000 ;
      old=symget('list');
      do i=1 to countw(old,'( ,)','q');
         word=quote(cats('%',dequote(scan(old,i,'( ,)','q')),'%'),"'");
         new=catx(',',new,word);
      end;
      call symputx('newlist',cats('(',new,')'));
    run;
    %put &newlist;
    

    Result

    379  %put &newlist;
    ('%a%','%b%','%c%')
    

    Note that a macro variable can be longer than a character variable in a data step. The maximum length in a variable is 32K (32,767) versus 64K for a macro variable.