1
votes

I'm looking for a way to split a very long string using this delimiter : '| '

The scan function doesn't seem to accept word delimiter so if I do

scan(string,3,'| ')

it will split at every | and space
instead of at every '| ' like I need.

In the documentation I don't see any modifier allowing this. http://support.sas.com/documentation/cdl/en/lrdict/64316/HTML/default/viewer.htm#a000214639.htm

2
I just got an idea: use transwrd to replace | by _ and then use scan - stallingOne
Remove the space from your delimiter in the scan function, and it should work fine. If you get an extra space use strip or trim to remove leading spaces. - Reeza

2 Answers

3
votes

INFILE has DLMSTR when combined with infile magic can do exactly what you need. Your transwrd idea should work well.

enter image description here

data test;
   input string $50.;
   cards4;
This|is | pipe space| delmimited
This| is| too I believe
;;;;
   run;
data test2;
   infile cards missover dlmstr='| ';
   if _n_ eq 1 then input @;
   array w[5] $64;
   do while(not eof);
      set test end=eof;
      _infile_ = string;
      input @1 w[*] @;
      output;
      end; 
   stop;
   cards;
Necessary evil
   run;
1
votes

I found it out by myself

use tranwrd to replace '| ' by '_'

newstring=tranwrd(string,'| ','_');

and then I can use the scan function normally

xxx=scan(newstringstring,3,'_');