<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Reg Ex lookahead to extract numbers in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Reg-Ex-lookahead-to-extract-numbers/m-p/615904#M180212</link>
    <description>&lt;P&gt;I have a string for a number of hours, minutes and seconds, but not all three will necessarily be present. E.g. it might be "1h2m34s" or just "2m34s" or just "34s".&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I want to extract the numbers from the string and assign them to appropriate variables called hour, min and sec (or similar). I'm trying to use regex and lookahead but I'm getting stuck.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;data have;
    infile datalines;
    input hms:$100.;
datalines;
7s
52s
2m9s
3m43s
10m43s
1h2m13s
;;;;
run;&lt;/PRE&gt;&lt;P&gt;I've tried:&lt;/P&gt;&lt;PRE&gt;data get_nums_re;
    retain re_hour re_min;
    if _n_ = 1 then do;
        re_hour = prxparse("/.*\d+(?=h).*/");
        re_min = prxparse("/.*\d+(?=m).*/");
        end;
    set have;
    hour_pos = prxposn(re_hour, 1, hms);
    min_pos = prxposn(re_min, 1, hms);
run;&lt;/PRE&gt;&lt;P&gt;to try and get the positions of just the hours and minutes (for example) but it's not populating the hour_pos and min_pos.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;How do I assign (where applicable) the relevant numbers to the relevant variables.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Wed, 08 Jan 2020 12:49:45 GMT</pubDate>
    <dc:creator>stevhill</dc:creator>
    <dc:date>2020-01-08T12:49:45Z</dc:date>
    <item>
      <title>Reg Ex lookahead to extract numbers</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Reg-Ex-lookahead-to-extract-numbers/m-p/615904#M180212</link>
      <description>&lt;P&gt;I have a string for a number of hours, minutes and seconds, but not all three will necessarily be present. E.g. it might be "1h2m34s" or just "2m34s" or just "34s".&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I want to extract the numbers from the string and assign them to appropriate variables called hour, min and sec (or similar). I'm trying to use regex and lookahead but I'm getting stuck.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;data have;
    infile datalines;
    input hms:$100.;
datalines;
7s
52s
2m9s
3m43s
10m43s
1h2m13s
;;;;
run;&lt;/PRE&gt;&lt;P&gt;I've tried:&lt;/P&gt;&lt;PRE&gt;data get_nums_re;
    retain re_hour re_min;
    if _n_ = 1 then do;
        re_hour = prxparse("/.*\d+(?=h).*/");
        re_min = prxparse("/.*\d+(?=m).*/");
        end;
    set have;
    hour_pos = prxposn(re_hour, 1, hms);
    min_pos = prxposn(re_min, 1, hms);
run;&lt;/PRE&gt;&lt;P&gt;to try and get the positions of just the hours and minutes (for example) but it's not populating the hour_pos and min_pos.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;How do I assign (where applicable) the relevant numbers to the relevant variables.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 08 Jan 2020 12:49:45 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Reg-Ex-lookahead-to-extract-numbers/m-p/615904#M180212</guid>
      <dc:creator>stevhill</dc:creator>
      <dc:date>2020-01-08T12:49:45Z</dc:date>
    </item>
    <item>
      <title>Re: Reg Ex lookahead to extract numbers</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Reg-Ex-lookahead-to-extract-numbers/m-p/615912#M180214</link>
      <description>&lt;P&gt;Make it easy and use the simpler string functions:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
    infile datalines;
    input hms:$100.;
datalines;
7s
52s
2m9s
3m43s
10m43s
1h2m13s
;

data want;
set have;
help = hms;
if indexc('h',help)
then do;
  hours = input(scan(help,1,'h'),2.);
  help = scan(help,2,'h');
end;
if indexc('m',help)
then do;
  minutes = input(scan(help,1,'m'),2.);
  help = scan(help,2,'m');
end;
seconds = input(scan(help,1,'s'),2.);
drop help;
run;

proc print data=want noobs;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Result:&lt;/P&gt;
&lt;PRE&gt;hms        hours    minutes    seconds

7s           .          .          7  
52s          .          .         52  
2m9s         .          2          9  
3m43s        .          3         43  
10m43s       .         10         43  
1h2m13s      1          2         13  
&lt;/PRE&gt;</description>
      <pubDate>Wed, 08 Jan 2020 13:10:29 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Reg-Ex-lookahead-to-extract-numbers/m-p/615912#M180214</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2020-01-08T13:10:29Z</dc:date>
    </item>
    <item>
      <title>Re: Reg Ex lookahead to extract numbers</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Reg-Ex-lookahead-to-extract-numbers/m-p/615915#M180215</link>
      <description>&lt;P&gt;Very interesting question.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
    infile datalines;
    input hms:$100.;
datalines;
7s
52s
2m9s
3m43s
10m43s
1h2m13s
;;;;
run;
data temp;
 set have;
 n+1;
 do i=1 to countw(hms,,'kd');
  vname=scan(hms,i,,'d');
  value=scan(hms,i,,'kd');
  output;
 end;
run;
proc transpose data=temp out=want;
by n;
id vname;
var value;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 08 Jan 2020 13:15:25 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Reg-Ex-lookahead-to-extract-numbers/m-p/615915#M180215</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2020-01-08T13:15:25Z</dc:date>
    </item>
  </channel>
</rss>

