DATA Step, Macro, Functions and more

If specific character found in string then return string

Accepted Solution Solved
Reply
Occasional Contributor
Posts: 17
Accepted Solution

If specific character found in string then return string

[ Edited ]

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;

 


Accepted Solutions
Solution
‎03-19-2018 12:27 PM
Super User
Posts: 24,002

Re: If specific character found in string then return string

Posted in reply to serrld113
Data want;
Set have;
If find(‘G’, variable)>0;
Run; 

View solution in original post


All Replies
Super User
Posts: 13,939

Re: If specific character found in string then return string

Posted in reply to serrld113

"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.

Occasional Contributor
Posts: 17

Re: If specific character found in string then return string

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.

Solution
‎03-19-2018 12:27 PM
Super User
Posts: 24,002

Re: If specific character found in string then return string

Posted in reply to serrld113
Data want;
Set have;
If find(‘G’, variable)>0;
Run; 
☑ This topic is solved.

Need further help from the community? Please ask a new question.

Discussion stats
  • 3 replies
  • 90 views
  • 3 likes
  • 3 in conversation