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

Hello. 

I have a problem on deleting the last words of a sentense.

So, here is the details of the problem:

 

the column contains models of phones with colors, I must eliminate the colors and needless details from it, there are cases when the models contain two type of colors or details, for instance 

Alcatel One Touch POP C7 Bluish Black
iPhone 6 S 32 GB Full Gold

I want to delete the last two "words" if they are colors or details. how to do it?

 

thank you a lot.

1 ACCEPTED SOLUTION

Accepted Solutions
Kurt_Bremser
Super User
data want;
set have;
newstring = string; /* to get the same length */
newstring = ' ';
do i = 1 to countw(string) - 2;
  newstring = catx(' ',newstring,scan(string,i));
end;
drop string;
rename newstring=string;
run;
  

View solution in original post

6 REPLIES 6
Garik
Obsidian | Level 7

Alcatel One Touch Idol 2 Mini S Slate White
Alcatel One Touch POP C5 Dark Grey
Alcatel One Touch POP C5 Pure White
Alcatel One Touch POP C7 Bluish Black
Alcatel One Touch POP C7 Full White

RW9
Diamond | Level 26 RW9
Diamond | Level 26

And what is the logic for know that either the last, or last two words are to do with color?  "Full" for instance has nothing logically to do with color.  I would suggest you build a list of strings, or combination of possibles, and use that to remove information from the string, e.g.:

data colors;
  color="SLATE WHITE"; output;
  color="DARK GREY"; output;
  color="GREY"; output;
...
run;
proc sql;
create table WANT as
select tranwrd(A.STRING,B.COLOR) as STRING
from HAVE A
left join COLORS
on index(A.STRING,B.COLOR) > 0;
quit;

 

Garik
Obsidian | Level 7

thanks for replay;

 

But, could you write the same code for sas, I am not familiar with sql.

RW9
Diamond | Level 26 RW9
Diamond | Level 26

Not without a lot of code.  You will see the join is on the value of one being within the string of the other (the index()).  This is difficult to do in base SAS.  Its really worth learning SQL, its relatively simple and very powerful on joins.

Kurt_Bremser
Super User
data want;
set have;
newstring = string; /* to get the same length */
newstring = ' ';
do i = 1 to countw(string) - 2;
  newstring = catx(' ',newstring,scan(string,i));
end;
drop string;
rename newstring=string;
run;
  
Garik
Obsidian | Level 7

 can I use an array if I must check if the last two words are needless words? Suppose I have the list of the "words" that must be deleted from the string.

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
  • 6 replies
  • 1989 views
  • 3 likes
  • 3 in conversation