String functions (COMPUTE command)
DATA LIST FREE / FullName (A20).
BEGIN DATA
"Fred Smith"
END DATA.
STRING FirstName LastName LastFirstName (A20).
COMPUTE #spaceLoc=INDEX(FullName, " ").
COMPUTE FirstName=SUBSTR(FullName, 1, (#spaceLoc-1)).
COMPUTE LastName=SUBSTR(FullName, (#spaceLoc+1)).
COMPUTE LastFirstName=CONCAT(RTRIM(LastName), ", ", FirstName).
COMPUTE LastFirstName=REPLACE(LastFirstName, "Fred", "Ted").
- The INDEX function returns a number that represents the location of the first blank space in the value of the string variable FullName.
- The first SUBSTR function sets FirstName to the portion of FullName prior to the first space in the value. So, in this example, the value of FirstName is "Fred".
- The second SUBSTR function sets LastName to the portion of FullName after the first blank space in the value. So, in this example, the value of LastName is "Smith".
- The CONCAT function combines the values of LastName and FirstName, with a comma and a space between the two values. So, in this example, the value of LastFirstName is "Smith, Fred". Since all string values are right-padded with blank spaces to the defined width of the string variable, the RTRIM function is needed to remove all the extra blank spaces from LastName.
- The REPLACE function changes any instances of the string "Fred" in LastFirstName to "Ted". So, in this example, the value of LastFirstName is changed to "Smith, Ted".
See the topic String functions for more information.