SAS Programming

DATA Step, Macro, Functions and more
BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
Dregerator
Obsidian | Level 7

Hi, I'm trying to find the position of where the second "," is

 

For example, this is my data and code:

 

data test2;
clause3="[Type:File, URL :websitedomainname , Version :21 , Status :done , Date :01JUN2021 Notes :none" ;

n2=index(upcase(clause3),'URL :');
n3=index(clause3,', Version');
if n2>0 then do;
URL=substr(clause3,n2, n3);
output;
end;
run;

 

I want to take whatever is after URL: and create it as it's own variable. However, I want it to only take "websitedomainname "...so how do i find the position of the comma after .com so i can apply that position in my substr function. 

Currently, this is what i'm getting 

 

n2      n3     URL

13      35     URL :websitedomainname  , Version :21

 

What i would like is 

 

n2      n3     URL

                  websitedomainname  

 

Your help is greatly appreciated. 

1 ACCEPTED SOLUTION

Accepted Solutions
Reeza
Super User
Have you considered the SCAN function instead?

URL_FULL = scan(clause3, 2, ",");
URL = scan(url_full, 2, ":");

Or try URL = scan(clause3, 4, ",:");

View solution in original post

2 REPLIES 2
Reeza
Super User
Have you considered the SCAN function instead?

URL_FULL = scan(clause3, 2, ",");
URL = scan(url_full, 2, ":");

Or try URL = scan(clause3, 4, ",:");

ballardw
Super User

@Dregerator wrote:

Hi, I'm trying to find the position of where the second "," is

 

For example, this is my data and code:

 

data test2;
clause3="[Type:File, URL :websitedomainname , Version :21 , Status :done , Date :01JUN2021 Notes :none" ;

n2=index(upcase(clause3),'URL :');
n3=index(clause3,', Version');
if n2>0 then do;
URL=substr(clause3,n2, n3);
output;
end;
run;

 

I want to take whatever is after URL: and create it as it's own variable. However, I want it to only take "websitedomainname "...so how do i find the position of the comma after .com so i can apply that position in my substr function. 

Currently, this is what i'm getting 

 

n2      n3     URL

13      35     URL :websitedomainname  , Version :21

 

What i would like is 

 

n2      n3     URL

                  websitedomainname  

 

Your help is greatly appreciated. 


Your question asks " how do i find the position of the comma after .com" which is different than "second comma". Plus, your example does not include ".com".

 

Find functions have an optional fourth parameter which is start position. So you could determine the position of the string ".com" and use that as the start position to find a comma.

This finds the first comma after the first time ".com" appears:

data example;
clause3="some text, other text, abc.com , text following .com" ;
commapostcom = findc(clause3,',','i',find(clause3,'.com','i'));
run;

sas-innovate-wordmark-2025-midnight.png

Register Today!

Join us for SAS Innovate 2025, our biggest and most exciting global event of the year, in Orlando, FL, from May 6-9. Sign up by March 14 for just $795.


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
  • 1437 views
  • 0 likes
  • 3 in conversation