I would like to find the location of a substring of the form #.#Z, where the # sign represents any numeric digit.
For example, the substring might be 7.6Z or 1.3Z.
It is possible that there may be other digits in the string overall, though this particular pattern will only be found once in the string.
Find the location of the Z, and then you know the location of the start of the substring as well.
where = find(string,'Z','i');
Of course, that fails if a Z could appear prior to the digits.
Assuming you then also want to extract this sub-string below one way to go.
data sample;
input have_str :$40.;
_prxid=prxparse('/\d+\.\d+Z/');
want_startpos=prxmatch(_prxid,trim(have_str));
call prxsubstr(_prxid, trim(have_str), _pos , _len);
length want_str $10;
want_str=substr(have_str,_pos,_len);
/* or if you just want the number and Z could be upper or lowercase */
_prxid2=prxparse('/\d+\.\d+(?=Z)/i');
call prxsubstr(_prxid2, trim(have_str), _pos , _len);
want_num=input(substr(have_str,_pos,_len), ?? best32.);
datalines;
asdfasd78adsfad7.6Zdafad
adfadZ90Zadsf1.3Zsafd7890p0
adfadZ90Zadsf13Zsafd7890p0
;
proc print data=sample;
run;
With apologies to Patrick for stealing his test data, perl expressions will do the job but can be difficult to understand - why not just use translate and the method your question suggests:
If the character '#' may also appear in the string then just substitute another printable character - or even a non-printable character such as '00'x
Can you post a working data step containing example data? That would make it easier to suggest working code. Showing the expected results would be useful, too.
Good news: We've extended SAS Hackathon registration until Sept. 12, so you still have time to be part of our biggest event yet – our five-year anniversary!
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.