BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
Aman4SAS
Obsidian | Level 7

Can we use string as delimiter.

 

like . scan(word,"AND",1);

1 ACCEPTED SOLUTION

Accepted Solutions
ballardw
Super User

@Aman4SAS wrote:

Can we use string as delimiter.

 

like . scan(word,"AND",1);


See it this gives you enough to get started.

data example;
   x="This is a phrase with the word and embedded";
  newx = tranwrd(x,' and ','+');
  y=scan(newx,1,'+');
run;

The spaces around the ' and ' are there to get and as word so it does not get replaced inside words like "sand".

 

Use a character such as the + that 1) will not appear in your data and 2) is not a normal delimiter for the scan function so you get breaks where you expect it.

TRANWRD function is case sensitive. You may need either multiple passes through TRANWRD if you need " And " as well as " and " " AND " and so forth. Or UPCASE or LOWCASE the whole string.

View solution in original post

2 REPLIES 2
RW9
Diamond | Level 26 RW9
Diamond | Level 26

Have you read the manual:

http://support.sas.com/documentation/cdl/en/lrdict/64316/HTML/default/viewer.htm#a000214639.htm

Particularly:

charlist

specifies an optional character expression that initializes a list of characters. This list determines which characters are used as the delimiters that separate words. The following rules apply:

  • By default, all characters in charlist are used as delimiters.

  • If you specify the K modifier in the modifier argument, then all characters that are not in charlist are used as delimiters.

Tip: You can add more characters to charlist by using other modifiers.

 

 

The string you provide is not used "as is", any place where A, N, or D is found is a delimiter.  Use substr() and findw() 

ballardw
Super User

@Aman4SAS wrote:

Can we use string as delimiter.

 

like . scan(word,"AND",1);


See it this gives you enough to get started.

data example;
   x="This is a phrase with the word and embedded";
  newx = tranwrd(x,' and ','+');
  y=scan(newx,1,'+');
run;

The spaces around the ' and ' are there to get and as word so it does not get replaced inside words like "sand".

 

Use a character such as the + that 1) will not appear in your data and 2) is not a normal delimiter for the scan function so you get breaks where you expect it.

TRANWRD function is case sensitive. You may need either multiple passes through TRANWRD if you need " And " as well as " and " " AND " and so forth. Or UPCASE or LOWCASE the whole string.

SAS Innovate 2025: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

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
  • 1961 views
  • 2 likes
  • 3 in conversation