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

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

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

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