DATA Step, Macro, Functions and more

how to find a word in the variable in sas datastep

Reply
Regular Contributor
Posts: 160

how to find a word in the variable in sas datastep

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.

Super User
Posts: 7,782

Re: how to find a word in the variable in sas datastep

[ Edited ]

If it is a separate word, look at the findw function. If it may be part of a word, look at index.

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
Regular Contributor
Posts: 160

Re: how to find a word in the variable in sas datastep

Posted in reply to KurtBremser

findw requires atleast 2 arguments but i only have one

Super User
Posts: 7,782

Re: how to find a word in the variable in sas datastep


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  
---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
Super User
Posts: 11,343

Re: how to find a word in the variable in sas datastep

[ Edited ]
Posted in reply to KurtBremser

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;

 

PROC Star
Posts: 746

Re: how to find a word in the variable in sas datastep

if index("SOMETH", var) > 0 then output;
Super Contributor
Posts: 308

Re: how to find a word in the variable in sas datastep

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;
Frequent Contributor
Posts: 136

Re: how to find a word in the variable in sas datastep

[ Edited ]

Use this function if your var has mixed case.

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

Thanks,
Suryakiran
Ask a Question
Discussion stats
  • 7 replies
  • 305 views
  • 0 likes
  • 6 in conversation