BookmarkSubscribeRSS Feed
Ullsokk
Pyrite | Level 9

I have a data set from a html where all the data is stored in one sting:

data have;
text = '<li><ahref="/Stavanger/A._Buddes_Vei/"title="A.BuddesVeiiStavanger">A.BuddesVei</a></li><li><ahref="/Stavanger/A.b.c.gata/"title="A.b.c.gataiStavanger">A.b.c.gata</a></li><li><ahref="/Stavanger/Abelstrappa/"title="AbelstrappaiStavanger">Abelstrappa</a></li>';
run;

 

I want to split this by each occurence of the string '</li><li>'. I want to try something like this, but where I can choose this "phrase" as the delimiter, not each individual character. The code below does not work, 

 

data try (keep=new);
 set have ; 
 do i=1 by 1 while(scan(text,i,'</a></li>') ^=' '); 
new=scan(text,i,'</a></li>'); 
output; 
end; 
run;

What I want is this: 

data want;
input text $250.;
datalines;
<li><ahref="/Stavanger/A._Buddes_Vei/"title="A.BuddesVeiiStavanger">A.BuddesVei</a></li> 
<li><ahref="/Stavanger/A.b.c.gata/"title="A.b.c.gataiStavanger">A.b.c.gata</a></li> 
<li><ahref="/Stavanger/Abelstrappa/"title="AbelstrappaiStavanger">Abelstrappa</a></li> 
run;

 

4 REPLIES 4
RW9
Diamond | Level 26 RW9
Diamond | Level 26

Something like:

%let dlm=</li>;

data have;
 text='<li><ahref="/Stavanger/A._Buddes_Vei/"title="A.BuddesVeiiStavanger">A.BuddesVei</a></li><li><ahref="/Stavanger/A.b.c.gata/"title="A.b.c.gataiStavanger">A.b.c.gata</a></li><li><ahref="/Stavanger/Abelstrappa/"title="AbelstrappaiStavanger">Abelstrappa</a></li>';
run;

data want /*(keep=res)*/;
  set have;
  length res $2000;
  do while(lengthn(text) > 0);
    res=substr(text,1,index(text,"&dlm.")+(lengthn("&dlm.")-1));
    output;
    text=substr(text,index(text,"&dlm.")+lengthn("&dlm."));
  end;
run;
Astounding
PROC Star

If you are reading from the html file directly, you can use the DLMSTR= option on the INFILE statement.

 

data want;

infile datalines dlmstr='</a></li>';

length text $ 250;

do _n_=1 to 1000 until (text=' ');

   input text @;

   if text > ' ' then output;

end;

datalines;

.........use the wide data line here ...............................

;

 

Note, however, that the delimiting string would NOT become part of the value of TEXT.

kiranv_
Rhodochrosite | Level 12

one more way

data want(keep = new);
	set have;
length val $350;
	IF _N_=1 THEN
		PATTERN=PRXPARSE ("/(.+?\<\/a\>\<\/li\>)/");
	retain pattern;
 start = 1;
 stop = length(text);
call prxnext(pattern,start,stop,text,position,length);
do while (position > 0);
new= substr(text,position,length);
call prxnext(pattern,start,stop,text,position,length);
output;
end;

run;
Ksharp
Super User

How about this one ?

 

 

data have;
text = '<li><ahref="/Stavanger/A._Buddes_Vei/"title="A.BuddesVeiiStavanger">A.BuddesVei</a></li><li><ahref="/Stavanger/A.b.c.gata/"title="A.b.c.gataiStavanger">A.b.c.gata</a></li><li><ahref="/Stavanger/Abelstrappa/"title="AbelstrappaiStavanger">Abelstrappa</a></li>';
run;

data temp;
 set have;
 id+1;
 p=find(text,'</li>');
 do while(p ne 0);
  output;
  p=find(text,'</li>',p+1);
 end;
run;
data want;
 set temp;
 by id;
 lag=lag(p);
 dif=dif(p);
 if first.id then do;start=1;length=p+4;end;
  else do;start=lag+5;length=dif;end;
 str=substr(text,start,length);
keep text str;
run;

hackathon24-white-horiz.png

The 2025 SAS Hackathon has begun!

It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.

Latest Updates

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 4 replies
  • 1760 views
  • 0 likes
  • 5 in conversation