BookmarkSubscribeRSS Feed
upadhi
Quartz | Level 8

hi team,

i want to extract single word coming before and after dot(.)

 

for example:

string : find all words before and after dot.like this; select lib.tab , lib1.tab1;

Output: dot.like lib.tab, lib1.tab1

 

The idea is to extract lib_name.table_name from sas code line

2 REPLIES 2
jimbarbour
Meteorite | Level 14

I would find the position of the dot (period) using the Index function and then scan left or right from there.

 

Jim

jimbarbour
Meteorite | Level 14

Here's some sample code:

%LET	String		=	I love apple.pie and ice cream;

%LET	Position	=	%SYSFUNC(INDEXC(&String,.));
%LET	Word_Before	=	%SYSFUNC(SCAN(%QSUBSTR(&String, 1, %EVAL(&Position - 1)), -1));
%LET	Word_After	=	%SYSFUNC(SCAN(%QSUBSTR(&String, %EVAL(&Position + 1)), 1));

OPTION	NOSOURCE;
%PUT	NOTE:  &=Position;
%PUT	NOTE-  &=Word_Before;
%PUT	NOTE-  &=Word_After;
OPTION	SOURCE;

Results:

NOTE:  POSITION=13
       WORD_BEFORE=apple
       WORD_AFTER=pie

Note that this code may not give you the results you want if there are multiple periods in one text string.

 

Jim

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