BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
tekish
Quartz | Level 8

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&center&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&center&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;

1 ACCEPTED SOLUTION

Accepted Solutions
anoopmohandas7
Quartz | Level 8

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&center&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&center&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 ;

View solution in original post

3 REPLIES 3
anoopmohandas7
Quartz | Level 8

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&center&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&center&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 ;
ballardw
Super User

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.

mkeintz
PROC Star

Waht do you want to do if both chrome and IE9 are present?

--------------------------
The hash OUTPUT method will overwrite a SAS data set, but not append. That can be costly. Consider voting for Add a HASH object method which would append a hash object to an existing SAS data set

Would enabling PROC SORT to simultaneously output multiple datasets be useful? Then vote for
Allow PROC SORT to output multiple datasets

--------------------------

hackathon24-white-horiz.png

2025 SAS Hackathon: There is still time!

Good news: We've extended SAS Hackathon registration until Sept. 12, so you still have time to be part of our biggest event yet – our five-year anniversary!

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
  • 3 replies
  • 1183 views
  • 1 like
  • 4 in conversation