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

lrackley_0-1642076534939.png

Here is my desired output and sample data in datalines. Can someone help me with the Perl code to get the number that is between PT and M or PT and M? It's elapsed time since dose. I know there has to be a quicker way than using scan function. 

data times;
input pceltm $;
datalines;
PT30M
PT2H
PT4H
PT6H
;
run;

 

Thanks!

1 ACCEPTED SOLUTION

Accepted Solutions
PeterClemmensen
Tourmaline | Level 20
data times;
input pceltm $;
datalines;
PT30M
PT2H
PT4H
PT6H
;
run;

data want;
   retain r;
   if _N_ = 1 then r = prxparse('/PT(\d+)[HM]/');
   set times;
   if prxmatch(r, pceltm) then time = prxposn(r, 1, pceltm);
run;

View solution in original post

6 REPLIES 6
Ksharp
Super User
data times;
input pceltm $;
datalines;
PT30M
PT2H
PT4H
PT6H
PT6C
;
run;

data want;
 set times;
 pid=prxparse('/(?<=PT)\d+(?=M|H)/');
 call prxsubstr(pid,pceltm,p,l);
 if p then want=substr(pceltm,p,l);
 drop p l pid;
run;
tarheel13
Rhodochrosite | Level 12

It works but I only know prxmatch and prxchange. Can you explain what p and l do? I got that pid is the pattern id. 

Ksharp
Super User
p is position and l is length .
which could be used in SUBSTR() .
PeterClemmensen
Tourmaline | Level 20
data times;
input pceltm $;
datalines;
PT30M
PT2H
PT4H
PT6H
;
run;

data want;
   retain r;
   if _N_ = 1 then r = prxparse('/PT(\d+)[HM]/');
   set times;
   if prxmatch(r, pceltm) then time = prxposn(r, 1, pceltm);
run;
tarheel13
Rhodochrosite | Level 12

Amazing! Thanks both!

r_behata
Barite | Level 11
data times;
input pceltm $;
datalines;
PT30M
PT2H
PT4H
PT6H
;
run;


data want;
set times;
time=prxchange('s/(PT)(\d+)((M|H))/$2/',-1,pceltm);
run;

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

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