Google SAS Search

Add to Google

Wednesday, March 26, 2008

The Cats() Function

One of my favorite new functions is the cats() function available in SAS v9. It is a compress() like function in that it removes leading and
trailing blanks, but it also concatenates the results. CATS() stands for concatenate and strip. Basically the cats() function takes this
type of assignment statement:

key = trim(left(firstName)) || trim(left(lastName)) ||
trim(left(phone)) || trim(left(zip)) ;

and changes it to this:

key = cats(firstName, lastName, phone, zip);

Generally, the cats() function will return a value with a length of 32767 in the data step. So as always, it's a good idea to use
a length statement on the variable you are assigning to. In this example I might use something like:

length key $200;

The cats() function belongs to a family of cat() functions each doing it's own version of concatenate: cat(), cats(), catt(), catx().

Coming up in version 10, the dog() family of functions! :)


  1. Good function! I haven't kept up with all of the new functions so it is nice to know about some of the better ones.

  2. Kind of almost unrelated question... is there any difference between using trim(left()) and strip()?

  3. This saved my tail tonight. I actually used the older TRIM format because I needed to concatenate last name and first name from an Excel upload while trimming and putting a comma between the fields. Worked like a charm The final statement was:


    Thanks a bunch.

  4. I am learning how to write macros. I have the following code but it does not do what I think it should do:
    %macro loop;
    FileN Physician FilePath DataSetName;
    data abardai1.test;
    set abardai1.FilesToImport;
    FileN = FileName;
    FilePath = "M:\MAN\PDI Data\NEW PDI Folders\VPL Processing\B- Validation\Stage 1\Passed\";
    DataSetName = "Abardai1."||trim(Physician)||"_Import";
    put position1 position2 physician DataSetName;
    %do i = 1 %to 1;
    proc import out=DataSetName
    datafile = ""||FilePath||FILEN||"";
    dbms=excel REPLACE;
    What I want to do: "Build a file path and name by reading the name in another dataset. Then IMPORT that file."

    I checked the intermediate varibles and they seem to be compiling the correct file name. But the IMPORT gives me the following error:
    5 ! dbms=excel REPLACE; run;

    ERROR 22-322: Syntax error, expecting one of the following: ;, DATAFILE, DATATABLE, DBMS, DEBUG,

    ERROR 76-322: Syntax error, statement will be ignored.

    I am totally at loss to try and figure out WHY SAS complains with "||", or so it seems. Examples I have seen here seem to suggest that 'a'||'b' should give me ab.

    If anyone can help me, I would be very very grateful!


    PS: The file name is like L123_ABC_1234.xls

  5. Be careful with cats function, especially if you try to convert numeric variable: if you provide missing as argument you get VERY unpredictable results.