Search code examples
sassas-macro

How can I concatenate a space using CATX function?


I need to form a line concatenating numbers and strings, but separated by a space.

I tried it in 5 ways, but it didn't give the desired result.

%LET lim1 = 113;
%LET lim2 = 166;

Test 1:

%LET linha = %SYSFUNC(CATS(De,&lim1,a,&lim2,clientes));
%PUT &linha;

Out 1:

De113a166clientes

Test 2:

%LET linha = %SYSFUNC(CATS('De ',&lim1,' a ',&lim2,' clientes'));
%PUT &linha;

Out 2 (Error):

30         %LET linha = %SYSFUNC(CATS('De ',&lim1,' a ',&lim2,' clientes'));
NOTE: Line generated by the macro function "SYSFUNC".
30          'De '113' a '166' clientes'
            _____   _____
            49      49
NOTE 49-169: The meaning of an identifier after a quoted string might change in a future SAS release.  Inserting white space 
             between a quoted string and the succeeding identifier is recommended.
             

Test 3:

%LET linha = %SYSFUNC(CATX(' ','De ',&lim1,' a ',&lim2,' clientes'));
%PUT &linha;

Out 3 (Error):

29         %LET linha = %SYSFUNC(CATX(' ','De ',&lim1,' a ',&lim2,' clientes'));
NOTE: Line generated by the macro function "SYSFUNC".
29          'De '' '113' '' a '' '166' '' clientes'
            ________   ___________
            49         49
NOTE 49-169: The meaning of an identifier after a quoted string might change in a future SAS release.  Inserting white space 
             between a quoted string and the succeeding identifier is recommended.

Test 4:

%LET linha = %SYSFUNC(CATX(' ',De,&lim1,a,&lim2,clientes));
%PUT &linha;

Out 4 (Error):

NOTE: Line generated by the macro function "SYSFUNC".
29          De' '113' 'a' '166' 'clientes
              ___   ___ ___   ___
              49    49  49    49
NOTE 49-169: The meaning of an identifier after a quoted string might change in a future SAS release.  Inserting white space 
             between a quoted string and the succeeding identifier is recommended.

Test 5:

%LET linha = %SYSFUNC(CATX(*,De,&lim1,a,&lim2,clientes));
%PUT &linha;

Out 5:

De*113*a*166*clientes

Test 5 is as close as I need, but I need to replace * with a blank space.

I need: De 113 a 166 clientes

Unfortunately, I was not successful.


Solution

  • In macro you don't need to use CAT for assembling a source code text.

    Just resolve the macro variables in the context desired.

    %LET lim1 = 113;
    %LET lim2 = 166;
    %LET linha = De &lim1 a &lim2 clientes;
    %PUT &=linha;
    ----- LOG -----
    LINHA=De 113 a 166 clientes
    

    If using a macro variable value in the DATA step context of a quoted string or string value computation the resolution should be within double quotes of a string literal (unless the macro value is already literally double quoted text)

    data have;
      input (part1-part3) ($);
    datalines;
    De a clientes
    Si o consumer
    Mr A Sky
    ;
    %LET lim1 = 113;
    %LET lim2 = 166;
    
    data want;
      set have;
      result = catx(' ', part1, "&lim1", part2, "&lim2", part3);
      put result=;
    run;
    ----- LOG -----
    result=De 113 a 166 clientes
    result=Si 113 o 166 consumer
    result=Mr 113 A 166 Sky