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

HI 

I want to Know the position of first occurence of '~LX' or '~HL' or '~SE' whichever comes first.

 

I don't now why the code is not WorKing.

 

I will really appreciate your help.


data s1;
input string :$2000.;
Datalines;
SV1*HC+99348+LM*25+KX++++CARE*84*UN*1*12**1~DTP*472*RD8*20180720-20180720~LX*1~
SV1*HC+11721+59+~HL*1~*Q8+XX++CARE*47*UN*1*~LX*1~*12**2+3~DTP*472*RD8*20180720-20180720~REF*6R*2~
SV1*HC+11057+Q8+JX++~HL*+LX+CARE*70*UN*1*12**4+1~DTP*472*RD8*~LX*4~20180720-20180720~REF
SV1*HC+~SE+99213+LX+~SE+++CARE*150*UN*1*11**1+2~DTP*472*RD8*~LX*2~*20181004-20181004~REF*
;


data s2;
set s1;
word1= indexc(String,'~LX', '~HL','SE') ;
run;

1 ACCEPTED SOLUTION

Accepted Solutions
Shmuel
Garnet | Level 18

INDEXC is used to get the position of a character in a string.

You may enter a list of characters to search for if any is found the position of first of them will be in result.

 

As same line may have any searched string more than once you must check each separately.

That can be done either by INDEX() or by FIND() functions.

For example:

data want;
 set have;
       pos1 = index(string,'~LX');
       pos2 = index(string,'~HL');
pos3 = index(string,'SE'); .....
word = min(pos1-pos3); run;

If you are looking for first '~' character you can use INDEX / INDEXC / FIND functions;

       word = indexc(string,'~');

View solution in original post

3 REPLIES 3
RW9
Diamond | Level 26 RW9
Diamond | Level 26

Read the manual:

http://support.sas.com/documentation/cdl/en/lefunctionsref/63354/HTML/default/viewer.htm#n04ha26lk1s...

 

Particularly:

Details

The INDEXC function searches source, from left to right, for the first occurrence of any character present in the excerpts and returns the position in source of that character. If none of the characters in excerpt-1 through excerpt-n in source are found, INDEXC returns a value of 0.
 
So indexc will return the position of the first character from your list, not the whole string.  
 
Use index for this:
Shmuel
Garnet | Level 18

INDEXC is used to get the position of a character in a string.

You may enter a list of characters to search for if any is found the position of first of them will be in result.

 

As same line may have any searched string more than once you must check each separately.

That can be done either by INDEX() or by FIND() functions.

For example:

data want;
 set have;
       pos1 = index(string,'~LX');
       pos2 = index(string,'~HL');
pos3 = index(string,'SE'); .....
word = min(pos1-pos3); run;

If you are looking for first '~' character you can use INDEX / INDEXC / FIND functions;

       word = indexc(string,'~');

athapa1183
Obsidian | Level 7
I really appreciate your help.

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

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.

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