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

I need to extract the time range and location from a variable "Itemproperties".  I'm having difficulty bc some location have 2 delimerters ("@") and others will have 1. Also the substr would not work bc the position of the characters are not consistent.

 

Data:

HitmonTran_1-1606426419690.png

 

 

Data:

Itemproperties

Pickup Details:6. 11/21 SAT    4:40 - 5:00 @ Mountain View @ Ranch 99

Pickup Details:6. 11/21 SAT 5:30 - 5:50 @ Cupertino

Pickup Details:5.      10/23 FRI 4:40 - 5:00 @     W. SJ @   Starbucks

 

 

want:

location                                                 time

Mountain View @ Ranch 99                  4:40 - 5:00

Cupertino                                               5:30 - 5:50

W. SJ @ Starbucks                                4:40 - 5:00

1 ACCEPTED SOLUTION

Accepted Solutions
r_behata
Barite | Level 11
data have;
infile cards truncover;
input Itemproperties  $100. ;
cards;
Pickup Details:6. 11/21 SAT    4:40 - 5:00 @ Mountain View @ Ranch 99
Pickup Details:6. 11/21 SAT 5:30 - 5:50 @ Cupertino
Pickup Details:5.      10/23 FRI 4:40 - 5:00 @     W. SJ @ Starbucks
;
run;


data want;
	set have;
	
	location=strip(substr(Itemproperties,index(Itemproperties,'@')+1));

/* Regular Expression - Definition */
/* \d matches a digit (equal to [0-9]) */
/* : matches the character : literally  */
/* \d+ matches a digit (equal to [0-9]) */
/* + Quantifier — Matches between one and unlimited times, as many times as possible, giving back as needed (greedy) */
/* \s matches any whitespace character  */
/* - matches the character - literally  */
/* \s matches any whitespace character  */
/* \d matches a digit (equal to [0-9]) */
/* : matches the character : literally  */
/* \d+ matches a digit (equal to [0-9]) */
/* + Quantifier — Matches between one and unlimited times, as many times as possible, giving back as needed (greedy) */
	
	patternID = prxparse('/(\d:\d+\s-\s\d:\d+)/');
	
	call prxsubstr(patternID, Itemproperties, position, length);
	
	if position > 0 then time=substr(Itemproperties,position,length);

	


drop patternID position length;
run;

View solution in original post

6 REPLIES 6
Reeza
Super User
Please expand your sample data to include records where the already provided solutions did not work. Otherwise you'll get the same answers again.
https://communities.sas.com/t5/SAS-Programming/how-to-find-the-nth-string/m-p/701893

HitmonTran
Pyrite | Level 9
i did provide the sample data above. it is different from the previous version with more inconsistent spaces.
r_behata
Barite | Level 11
data have;
infile cards truncover;
input Itemproperties  $100. ;
cards;
Pickup Details:6. 11/21 SAT    4:40 - 5:00 @ Mountain View @ Ranch 99
Pickup Details:6. 11/21 SAT 5:30 - 5:50 @ Cupertino
Pickup Details:5.      10/23 FRI 4:40 - 5:00 @     W. SJ @ Starbucks
;
run;


data want;
	set have;
	
	location=strip(substr(Itemproperties,index(Itemproperties,'@')+1));

/* Regular Expression - Definition */
/* \d matches a digit (equal to [0-9]) */
/* : matches the character : literally  */
/* \d+ matches a digit (equal to [0-9]) */
/* + Quantifier — Matches between one and unlimited times, as many times as possible, giving back as needed (greedy) */
/* \s matches any whitespace character  */
/* - matches the character - literally  */
/* \s matches any whitespace character  */
/* \d matches a digit (equal to [0-9]) */
/* : matches the character : literally  */
/* \d+ matches a digit (equal to [0-9]) */
/* + Quantifier — Matches between one and unlimited times, as many times as possible, giving back as needed (greedy) */
	
	patternID = prxparse('/(\d:\d+\s-\s\d:\d+)/');
	
	call prxsubstr(patternID, Itemproperties, position, length);
	
	if position > 0 then time=substr(Itemproperties,position,length);

	


drop patternID position length;
run;
ChrisNZ
Tourmaline | Level 20

Many ways to do this. Here are 2 examples. Arrange to suit.

data TEST;
  infile cards truncover;
  input ITEMPROPERTIES  $100. ;
  TIME1=scan(ITEMPROPERTIES,5,' ')||'-'||scan(ITEMPROPERTIES,7,' ');
  LOC1 =substr(ITEMPROPERTIES,index(ITEMPROPERTIES,'@')+1);
  TIME2=prxchange('s/.*(\d\d?:\d\d[ -]{1,3}\d\d?:\d\d).*/\1/',1,ITEMPROPERTIES);
  LOC2 =prxchange('s/.*?@ *(.*)/\1/',1,ITEMPROPERTIES);
cards;                                     
Pickup Details:6. 11/21 SAT    4:40 - 5:00 @ Mountain View @ Ranch 99
Pickup Details:6. 11/21 SAT 5:30 - 5:50 @ Cupertino
Pickup Details:5.      10/23 FRI 4:40 - 5:00 @     W. SJ @ Starbucks
run;
ITEMPROPERTIES TIME1 LOC1 TIME2 LOC2
Pickup Details:6. 11/21 SAT 4:40 - 5:00 @ Mountain View @ Ranch 99 4:40-5:00 Mountain View @ Ranch 99 4:40 - 5:00 Mountain View @ Ranch 99
Pickup Details:6. 11/21 SAT 5:30 - 5:50 @ Cupertino 5:30-5:50 Cupertino 5:30 - 5:50 Cupertino
Pickup Details:5. 10/23 FRI 4:40 - 5:00 @ W. SJ @ Starbucks 4:40-5:00 W. SJ @ Starbucks 4:40 - 5:00 W. SJ @ Starbucks

 

ChrisNZ
Tourmaline | Level 20

Also note how more flexible the RegEx is. It can parse this data without issue (spaces removed around and inside time, and after @).

Pickup Details:5. 10/23 FRI4:40-5:00@W. SJ @ Starbucks

 

HitmonTran
Pyrite | Level 9
thanks, your code worked as well. happy holidays!

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!

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
  • 6 replies
  • 668 views
  • 0 likes
  • 4 in conversation