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-2026-white.png



April 27 – 30 | Gaylord Texan | Grapevine, Texas

Registration is open

Walk in ready to learn. Walk out ready to deliver. This is the data and AI conference you can't afford to miss.
Register now and lock in 2025 pricing—just $495!

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.

SAS Training: Just a Click Away

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

Browse our catalog!

Discussion stats
  • 2 replies
  • 876 views
  • 0 likes
  • 3 in conversation