Amadeus cookies policy - you'll see this message only once.

Amadeus use cookies on this website. They help us to know a little bit about you and how you use our website, which improves the browsing experience and marketing - both for you and for others. They are stored locally on your computer or mobile device. To accept cookies, continue browsing as normal. Or, go to the privacy policy for more information.

Incrementing Character Values

Incrementing numeric values can be easily achieved using SAS software, but what about incrementing character values?

Let's consider a scenario where we have our starting code AA and we would like to increment this value by a one character each time, so therefore the next code is AB, then AC etc to the value of ZZ.

When incrementing numeric values we can simply add 1 to the value, similarly we can use the same method with character values.

The steps we need to take are:

  • Find the ASCII equivalent value for our last character value in our code using the RANK function;
  • Find the ASCII equivalent value for our minimum and maximum values so 'A' and 'Z' which are 65 and 90 respectively;
  • Increment the ASCII equivalent value by 1 for our code;
  • Place the new ASCII value back into a character value using the BYTE function.

The RANK function is used to find the position of a character in the ASCII collating sequence.
Syntax is

RANK()

The BYTE function will return one character in the ASCII collating sequence.
Syntax is

BYTE(n)

where n ranges from 0 to 255.

Below is a simple example which starts with the code AW and will then increment the current code value by the next character value for duration of 10 iterations.

data work.code(keep=code);
  input code $;
  output; 
  do i=1 to 10;
    *Find the ASCII value for the second character value;
    last_char=rank(substr(code,2,1))+1;
    *If the new last character is higher than Z then set the  
    letter to A and increment the first character;
    if last_char > 90 then do;
      last_char=65;
      *Find the ASCII value for the first character value;
      first_char=rank(substr(code,1,1))+1;
      *If the existing code is ZZ then error;
      if first_char > 90 then put 'ERROR: No More Unique Codes'; 
      *Replace the first character with new value; 
      else substr(code,1,1)=byte(first_char);
    end;
    *Replace the second character with new value;
    substr(code,2,1)=byte(last_char);
    output;
  end;
datalines;
AW
run;
proc print data=new_codes; run;

Output Window:

Obs    code
  1     AW
  2     AX
  3     AY
  4     AZ
  5     BA
  6     BB
  7     BC
  8     BD
  9     BE
  10    BF