I have a an excel which contains extra character. How to serach for a specific strung and create another column to assign those value.
Here is a sample example.
data check;
input Message$40. ID ;
cards;
this&is&the&season 2
chrome&is&powerful&chrome 4
chrome&is&causing&the&Error&chrome. 3
the&service¢er&is&causing&the&Error. 5
the&error&is&comming&from&IE9 6
the &error&is&comming&from&chrome 7
chrome&is&causing&the&Error&IE9 . 9
the&service¢er&is&causing&the&Error. 10
the&error&is&comming&from&IE9 11
the&error&is&comming&from&chrome 12
;
run;
data check2;
set check;
if Message="chrome" then flag="chorme";
if Message="IE9" then flag="IE9";
else flag="";
run;
try using a CASE statement with LIKE in case you wanted to match any value within a string. Hope the below example sheds some light.
data check;
input Message$40. ID ;
cards;
this&is&the&season 2
chrome&is&powerful&chrome 4
chrome&is&causing&the&Error&chrome. 3
the&service¢er&is&causing&the&Error. 5
the&error&is&comming&from&IE9 6
the &error&is&comming&from&chrome 7
chrome&is&causing&the&Error&IE9 . 9
the&service¢er&is&causing&the&Error. 10
the&error&is&comming&from&IE9 11
the&error&is&comming&from&chrome 12
;
run;
proc sql ;
select
case
when Message like "%chrome%" then "chorme"
when Message like "%IE9%" then "IE9"
else "None"
end as flag,ID
from check;
quit ;
try using a CASE statement with LIKE in case you wanted to match any value within a string. Hope the below example sheds some light.
data check;
input Message$40. ID ;
cards;
this&is&the&season 2
chrome&is&powerful&chrome 4
chrome&is&causing&the&Error&chrome. 3
the&service¢er&is&causing&the&Error. 5
the&error&is&comming&from&IE9 6
the &error&is&comming&from&chrome 7
chrome&is&causing&the&Error&IE9 . 9
the&service¢er&is&causing&the&Error. 10
the&error&is&comming&from&IE9 11
the&error&is&comming&from&chrome 12
;
run;
proc sql ;
select
case
when Message like "%chrome%" then "chorme"
when Message like "%IE9%" then "IE9"
else "None"
end as flag,ID
from check;
quit ;
I think you may be looking for this:
if indexw(upcase(Message),"CHROME",'&')>0 then flag="chrome";
Since this looks like unstructured text the upcase and the searched for word in upper case may work better if you want to find Chrome, CHROME, CHrome, cHrome and such. I recommend a similar UPCASE approach with the IE9 search as you may have folks enter ie9.
Waht do you want to do if both chrome and IE9 are present?
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.
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.