BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
DivyaGadde
Fluorite | Level 6

Hello,

 

I am trying to extract multiple substrings from a string.

For example, I have a variable called Itinerary =  Orlando, United States (North America) Start Date: 03/30/2020 - End Date: 04/01/2020|Fort Pierce, United States (North America) Start Date: 04/01/2020 - End Date: 04/07/2020|Fort Lauderdale, United States (North America) Start Date: 04/07/2020 - End Date: 04/07/2020 

I would like to pull the substrings which are before and after the special character comma (,) So I would like to have a variable " location" which has Orlando, Unitedstates

Fort Pierce, United States

Fort Lauderdale, United States.

I have tried using Scan function but am able to pull just Orlando, United states but no the rest of the locations.

 

Any help would be highly appreciated.

 

Thanks.

 

1 ACCEPTED SOLUTION

Accepted Solutions
Kurt_Bremser
Super User

Is this whole data in one observation, or did you just put in the pipe characters to show ends of records?

 

If it's one observation, see here:

data have;
infile datalines truncover;
input itinerary $500.;
datalines;
Orlando, United States (North America) Start Date: 03/30/2020 - End Date: 04/01/2020|Fort Pierce, United States (North America) Start Date: 04/01/2020 - End Date: 04/07/2020|Fort Lauderdale, United States (North America) Start Date: 04/07/2020 - End Date: 04/07/2020 
;

data want (keep=location);
set have;
length location $100;
do i = 1 to countw(itinerary,'|');
  location = scan(scan(itinerary,i,'|'),1,'(');
  output;
end;
run;

 

View solution in original post

2 REPLIES 2
Kurt_Bremser
Super User

Is this whole data in one observation, or did you just put in the pipe characters to show ends of records?

 

If it's one observation, see here:

data have;
infile datalines truncover;
input itinerary $500.;
datalines;
Orlando, United States (North America) Start Date: 03/30/2020 - End Date: 04/01/2020|Fort Pierce, United States (North America) Start Date: 04/01/2020 - End Date: 04/07/2020|Fort Lauderdale, United States (North America) Start Date: 04/07/2020 - End Date: 04/07/2020 
;

data want (keep=location);
set have;
length location $100;
do i = 1 to countw(itinerary,'|');
  location = scan(scan(itinerary,i,'|'),1,'(');
  output;
end;
run;

 

mkeintz
PROC Star

Is the location component following the comma (e.g. "United States") always followed by an open parenthesis?  If so, and if the location component preceding the comma ("Orlando") is always at the beginning of the string, then the open paren is, operationally speaking, the real special character, as in:

 

 location=scan(strng,1,'(');

This just retrieve the first "word" in the string, where each word is all the text between (or preceding or following) the separator - in this case the "(" character.

--------------------------
The hash OUTPUT method will overwrite a SAS data set, but not append. That can be costly. Consider voting for Add a HASH object method which would append a hash object to an existing SAS data set

Would enabling PROC SORT to simultaneously output multiple datasets be useful? Then vote for
Allow PROC SORT to output multiple datasets

--------------------------

SAS Innovate 2025: Call for Content

Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!

Submit your idea!

How to Concatenate Values

Learn how use the CAT functions in SAS to join values from multiple variables into a single value.

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
  • 2 replies
  • 493 views
  • 0 likes
  • 3 in conversation