BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
singhsahab
Lapis Lazuli | Level 10

Hi All,

 

In my variable there are multiple URL were entered , but not in sequence. I need to filter all the URL in new dataset. Below is the sample data . 

 

data have;
infile cards ;
input url & $5000.;
cards;
https://www.youtube.com/w ahsjskjk https://www.yahoo.com jhjkj https://www.google.com
;
run;


Output want:

URL
https://www.youtube.com/w  https://www.yahoo.com  https://www.google.com

Thank you in advance 🙂 

1 ACCEPTED SOLUTION

Accepted Solutions
Kurt_Bremser
Super User

Count through "words" separated by blanks, identify those beginning with the wanted token, and concatenate to a new string:

data have;
infile cards ;
input url & $5000.;
cards;
https://www.youtube.com/w ahsjskjk https://www.yahoo.com jhjkj https://www.google.com
;

data want;
set have (rename=(url=_url));
length url $5000;
do _i = 1 to countw(_url," ");
  _word = scan(_url,_i," ");
  if substr(_word,1,4) = "http" then url = catx(" ",url,_word);
end;
drop _:;
run;

View solution in original post

2 REPLIES 2
Kurt_Bremser
Super User

Count through "words" separated by blanks, identify those beginning with the wanted token, and concatenate to a new string:

data have;
infile cards ;
input url & $5000.;
cards;
https://www.youtube.com/w ahsjskjk https://www.yahoo.com jhjkj https://www.google.com
;

data want;
set have (rename=(url=_url));
length url $5000;
do _i = 1 to countw(_url," ");
  _word = scan(_url,_i," ");
  if substr(_word,1,4) = "http" then url = catx(" ",url,_word);
end;
drop _:;
run;
ed_sas_member
Meteorite | Level 14

Hi @singhsahab 

 

Here is another approach:

proc sql noprint;
	select max(countw(url,' ')), max(count(url,"http")) into:nb1,:nb2 from have;
quit;

data want;
	set have;
	array _URL (&nb1) $ 200;
	array URL_ (&nb2) $ 200;
	do i=1 to countw(url,' ');
		if prxmatch('/http/',scan(url,i,' ')) then _URL(i) = scan(url,i,' ');
	end;
	URL_extract = catx(',',of _URL(*));
	do j=1 to count(url,"http");
		URL_(j) = scan(URL_extract,j,',');
	end;
	drop i j URL_extract _:;
run;

Capture d’écran 2020-02-26 à 09.47.05.png 

SAS Innovate 2025: Call for Content

Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!

Submit your idea!

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
  • 2 replies
  • 470 views
  • 0 likes
  • 3 in conversation