BookmarkSubscribeRSS Feed
jerry898969
Pyrite | Level 9
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.
4 REPLIES 4
sbb
Lapis Lazuli | Level 10 sbb
Lapis Lazuli | Level 10
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.
jerry898969
Pyrite | Level 9
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
AhmedAl_Attar
Ammonite | Level 13
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
jerry898969
Pyrite | Level 9
Ahmed,

thank you as well your solution works as well.

Thank you

hackathon24-white-horiz.png

The 2025 SAS Hackathon has begun!

It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.

Latest Updates

What is Bayesian Analysis?

Learn the difference between classical and Bayesian statistical approaches and see a few PROC examples to perform Bayesian analysis in this video.

Find more tutorials on the SAS Users YouTube channel.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 4 replies
  • 5807 views
  • 1 like
  • 3 in conversation