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;
Data want;
Set have;
If find(‘G’, variable)>0;
Run;
"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.
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.
Data want;
Set have;
If find(‘G’, variable)>0;
Run;
It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.
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.
Ready to level-up your skills? Choose your own adventure.