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

It would be helpful if you post some data -- even if it's just mocked up data.  

 

One question I have is:  Can "Toyota" or "Honda" appear within other text or is the brand name of the car in a column by itself?

For example, would your data look more like the "Brand" column below or would it look more like the "Car_Info" column below?

Brand Car_Info
Toyota 2014 Toyota Camry

 

If "Toyota" can appear anywhere in the column, then we need to take a different approach. 

 

Jim

mgrasmussen
Quartz | Level 8

Dear Jim

 

Thanks for the input.

 

I realize that I should have specified this detail. The data is as Brand, but at some point I will need to find similar solutions for variables which are organised as 'Car_Info'. I believe another person posted code which would work in such a scenario (with 'Car_info').

ErikLund_Jensen
Rhodochrosite | Level 12

Hi @mgrasmussen 

 

Here is a slightly modified version of @tarheel13 's code. It uses an automatic array _characters_ containing all defined character variables (a smart SAS feature). The index function + lowcase identifies all occurences of Toyota or Honda in all character strings regardless of case or other content in the text.

data mydata;
	set sashelp.cars;
	array cars $ _character_;
	flag='N';
	do i=1 to dim(cars);
		if index(lowcase(cars[i]),'toyota') or index(lowcase(cars[i]),'honda') then flag='Y';
	end;
	if flag='Y' then delete;
run;

 

mgrasmussen
Quartz | Level 8

Dear Erik

 

Thanks. These are great features and would work in my case.

 

What is the purpose of the lowcase function? I believe I understand what it does, but why is it necessary here? I would suspect that this specification is related to the fact that you write 'toyota' and 'honda' within the code in brackets. 

tarheel13
Rhodochrosite | Level 12
Lowcase function is more defensive programming in case your variables were not all in the same case. Using this function will capture Toyota or Honda regardless of case

SAS Innovate 2025: Call for Content

Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 16. Read more here about why you should contribute and what is in it for you!

Submit your idea!

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
  • 19 replies
  • 1765 views
  • 13 likes
  • 6 in conversation