introduction to string manipulation.
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.
Using Statit's Concatenation operator, &
you can build some horrendous strings. You need
to exercise care in some cases. Let's start
out with a simple example:
%helloworld = "Hello " & "World"
Sometimes you will concatenate temporary variables
%world = "World"
let %helloworld - "Hello " & %world
Sometimes you may want to use variables:
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:
%helloworld = catvar(Helloworld,32)
) 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
The file name is "test_file"
use the command:
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:
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:
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
) functions the same way as the "&"
%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
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
stringvar = trim(left(stringvar))
in our code. This forces the string to be left
justified, then trims the trailing non-printing
Another very important function is putwith(
) which does an encode but with formatting.
agechar = putwith(age,"%5.1f")
where the second argument is a printformat.
This example printformats age as a 5.1 floating
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:
%class = putwith(class(3),varid(class))
returns the variable ID number of class.
Sometimes you need to convert a string variable
to a numeric variable. decode( ) is used
age = decode(agechar)
Finally, len( ) and substr( )
return the length of a string and a part of
a string, respectively.
namelength = len(name)
$len = len(name(1))
To take the first 3 characters of a name:
name3 = substr(name,1,3)
where 1 is the starting position and 3 is the
To trim off the first 2 characters of name:
name_2 = substr(name,3,len(name))
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.