SAS Programming

DATA Step, Macro, Functions and more
BookmarkSubscribeRSS Feed
deleted_user
Not applicable
I have a large dataset with the variable 'species' that consists of the scientific name in the form of the two word 'Genus species' and would like to assign some of these animals into a more general 'name' variable by using only the genus portion of the scientific name. I have tried to use the INDEX function but cannot figure out how to search for multiple excerpts--an example:

if index(species,'Genus1'||'Genus2') then name = 'new name';
else name = species;

this code does not generate an error but also does resolves to the 'else' statement.
INDEXC does not appear to be an option because it does not retain the character pattern.

Does anyone have any suggestions?

Thanks
4 REPLIES 4
deleted_user
Not applicable
I'm not sure I totally understand what you're after but if the first word is always the genus then could you not use:

if scan(species,1) in ('Genus1' 'Genus2');
deleted_user
Not applicable
That is what I'm after, but this code did not work.
deleted_user
Not applicable
How did it not work? DId you get an error?

Have you managed to work it the way you want?
deleted_user
Not applicable
Please excuse me, but It works now that I separated each 'Genus' with a comma. Problem solved! Thanks a lot for your help.

sas-innovate-white.png

Special offer for SAS Communities members

Save $250 on SAS Innovate and get a free advance copy of the new SAS For Dummies book! Use the code "SASforDummies" to register. Don't miss out, May 6-9, in Orlando, Florida.

 

View the full agenda.

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
  • 4 replies
  • 11897 views
  • 0 likes
  • 1 in conversation