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: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

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