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!
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;
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;
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.
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;
Amazing! Thanks both!
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;
It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.
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.
Ready to level-up your skills? Choose your own adventure.