Search code examples
regexreplacesasenterprise-guide

Using REGEX to create duplicates in find / replace window of an


I have a string of words separated by tabs.

lets say I want to surround each word with brackets and add "sum"

so I use the find/replace function to replace \t with )\t, sum(.

so from

A B C D E

I get

sum(A), sum(B), sum(C), sum(D), sum(E)

But what if I want to duplicate the words.

i.e.

go from:

A B C D E

to

sum(A) as A, sum(B) as B, sum(C) as C, sum(D) as D, sum(E) as E ?

https://learn.microsoft.com/en-us/dotnet/standard/base-types/substitutions-in-regular-expressions

didn't work.

The environment in question is SAS EG 7.1


Solution

  • If you cannot figure out how to make the editor generate code for you why not create a macro to do it for you in the code you submit to SAS?

    Your example would translate into a macro like this:

    %macro sumlist(varlist);
    %local i word sep;
    %do i=1 %to %sysfunc(countw(&varlist));
      %let word=%scan(&varlist,&i);
    &sep.sum(&word) as &word
      %let sep=,;
    %end;
    %mend sumlist;
    

    Which you could then call in your program to generate part of an SQL statement.

    proc sql ;
    create table want as 
      select %sumlist(a b c d e)
      from have 
    ;
    quit;
    

    Then if you really need your editor to help you create the code you are only talking about adding a constant prefix %sumlist( and suffix ) around the selected list of variable names.