BookmarkSubscribeRSS Feed
☑ This topic is solved. Need further help from the community? Please sign in and ask a new question.
RandoDando
Pyrite | Level 9

 I have a raw data file which is importing some text columns with tags on either side of the data.  The tags are not exactly the same on the left side for each case, and include no space.  An example would be this:

<a href="a213w9245992999Vrh" target="_blank">PROPER DATA</a>

I want to extract from that PROPER DATA.

The alphanumeric portion after href= on the left side can vary from row to row. 

I've tried this, but it does nothing.

pos1=findc(StringVar, '>');
pos2=findc(StringVar, '</a>');
NewVar = substr(StringVar, nos1+1, nos2-1-nos2);

I've tried this and had some result.  No change.

pos1=findc(StringVar, '_blank">');
pos2=findc(StringVar, '</a>');
NewVar = substr(StringVar, nos1+1, nos2-1-nos2);

How can I extract that text?

1 ACCEPTED SOLUTION

Accepted Solutions
Quentin
Super User

Assuming the only time the < and > symbols are in your data is for the tags, you could use the SCAN function and treat those as delimiters:

 

6    data want ;
7      str='<a href="a213w9245992999Vrh" target="_blank">PROPER DATA</a>' ;
8      length want $50 ;
9      want=scan(str,2,'<>') ;
10     put want= ;
11   run ;

want=PROPER DATA
NOTE: The data set WORK.WANT has 1 observations and 2 variables.
BASUG is hosting free webinars Next up: Mark Keintz presenting History Carried Forward, Future Carried Back: Mixing Time Series of Differing Frequencies on May 8. Register now at the Boston Area SAS Users Group event page: https://www.basug.org/events.

View solution in original post

5 REPLIES 5
Quentin
Super User

Assuming the only time the < and > symbols are in your data is for the tags, you could use the SCAN function and treat those as delimiters:

 

6    data want ;
7      str='<a href="a213w9245992999Vrh" target="_blank">PROPER DATA</a>' ;
8      length want $50 ;
9      want=scan(str,2,'<>') ;
10     put want= ;
11   run ;

want=PROPER DATA
NOTE: The data set WORK.WANT has 1 observations and 2 variables.
BASUG is hosting free webinars Next up: Mark Keintz presenting History Carried Forward, Future Carried Back: Mixing Time Series of Differing Frequencies on May 8. Register now at the Boston Area SAS Users Group event page: https://www.basug.org/events.
ballardw
Super User

If your example is of all the lines you need to read (unlikely I suspect) then perhaps:

data example;
   string='<a href="a213w9245992999Vrh" target="_blank">PROPER DATA</a>';
   want = scan(string,2,'<>');
run;

SCAN will allow you to provide characters that delimit string values. Since your line starts with an < then it precedes the first string which would be everything until the next < or > character.

You would want to assign a length to the target variable, Want above, to hold the longest expected value prior to use.

 

I would expect strange results. Your use of

nos2-1-nos2)

Would tent to always result in a value of -1. ( nos2-1-nos2 is equivalent to nos2-nos2-1) Which when you get negative lengths SUBSTR tends to odd things as documented in the online help:

Interaction If length is zero, a negative value, or larger than the length of the expression that remains in string after position, SAS extracts the remainder of the expression. SAS also sets _ERROR_ to 1 and prints a note to the log indicating that the length argument is invalid.

There should be notes in the log.

 


@RandoDando wrote:

 I have a raw data file which is importing some text columns with tags on either side of the data.  The tags are not exactly the same on the left side for each case, and include no space.  An example would be this:

<a href="a213w9245992999Vrh" target="_blank">PROPER DATA</a>

I want to extract from that PROPER DATA.

The alphanumeric portion after href= on the left side can vary from row to row. 

I've tried this, but it does nothing.

pos1=findc(StringVar, '>');
pos2=findc(StringVar, '</a>');
NewVar = substr(StringVar, nos1+1, nos2-1-nos2);

I've tried this and had some result.  No change.

pos1=findc(StringVar, '_blank">');
pos2=findc(StringVar, '</a>');
NewVar = substr(StringVar, nos1+1, nos2-1-nos2);

How can I extract that text?


 

FreelanceReinh
Jade | Level 19

Hello @RandoDando,

 

Let's create a few more test strings as sample data:

data have;
input StringVar &$100.;
cards;
<a href="a213w9245992999Vrh" target="_blank">PROPER DATA</a>
<a href="a213w9245992999Vrh" target="_blank"></a>
<b>bla</b><abc>xyz</abc><a href="a213w9245992999Vrh" target="_blank">PROPER DATA</a>
<a href="a213w9245992999Vrh" target="_blank">
TEST</a>
;

 

Now let's fix your first approach:

data want(drop=pos:);
set have;
pos0=find(StringVar, '<a ');
pos1=findc(StringVar, '>', pos0+3);
pos2=find(StringVar, '</a>', pos1+1);
NewVar = substrn(StringVar, pos1+1, pos2-1-pos1);
run;

To make it more robust (against strings such as 'NOT>THIS</a>'), you may want to add IF conditions like

data want(drop=pos:);
set have;
pos0=find(StringVar, '<a ');
if pos0 then pos1=findc(StringVar, '>', pos0+3);
if pos1 then pos2=find(StringVar, '</a>', pos1+1);
if pos2 then NewVar = substrn(StringVar, pos1+1, pos2-1-pos1);
run;

possibly with nested DO-END blocks if performance is an issue. You could also modify your second approach analogously.

 

Note that the separation of the fourth and fifth observation prevents 'TEST' from being found.

Ksharp
Super User

It is best example/scenario for Perl Regular Expression.

 

data have;
input StringVar &$100.;
cards;
<a href="a213w9245992999Vrh" target="_blank">PROPER DATA</a>
<a href="a213w9245992999Vrh" target="_blank"></a>
<b>bla</b><abc>xyz</abc><a href="a213w9245992999Vrh" target="_blank">PROPER DATA</a>
<a href="a213w9245992999Vrh" target="_blank">
TEST</a>
;
data want;
 set have;
 want=prxchange('s/<.+?>//',-1,StringVar);
run;

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

Register now!

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
  • 422 views
  • 0 likes
  • 5 in conversation