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-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

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