Solutions Company Statit Training Home
 



String Manipulation

Abstract: An introduction to string manipulation.

Products: Statit e-QC, Statit e-Server, Statit Custom QC

String Manipulation is often one of the toughest data manipulations to accomplish. The following is a quick lesson in working with strings. We'll discuss concatenation as well as some functions we find useful: catvar(), concat(), decode(), encode(), len(), putwith(), substr(), and trim().

In this article, we will work with both temporary variables and common variables. We believe this will illustrate how you can use the functions discussed with both types and how you can mix the two types.

String Concatenation:

Using Statit's Concatenation operator, & or //, you can build some horrendous strings. You need to exercise care in some cases. Let's start out with a simple example:

let %helloworld = "Hello " & "World"

Sometimes you will concatenate temporary variables and literals:

let %world = "World"
let %helloworld - "Hello " & %world

Sometimes you may want to use variables:

sassign HelloWorld "Hello" "World" ## Create a variable with 2 cases
let %helloworld = HelloWorld(1) & " " & Helloworld(2) ##Need to separate the two with a space

This could also be done with a function:

let %helloworld = catvar(Helloworld,32)

catvar( ) takes as arguments a string variable and an ASCII ordinate value for the delimiter. In this case, 32 is a space so the string %helloworld would come out as "Hello World". If we used 44, the ordinate for a comma, it would come out as "Hello,World". We have used this to create a query string when the fields we want to query are in a variable.

Quotation Marks are often confusing. Here is an example:

To set the character string variable X to the string:

The file name is "test_file"

use the command:

let X = "The file name is ""test_file"""

where quotation marks to be included in the string are represented by 2 quotation marks.

Or you can do it by brute force:

let X = "The file name is "//""""//"test_file"//""""

where the string of 4 quotation marks represent a quotation mark.

Now to further confuse you, but also to illustrate another way to include quotation marks, try:

let X = 'The file name is "test_file"'

Here we use the single quote as my string delimiter and a single double quote to include a double quote in the string. Use of the single quote often takes some confusion out of a long string definition.

The concat( ) functions the same way as the "&" and "//" operators.

let %helloworld = concat("Hello ","World")

The two arguments for this function can be strings, temporary variables or variables. For example, using the data file student.wrk, you might try:

let temp = concat(name,encode(age))

This illustrates the use of a function in an argument of a function as well as introducing the function encode( ). encode() converts a numeric value to a string. The simplest form is to feed it a numeric value or variable. However, there are optional arguments to specify trimming of non-printing characters, the field width and the number of decimals.

The function trim( ) is also available to trim non-printing characters. We often use

let stringvar = trim(left(stringvar))

in our code. This forces the string to be left justified, then trims the trailing non-printing characters.

Another very important function is putwith( ) which does an encode but with formatting. For example:

let agechar = putwith(age,"%5.1f")

where the second argument is a printformat. This example printformats age as a 5.1 floating point.

You can also use a variable ID number as the second argument and the function will output the string in the format of that variable:

let %class = putwith(class(3),varid(class))

where varid(class) returns the variable ID number of class.

Sometimes you need to convert a string variable to a numeric variable. decode( ) is used for this:

let age = decode(agechar)

Finally, len( ) and substr( ) return the length of a string and a part of a string, respectively.

let namelength = len(name)
let $len = len(name(1))

To take the first 3 characters of a name:

let name3 = substr(name,1,3)

where 1 is the starting position and 3 is the substring length.

To trim off the first 2 characters of name:

let name_2 = substr(name,3,len(name))

Now a string:

let title = name & ' is ' & encode(age) & " years old, weighs " _
& encode(weight) & " pounds and is " & _
encode(height) & " inches tall. " & Name & " is in the " _
& putwith(class,varid(class)) & "class

Notice the use of ' _' (Space Underscore) for the line continuation character.