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

I have this data set and need to select the observations that have a particular character in the string and then (edit) spit out RETURN the particular string containing that character..

 

For example, if I want the observations containing 'G' then I'd need to get these: ZGZZX, GZZZZ, HSGZZ. If I wanted the observations containing 'X' then I'd return: ZGZZX

 

Thank you

 

data temp;
input string $;
datalines;
ZGZZX
GZZZZ
ZZZZZ
HSGZZ
;
run;

 

1 ACCEPTED SOLUTION

Accepted Solutions
Reeza
Super User
Data want;
Set have;
If find(‘G’, variable)>0;
Run; 

View solution in original post

3 REPLIES 3
ballardw
Super User

"spit out" is certainly ambiguous.

 

The INDEX function is one way to find single characters anywhere in a string.

 

rc = index(string, 'G');

the variable RC will have a value greater than 0 if G is found. Note that is a case sensitive comparison and would not find "g".

 

Other search functions such as FINDC may also work depending on exact requirements.

serrld113
Obsidian | Level 7

Thank you, I edited my post to be more clear. I know to use INDEX (I believe the value returned would be the position where the character is found within the string) but I would like for the program to return the actual string where the character was found.

Reeza
Super User
Data want;
Set have;
If find(‘G’, variable)>0;
Run; 

SAS Innovate 2025: Register Now

Registration is now open for SAS Innovate 2025 , our biggest and most exciting global event of the year! Join us in Orlando, FL, May 6-9.
Sign up by Dec. 31 to get the 2024 rate of 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
  • 3 replies
  • 8839 views
  • 3 likes
  • 3 in conversation