BookmarkSubscribeRSS Feed
ChrilleB
Calcite | Level 5
Hi!

We ran into some problem here and I really could use some help 🙂
I am trying to locate a specific word (litium) from a variable which is coded as a string, with several different other values beside from litium. If a cell contains the word "litium", i want to assign the cell a new value such as "1", and if it doesn't contain the word "litium" I want it to be assigned "0".
The "word" litium can be stored randomly in the cell, with other words around it, or after it etc.
If it's possible I would like to do it as beutiful as possible, in a SAS data step 🙂

Thank you!

Regards,
Christian and Maja.
4 REPLIES 4
sbb
Lapis Lazuli | Level 10 sbb
Lapis Lazuli | Level 10
Explore using the TRANWRD function.

Scott Barry
SBBWorks, Inc.

Recommended Google advanced search, this topic/post:

character functions translate tranwrd site:sas.com
Flip
Fluorite | Level 6
I would suggest
newvar = (index( word, 'litium') ne 0);
ChrilleB
Calcite | Level 5
Thank you very much for your help!
The tip from Flip worked out in a very good and easy way!

Have a nice day 🙂
MikeZdeb
Rhodochrosite | Level 12
hi ... fyi, this might not apply with your current data set but ...

sometimes there's a difference between looking for a WORD and
looking for a STRING of characters

when looking for a word, it's safer to use INDEXW
since the search string might be embedded in the
text of another word

also, case might be an issue, so ...
[pre]
data test;
input text $20.;
datalines;
Mary is from Maryland
Scott is from Maryland
mary is from Maryland
;
run;

data mary;
set test;
mary1 = index (text,'mary') ne 0;
mary2 = indexw(text,'mary') ne 0;
mary3 = index(upcase(text),'MARY') ne 0;
mary4 = indexw(upcase(text),'MARY') ne 0;
run;

Obs text mary1 mary2 mary3 mary4
1 Mary is from Maryland 0 0 1 1
2 Scott is from Maryland 0 0 1 0
3 mary is from Maryland 1 1 1 1
[/pre]
if I'm looking for any observation with the person Mary
(regardless of case), only the variable MARY4 works with
this data set

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

Register now!

What is Bayesian Analysis?

Learn the difference between classical and Bayesian statistical approaches and see a few PROC examples to perform Bayesian analysis in this video.

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