How do you split strings by delimiter in SAS?

In SAS, strings can be split by delimiter using the SCAN function, which takes a character string and divides it into separate parts based on the specified delimiter. The SCAN function returns the parts of the string as separate variables, which can then be used as needed.


You can use the scan() function in SAS to quickly split a string based on a particular delimiter.

The following example shows how to use this function in practice.

Example: Split Strings by Delimiter in SAS

Suppose we have the following dataset in SAS:

/*create dataset*/
data my_data1;
    input name $25.;
    datalines;
Andy_Lincoln_Bernard
Barry_Michael
Chad_Simpson_Smith
Derrick_Parson_Henry
Eric_Miller
Frank_Giovanni_Goodwill
;
run;

/*print dataset*/
proc print data=my_data1;

We can use the following code to quickly split the name string into three separate strings:

/*create second dataset with name split into three columns*/
data my_data2;
    set my_data1;
    name1=scan(name, 1, '_');
    name2=scan(name, 2, '_');
    name3=scan(name, 3, '_');
run;

/*view second dataset*/
proc print data=my_data2;

Notice that the string in the name column has been split into three new columns.

For the names where there was only one delimiter, the value in the name3 column is simply blank.

Note that we could also use the drop function to drop the original name column from the new dataset:

/*create second dataset with name split into three columns, drop original name*/
data my_data2;
    set my_data1;
    name1=scan(name, 1, '_');
    name2=scan(name, 2, '_');
    name3=scan(name, 3, '_');
    drop name;
run;

/*view second dataset*/
proc print data=my_data2;

The following tutorials explain how to perform other common tasks in SAS:

x