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.

 

Data:

HitmonTran_0-1606422767859.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
novinosrin
Tourmaline | Level 20

Thank you @HitmonTran  Have i read the pattern correctly here? If yes, it's too simple 🙂

 

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
;

data want;
 set have;
 location=substr(Itemproperties,42);
 time=substr(Itemproperties,29,12);
run;

View solution in original post

10 REPLIES 10
novinosrin
Tourmaline | Level 20

Hi @HitmonTran  Can you please paste the data as plain text rather than pics for users to copy paste?

HitmonTran
Pyrite | Level 9
updated, thanks for looking into this
novinosrin
Tourmaline | Level 20

Thank you @HitmonTran  Have i read the pattern correctly here? If yes, it's too simple 🙂

 

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
;

data want;
 set have;
 location=substr(Itemproperties,42);
 time=substr(Itemproperties,29,12);
run;
HitmonTran
Pyrite | Level 9
gosh, i think i was thinking too technical, thanks!
novinosrin
Tourmaline | Level 20

Actually so did I. I was thinking perhaps to construct a regular expression of sorts. However, either way I would still need to identify a pattern. That's when I noticed the heck in the pattern. Haha. Have a great thanksgiving holiday. Happy thanksgiving!!!

HitmonTran
Pyrite | Level 9
ok sorry, the substr function doesn't work. The position is not consistent throughout all records. It only worked in the example above
novinosrin
Tourmaline | Level 20

Can you post a few more records that varies plz?

 

Hi again @HitmonTran  I took your updated sample from the other thread. I used COMPBL function for the updated sample-

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
;
data want;
 set have;
 location=substr(compbl(Itemproperties),42);
 time=substr(compbl(Itemproperties),29,12);
run;

 

HitmonTran
Pyrite | Level 9
still not working, for example 12:00 will have an extra position compared to 2:00, thanks for looking into this during the holiday
novinosrin
Tourmaline | Level 20
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
;

data want;
 set have;
 time=prxchange('s/.*?(\d?\d:\d{2}\s-\s\d?\d:\d{2}\s(?=@)).*/$1/', -1, Itemproperties);
 location=compbl(substr(Itemproperties,index(Itemproperties,'@')+1));
run;
Ksharp
Super User
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
;

data want;
 set have;
 call scan(Itemproperties,2,p,l,'@');
 location=substr(Itemproperties,p);
 time=scan(substr(Itemproperties,1,p-1),-1,'@','a');
run;
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.

SAS Training: Just a Click Away

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

Browse our catalog!

Discussion stats
  • 10 replies
  • 1928 views
  • 0 likes
  • 3 in conversation