Help using Base SAS procedures

Character String Parsing

Accepted Solution Solved
Reply
New Contributor
Posts: 2
Accepted Solution

Character String Parsing


Using SAS V8, how can I parse a character string into several smaller fields based on a delimeter character of '*' or '~'?


Accepted Solutions
Solution
‎05-18-2015 02:06 PM
Valued Guide
Posts: 854

Re: Character String Parsing

data have;

infile cards dsd;

length name $50.;

format name $char50.;

input name;

cards;

name1*name2*name3~name4

;

run;

data want;

set have;

name1=scan(name,1,'*~');

name2=scan(name,2,'*~');

name3=scan(name,3,'*~');

name4=scan(name,4,'*~');

run;

View solution in original post


All Replies
Solution
‎05-18-2015 02:06 PM
Valued Guide
Posts: 854

Re: Character String Parsing

data have;

infile cards dsd;

length name $50.;

format name $char50.;

input name;

cards;

name1*name2*name3~name4

;

run;

data want;

set have;

name1=scan(name,1,'*~');

name2=scan(name,2,'*~');

name3=scan(name,3,'*~');

name4=scan(name,4,'*~');

run;

New Contributor
Posts: 2

Re: Character String Parsing

Thank you very much for the quick response!!!

Contributor
Posts: 30

Re: Character String Parsing

Hi Cherric ,

I have a another automated way which you can try,  here  you dont have to type name if the string have 1000 names.  .

data all;

  drop string;

  string = ' name1*name2*name3~name4*name5~name6 ';

  do until(word=' ');

  count+1;

  word = scan(string, count,'*~');

  output;

  end;

run;

if you want it in horizontal way you can transpose the same

☑ This topic is SOLVED.

Need further help from the community? Please ask a new question.

Discussion stats
  • 3 replies
  • 195 views
  • 0 likes
  • 3 in conversation