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
Rhodochrosite | Level 12
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

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

Register now!

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

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