Help using Base SAS procedures

Splitting variable into multiple rows

Reply
Super Contributor
Posts: 398

Splitting variable into multiple rows

I have a variable that has a list of people delimited by "--". I want to seperate each person and make each one a row so I can sort then get a distinct list of people.

example data
Bob Jones--Fred Smith
William Feek--Cindy Simpson--Abigail Wii
David Davidson
Chris Testers--Mindy Sohow

I need this as the output
Abigail Wii
Bob Jones
Chris Testers
Cindy Simpson
David Davidson
Fred Smith
Mindy Sohow
William Feek

I was looking at using the scan function but I'm not sure how to cycle through the string.
Super Contributor
Super Contributor
Posts: 3,174

Re: Splitting variable into multiple rows

Use a DO / END loop and use COUNTW function to count the words (in your part of the DO). You will have a LENGTH/ATTRIB, a SAS assignment where SCAN is used to parse the data-string, and an OUTPUT statement (minimum) in the DO/END code paragraph.

Scott Barry
SBBWorks, Inc.
Super Contributor
Posts: 398

Re: Splitting variable into multiple rows

Scott,

You are the best. This is what I came up with and it seems to work.

data t ;
set pep (obs=5);
cnt = COUNTW(people, '--') ;
i=0;
do while(i < cnt);
i+1;
x = scan(people,i,'--') ;
output;
end;
run ;

Do you see any issues with the way i'm doing it?

Thank you again
Regular Contributor
Posts: 213

Re: Splitting variable into multiple rows

Hi,

Try using the count() function in BASE SAS.

data test (keep=name);
/* This a single string with multiple delimited names */
x='Bob Jones--Fred Smith--William Feek--Cindy Simpson--Abigail Wii--David Davidson--Chris Testers--Mindy Sohow';

/* Count number of times the delimiter occurs */
count= count(x,'--');
put count=;

/* Output each name to a separate observation */
do i=1 to (count+1);
name = scan(x,i,'--');
output;
end;
run;

proc print data=test; run;

Hope this helps,
Ahmed
Super Contributor
Posts: 398

Re: Splitting variable into multiple rows

Ahmed,

thank you as well your solution works as well.

Thank you
Ask a Question
Discussion stats
  • 4 replies
  • 2382 views
  • 1 like
  • 3 in conversation