BookmarkSubscribeRSS Feed
kz_
Quartz | Level 8 kz_
Quartz | Level 8

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. 

5 REPLIES 5
PaigeMiller
Diamond | Level 26

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.

--
Paige Miller
Patrick
Opal | Level 21

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;

Patrick_0-1674904989610.png

 

 

 

 

johnagalloway0
Fluorite | Level 6

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:

 

johnagalloway0_1-1675071299166.png

 

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

johnagalloway0
Fluorite | Level 6
apologies I misunderstood - the +3 finds the position of 'Z' - remove it (or change to +0) to get position of string
andreas_lds
Jade | Level 19

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.

 

Ready to join fellow brilliant minds for the SAS Hackathon?

Build your skills. Make connections. Enjoy creative freedom. Maybe change the world. Registration is now open through August 30th. Visit the SAS Hackathon homepage.

Register today!
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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 5 replies
  • 681 views
  • 0 likes
  • 5 in conversation