SAS: How to Split Strings by Delimiter


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;

Additional Resources

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

How to Normalize Data in SAS
How to Rename Variables in SAS
How to Remove Duplicates in SAS
How to Replace Missing Values with Zero in SAS

Leave a Reply

Your email address will not be published.