Return to Archive

SAS Tip of the Month
July 2007

The COLLATE function is a strange one but can fill gap where you have to produce a string of characters in a sequence.

The syntax for the COLLATE function is:

   COLLATE(start,stop,length);

Where start is the starting character, stop is the ending character, and length is the number of characters from the starting character -- if a start, stop and length are defined then length is ignored.

The following example produces the characters A to Z to a variable called CHAR:

   69 data _null_;
   70 char=collate(rank('A'),rank('Z'));
   71 put char=;
   72 run;

   char=ABCDEFGHIJKLMNOPQRSTUVWXYZ

Note the use of the RANK function that converts the character to the character code so that we do not have to work out the code for 'A' and 'Z' before the step. We could have easily written another way using the following code:

   73 data _null_;
   74 char=collate(rank('A'),,26);
   75 put char=;
   76 run;

   char=ABCDEFGHIJKLMNOPQRSTUVWXYZ
________________________________
Updated July 1, 2007