BookmarkSubscribeRSS Feed
vraj1
Quartz | Level 8

I have a dataset and in that a variable called var. In that i need extract observations which has a word "SOMETH". It may be first  word or middle or last. Can you please explain me how to do it.

 

I tried var=:"SOMETH" but it only gives if the word comes first.

7 REPLIES 7
vraj1
Quartz | Level 8

findw requires atleast 2 arguments but i only have one

Kurt_Bremser
Super User

@vraj1 wrote:

findw requires atleast 2 arguments but i only have one


Really?

You have

- a variable to search (var)

- a string to search for ('SOMETH')

So where's the problem?

 

data test;
var = 'This is a text with SOMETH in it';
result = findw(var,'SOMETH');
run;

proc print noobs; run;

Result:

              var                   result

This is a text with SOMETH in it      21  
ballardw
Super User

And if your value may sometimes have different case and you want to match "Someth", "someth" or even "someTH" as well as exactly "SOMETH" your comparison would need to consider case:

result = findw( upcase(var),'SOMETH')

 

Some actual concrete examples would help with input and desired result.

 

Note that your original search does not match "words".

See this example:

data example;
   informat str $15.;
   input str 1-15;
   if str =: 'SOMETH' then put "Found" Str=;
datalines;
SOMETH
SOMETHing
Someth ELSE
something 
;
run;

 

PeterClemmensen
Tourmaline | Level 20
if index("SOMETH", var) > 0 then output;
Loko
Barite | Level 11

Hello,

 

Sql Solution:

 

data have;
set sashelp.class;
if mod(_n_,4)=1 then name_new=cats(name)||'SOMETHetc etc';
else if mod(_n_,4)=2 then name_new=cats(name)||'SOMETHetc';
else if mod(_n_,4)=3 then name_new=name||'SOMETH';
else name_new=name;
run;

proc sql;
select * from have
where name_new like '%SOMETH%';
quit;
SuryaKiran
Meteorite | Level 14

Use this function if your var has mixed case.

find(var,"SOMETH","i")

Thanks,
Suryakiran

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!

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
  • 7 replies
  • 1159 views
  • 0 likes
  • 6 in conversation