stallingOne
stallingOne

Reputation: 4006

split a string using a word delimiter in sas

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

Upvotes: 1

Views: 6090

Answers (2)

data _null_
data _null_

Reputation: 9109

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;

Upvotes: 3

stallingOne
stallingOne

Reputation: 4006

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,'_');

Upvotes: 1

Related Questions